Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

YSHOP2-876: Direct Add to cart feature #397

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions assets/add-to-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,25 @@ function preventCartDrawerOpening(templateName) {
cartDrawerIcon.removeEventListener("click", toggleCartDrawer);
window.location.reload();
}

async function directAddToCart(productId) {
try {
adildev101 marked this conversation as resolved.
Show resolved Hide resolved
const response = await youcanjs.cart.addItem({
productVariantId: productId,
quantity: 1
});

if (response.error) throw new Error(response.error);

updateCartCount(response.count);
await updateCartDrawer();

notify(ADD_TO_CART_EXPECTED_ERRORS.product_added, 'success');
toggleCartDrawer();
} catch (err) {
notify(err.message, 'error');
} finally {
stopLoad('#loading__cart');
}
}

1 change: 1 addition & 0 deletions assets/featured-products.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions assets/product-slider.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
width: 100%;
background-color: white;
border-radius: 3px;
cursor: pointer;
}
.product-slider .product-block .product-details {
text-align: center;
Expand Down
6 changes: 6 additions & 0 deletions config/settings_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions sections/featured-product-slider.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,5 @@
]
}
{%- endschema -%}

{{ 'add-to-cart.js' | asset_url | script_tag_deferred }}
2 changes: 2 additions & 0 deletions sections/featured-products.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,5 @@
]
}
{%- endschema -%}

{{ 'add-to-cart.js' | asset_url | script_tag_deferred }}
13 changes: 8 additions & 5 deletions snippets/product-preview.liquid
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{{ 'featured-products.css' | asset_url | stylesheet_tag }}
{{ 'countdown.css' | asset_url | stylesheet_tag }}

<a
href='{{ item.url }}'
class='product-block'
>
{% if settings.direct_add_to_cart %}
<div class='product-block' onclick="directAddToCart('{{ item.variants[0].id }}')">
{% else %}
<div class='product-block' onclick="window.location.href='{{ item.url }}'">
{% endif %}
<div class='product-thumbnail'>
{% if item.thumbnail %}
<img
Expand All @@ -26,9 +27,11 @@
</div>
{% endif %}
</div>

<div class='product-details'>
<span class='product-title'>{{ item.name }}</span>
</div>

<div class='prices'>
{% if item.price %}
<span class='product-price'>{{ item.price | money }}</span>
Expand All @@ -37,4 +40,4 @@
<span class='compare-at-price'>{{ item.compare_at_price | money }}</span>
{% endif %}
</div>
</a>
</div>
13 changes: 7 additions & 6 deletions snippets/product-slider.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@
class='splide__slide'
{{ block.youcan_attributes }}
>
<a
href='{{ block.settings.product.url }}'
class='product-block'
>
{% if settings.direct_add_to_cart %}
<div class='product-block' onclick="directAddToCart('{{ block.settings.product.variants[0].id }}')">
{% else %}
<div class='product-block' onclick="window.location.href='{{ block.settings.product.url }}'">
{% endif %}
<div class="product-slider">
<div class='product-thumbnail'>
<img
Expand Down Expand Up @@ -80,9 +81,9 @@
{% endif %}
</div>
</div>
</a>
</div>
</li>
{%- endfor %}
</ul>
</div>
</div>
</div>
5 changes: 3 additions & 2 deletions styles/featured-products.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -62,7 +63,7 @@
height: 100%;
transform: translate(-50%,-50%);
transition: transform .25s ease;

&:hover {
transform: translate(-50%,-50%) scale(1.1);
}
Expand Down
5 changes: 3 additions & 2 deletions styles/product-slider.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.product-thumbnail {
position: relative;
overflow: hidden;
padding: 0 0 100%;
padding: 0 0 100%;
height: 0;

img {
Expand Down Expand Up @@ -55,6 +55,7 @@
width: 100%;
background-color: white;
border-radius: 3px;
cursor: pointer;

.product-details {
text-align: center;
Expand All @@ -75,4 +76,4 @@
text-decoration-line: line-through;
}
}
}
}