commit bd567d0788f1bba3d56f7dd042bf8258e763f91d Author: Mykola Zahorulko Date: Thu Aug 14 13:01:01 2025 +0200 Add button feature diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/add-to-basket.iml b/.idea/add-to-basket.iml new file mode 100644 index 0000000..24643cc --- /dev/null +++ b/.idea/add-to-basket.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..2439840 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0949533 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..15c63b9 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# Add To Basket Button # + +Use the function to generate the markup. Then just insert the button somewhere in your item element. + +### How to use ### + +1. Copy the code, and use **idmAddToBasket** function to generate the markup +2. Don't forget to put it somewhere after + +``` +const afterWhatPaste = document.getElementById('item-container'); +const idmAddToBasketElement = idmAddToBasket(product, size); +afterWhatPaste.insertAdjacentHTML('afterend', idmAddToBasketElement); +``` + +--- + +Created by • **[IdoMods](https://idomods.pl/)** • 2025 \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..1bf4630 --- /dev/null +++ b/main.js @@ -0,0 +1,92 @@ +/** + * @product {object} product information + * @size product size, get form product.sizes.[0] + * @return html markup + **/ +function idmAddToBasket(product, size) { + const can = product?.type === 'product' && product?.sizes?.length === 1 && size; + if (!can) return ''; + + const sellBy = size?.unitSellby || 1; + const precision = size?.unitPrecision || 0; + const max = (typeof size?.amount === 'number' && size.amount > 0) ? size.amount : ''; + + return ` +
+
+ + + + +
+ + + + + +
+ + +
+
`; +} + +// +/- i walidacja inputów ilości +document.addEventListener('click', (e) => { + const wrapper = e.target.closest('.idm-products-banner__qty'); + if (!wrapper) return; + + const input = wrapper.querySelector('.idm-products-banner__qty-input'); + const step = parseFloat(wrapper.dataset.sellBy || '1'); + const precision = parseInt(wrapper.dataset.precision || '0'); + const max = parseFloat(wrapper.dataset.max || '999999'); + let current = parseFloat(input.value) || 0; + + if (e.target.classList.contains('idm-products-banner__qty-increase')) { + current += step; + if (current > max) current = max; + } else if (e.target.classList.contains('idm-products-banner__qty-decrease')) { + current -= step; + if (current < step) current = step; + } + + input.value = current.toFixed(precision); +}); + +document.addEventListener('blur', (e) => { + if (!e.target.classList.contains('idm-products-banner__qty-input')) return; + + const input = e.target; + const wrapper = input.closest('.idm-products-banner__qty'); + const step = parseFloat(wrapper.dataset.sellBy || '1'); + const precision = parseInt(wrapper.dataset.precision || '0'); + const max = parseFloat(wrapper.dataset.max || '999999'); + + let val = parseFloat(input.value); + if (isNaN(val) || val < step) { + val = step; + } else if (val > max) { + val = max; + } else { + val = Math.round(val / step) * step; + } + + input.value = val.toFixed(precision); +}, true); + +// helper +const escapeHtml = (v = '') => String(v) + .replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"') + .replace(/'/g, '''); \ No newline at end of file diff --git a/style.less b/style.less new file mode 100644 index 0000000..f1fb147 --- /dev/null +++ b/style.less @@ -0,0 +1,53 @@ +.idm-products-banner__qty { + display: flex; + justify-content: space-between; + width: 100%; + align-items: center; + gap: 1rem; +} + +.idm-products-banner__qty-input { + height: 4rem; + text-align: center; + border: 1px solid #ccc; + width: 60%; + max-width: unset; + min-width: unset; +} + +.idm-products-banner__qty button { + background: #000; + height: 4rem; + width: 4rem; + color: #fff; + border-radius: 0.5rem; + min-width: 4rem; +} + +.idm-products-banner__add-to-basket { + display: flex; + align-items: end; + flex-direction: column; + width: 80%; + margin: 0 auto; +} + +.btn.--solid.--medium.idm-products-banner__add-to-basket-button { + height: 4rem; + background: #000; + display: flex; + align-items: center; + justify-content: center; + border: 1px solid #000; +} + +.btn.--solid.--medium.idm-products-banner__add-to-basket-link { + width: 100%; + height: 4rem; + display: flex; + align-items: center; + justify-content: center; + background: #000; + border: 1px solid #000; + margin-top: auto; +} \ No newline at end of file