Skip to content

Commit

Permalink
fix cart drawer bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
bj-anas committed Oct 17, 2024
1 parent ee8a57d commit 5b01472
Showing 4 changed files with 13 additions and 10 deletions.
16 changes: 9 additions & 7 deletions themes/harmony/assets/add-to-cart.js
Original file line number Diff line number Diff line change
@@ -64,9 +64,11 @@ function attachRemoveItemListeners() {
const cartItemId = event.target.getAttribute('data-cart-item-id');
const productVariantId = event.target.getAttribute('data-product-variant-id');

await removeCartItem(cartItemId, productVariantId);
await updateCartDrawer();
updateCartCount(-1, true);
if(cartItemId && productVariantId) {
await removeCartItem(cartItemId, productVariantId);
await updateCartDrawer();
updateCartCount(-1, true);
}
})
);
}
@@ -303,7 +305,7 @@ function showSpinner(spinnerElement) {
}

function hideSpinner(spinnerElement) {
const spinnerAction = spinnerElement.previousElementSibling;
const spinnerAction = spinnerElement?.previousElementSibling;
toggleVisibility(spinnerElement, spinnerAction);
}

@@ -368,15 +370,15 @@ function preventCartDrawerOpening(templateName) {
window.location.reload();
}

async function directAddToCart(event, productId, inventory) {
async function directAddToCart(event, productId, inventory, isTrackingInventory) {
event.preventDefault();

await trackVariantQuantityOnCart(productId);

const variantQuantityInCart = parseInt(document.querySelector('#cartQuantity')?.value) || null;
const isTrackingInventoryAvailable = Boolean(isTrackingInventory) && Number.isFinite(inventory);
const newQuantity = 1;

if (Number.isFinite(inventory) && ((variantQuantityInCart ?? 0) + newQuantity) > inventory) {
if (isTrackingInventoryAvailable && ((variantQuantityInCart ?? 0) + newQuantity) > inventory) {
return notify(ADD_TO_CART_EXPECTED_ERRORS.max_quantity + inventory, 'warning');
}

3 changes: 2 additions & 1 deletion themes/harmony/assets/main.js
Original file line number Diff line number Diff line change
@@ -399,6 +399,8 @@ function restrictInputValue(inputElement, maxInventoryValue) {
async function trackVariantQuantityOnCart(selectedVariantId) {
try {
load('#loading__cart');
const cartQuantityInput = document.querySelector('#cartQuantity');
cartQuantityInput.value = 0;
const cart = await youcanjs.cart.fetch();

if (!cart) {
@@ -410,7 +412,6 @@ async function trackVariantQuantityOnCart(selectedVariantId) {
}

const cartItem = cart.items.find((item) => item.productVariant.id === selectedVariantId);
const cartQuantityInput = document.querySelector('#cartQuantity');

if (!cartItem || cartItem.productVariant.product.track_inventory === false) {
return;
2 changes: 1 addition & 1 deletion themes/harmony/snippets/product-preview.liquid
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@
</div>
<div class='product-btn'>
{% if settings.direct_add_to_cart and item.variants.size <= 1 %}
<button class="yc-btn-secondary" onclick="directAddToCart(event, '{{ item.variants[0].id }}', {{ item.variants[0].inventory }})">
<button class="yc-btn-secondary" onclick="directAddToCart(event, '{{ item.variants[0].id }}', {{ item.variants[0].inventory }}, {{ item.isTrackingInventory }})">
{{ 'general.direct_add_to_cart' | t }}
</button>
{% else %}
2 changes: 1 addition & 1 deletion themes/harmony/snippets/product-slider.liquid
Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@
{% endif %}
</div>
{% if settings.direct_add_to_cart and block.settings.product.variants.size <= 1 %}
<button class="yc-btn-secondary--large" onclick="directAddToCart(event, '{{ block.settings.product.variants[0].id }}', {{ block.settings.product.variants[0].inventory }});">
<button class="yc-btn-secondary--large" onclick="directAddToCart(event, '{{ block.settings.product.variants[0].id }}', {{ block.settings.product.variants[0].inventory }}, {{ block.settings.product.isTrackingInventory }})">
{{ 'general.direct_add_to_cart' | t }}
</button>
{% else %}

0 comments on commit 5b01472

Please sign in to comment.