From 107c121e0dee097b99c9067287890c1e821b3a2b Mon Sep 17 00:00:00 2001 From: adildev101 Date: Sun, 24 Sep 2023 18:46:46 +0100 Subject: [PATCH 1/3] add direct to cart feature --- assets/add-to-cart.js | 31 +++++++++++++++++++++++++ assets/featured-products.css | 1 + assets/product-slider.css | 1 + config/settings_schema.json | 6 +++++ sections/featured-product-slider.liquid | 3 +++ sections/featured-products.liquid | 2 ++ snippets/product-preview.liquid | 13 +++++++---- snippets/product-slider.liquid | 13 ++++++----- styles/featured-products.scss | 5 ++-- styles/product-slider.scss | 5 ++-- 10 files changed, 65 insertions(+), 15 deletions(-) diff --git a/assets/add-to-cart.js b/assets/add-to-cart.js index 1a0f7338..e1487b03 100644 --- a/assets/add-to-cart.js +++ b/assets/add-to-cart.js @@ -310,3 +310,34 @@ function preventCartDrawerOpening(templateName) { cartDrawerIcon.removeEventListener("click", toggleCartDrawer); window.location.reload(); } + +async function directAddToCart(productId) { + console.log("Product ID:", productId); + + let inventory; + + // Check if inventory is zero + if (inventory == 0) { + return notify(ADD_TO_CART_EXPECTED_ERRORS.empty_inventory, 'error'); + } + + try { + const response = await youcanjs.cart.addItem({ + productVariantId: productId, + quantity: 1 + }); + + if (response.error) throw new Error(response.error); + + updateCartCount(response.count); + await updateCartDrawer(); + + stopLoad('#loading__cart'); + notify(ADD_TO_CART_EXPECTED_ERRORS.product_added, 'success'); + toggleCartDrawer(); + } catch (err) { + stopLoad('#loading__cart'); + notify(err.message, 'error'); + } +} + diff --git a/assets/featured-products.css b/assets/featured-products.css index 3b24c285..0e51c3e0 100644 --- a/assets/featured-products.css +++ b/assets/featured-products.css @@ -47,6 +47,7 @@ display: flex; flex-direction: column; position: relative; + cursor: pointer; } .yc-featured-products .product-list .product-block .product-thumbnail { border: 1px solid #EFEFEF; diff --git a/assets/product-slider.css b/assets/product-slider.css index 780f184b..c0a23bf0 100644 --- a/assets/product-slider.css +++ b/assets/product-slider.css @@ -48,6 +48,7 @@ width: 100%; background-color: white; border-radius: 3px; + cursor: pointer; } .product-slider .product-block .product-details { text-align: center; diff --git a/config/settings_schema.json b/config/settings_schema.json index f6c28861..d55341cb 100644 --- a/config/settings_schema.json +++ b/config/settings_schema.json @@ -7,6 +7,12 @@ "id": "meta_title", "label": "Title" }, + { + "type": "checkbox", + "id": "direct_add_to_cart", + "label": "Direct add to cart", + "default": false + }, { "type": "select", "id": "theme_direction", diff --git a/sections/featured-product-slider.liquid b/sections/featured-product-slider.liquid index 548dd107..dd6d2785 100644 --- a/sections/featured-product-slider.liquid +++ b/sections/featured-product-slider.liquid @@ -395,3 +395,6 @@ ] } {%- endschema -%} + +{{ 'add-to-cart.js' | asset_url | script_tag_deferred }} + diff --git a/sections/featured-products.liquid b/sections/featured-products.liquid index 5e38fa9f..ad2d89ae 100644 --- a/sections/featured-products.liquid +++ b/sections/featured-products.liquid @@ -202,3 +202,5 @@ ] } {%- endschema -%} + +{{ 'add-to-cart.js' | asset_url | script_tag_deferred }} diff --git a/snippets/product-preview.liquid b/snippets/product-preview.liquid index 5b1ef94a..884ab61b 100644 --- a/snippets/product-preview.liquid +++ b/snippets/product-preview.liquid @@ -1,10 +1,11 @@ {{ 'featured-products.css' | asset_url | stylesheet_tag }} {{ 'countdown.css' | asset_url | stylesheet_tag }} - +{% if settings.direct_add_to_cart %} +
+{% else %} + diff --git a/snippets/product-slider.liquid b/snippets/product-slider.liquid index 12bc7bdb..fc91af33 100644 --- a/snippets/product-slider.liquid +++ b/snippets/product-slider.liquid @@ -49,10 +49,11 @@ class='splide__slide' {{ block.youcan_attributes }} > - + {% if settings.direct_add_to_cart %} +
diff --git a/styles/featured-products.scss b/styles/featured-products.scss index 42d62d6c..ff1d9a85 100644 --- a/styles/featured-products.scss +++ b/styles/featured-products.scss @@ -46,13 +46,14 @@ display: flex; flex-direction: column; position: relative; + cursor: pointer; .product-thumbnail { border: 1px solid #EFEFEF; border-radius: 3px; position: relative; overflow: hidden; - padding: 0 0 100%; + padding: 0 0 100%; height: 0; img { @@ -62,7 +63,7 @@ height: 100%; transform: translate(-50%,-50%); transition: transform .25s ease; - + &:hover { transform: translate(-50%,-50%) scale(1.1); } diff --git a/styles/product-slider.scss b/styles/product-slider.scss index 269dd60d..916170a0 100644 --- a/styles/product-slider.scss +++ b/styles/product-slider.scss @@ -2,7 +2,7 @@ .product-thumbnail { position: relative; overflow: hidden; - padding: 0 0 100%; + padding: 0 0 100%; height: 0; img { @@ -55,6 +55,7 @@ width: 100%; background-color: white; border-radius: 3px; + cursor: pointer; .product-details { text-align: center; @@ -75,4 +76,4 @@ text-decoration-line: line-through; } } -} \ No newline at end of file +} From 308e6ab1f2a061223bb596287bb2afd5afbf9fe1 Mon Sep 17 00:00:00 2001 From: adildev101 Date: Sun, 24 Sep 2023 18:58:33 +0100 Subject: [PATCH 2/3] remove unused code --- assets/add-to-cart.js | 9 --------- sections/featured-product-slider.liquid | 1 - 2 files changed, 10 deletions(-) diff --git a/assets/add-to-cart.js b/assets/add-to-cart.js index e1487b03..3f1e85b4 100644 --- a/assets/add-to-cart.js +++ b/assets/add-to-cart.js @@ -312,15 +312,6 @@ function preventCartDrawerOpening(templateName) { } async function directAddToCart(productId) { - console.log("Product ID:", productId); - - let inventory; - - // Check if inventory is zero - if (inventory == 0) { - return notify(ADD_TO_CART_EXPECTED_ERRORS.empty_inventory, 'error'); - } - try { const response = await youcanjs.cart.addItem({ productVariantId: productId, diff --git a/sections/featured-product-slider.liquid b/sections/featured-product-slider.liquid index dd6d2785..a2d528cc 100644 --- a/sections/featured-product-slider.liquid +++ b/sections/featured-product-slider.liquid @@ -397,4 +397,3 @@ {%- endschema -%} {{ 'add-to-cart.js' | asset_url | script_tag_deferred }} - From f83934f3f5a545c1fc8cbe2e252b9a48c90146e8 Mon Sep 17 00:00:00 2001 From: adildev101 Date: Mon, 25 Sep 2023 09:40:04 +0100 Subject: [PATCH 3/3] req --- assets/add-to-cart.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/add-to-cart.js b/assets/add-to-cart.js index 3f1e85b4..6049c942 100644 --- a/assets/add-to-cart.js +++ b/assets/add-to-cart.js @@ -323,12 +323,12 @@ async function directAddToCart(productId) { updateCartCount(response.count); await updateCartDrawer(); - stopLoad('#loading__cart'); notify(ADD_TO_CART_EXPECTED_ERRORS.product_added, 'success'); toggleCartDrawer(); } catch (err) { - stopLoad('#loading__cart'); notify(err.message, 'error'); + } finally { + stopLoad('#loading__cart'); } }