Skip to content

Commit

Permalink
update cart items count near page title
Browse files Browse the repository at this point in the history
  • Loading branch information
eihabkhan1 committed Oct 21, 2024
1 parent 5e4a9cf commit 5364f28
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
3 changes: 3 additions & 0 deletions themes/aura/assets/cart.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
font-size:14px;
line-height:22px;
}
.yc-cart .cart-table .header-block #cart-items-count{
text-transform:lowercase;
}
.yc-cart .cart-table .header-block .checkout_cta{
max-width:100%;
width:437px;
Expand Down
30 changes: 19 additions & 11 deletions themes/aura/assets/cart2.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const CartUI = {
}
}
},
updateCartItem(cartItemId, productVariantId, quantity, itemSubtotal) {
updateCartItem(cartItemId, productVariantId, quantity, itemSubtotal, inventory) {
const itemRow = document.getElementById(cartItemId);
const [input, totalPrice] = itemRow.querySelectorAll(`input[id="${productVariantId}"], .total-price`);
const decrease = input.previousElementSibling;
Expand All @@ -98,22 +98,24 @@ const CartUI = {
decrease,
cartItemId,
productVariantId,
quantity
quantity,
inventory
);

input.value = quantity;
totalPrice.innerHTML = formatCurrency(itemSubtotal, currencyCode, customerLocale);
},
setQuantityButtonsHandlers(increase, decrease, cartItemId, productVariantId, quantity) {
setQuantityButtonsHandlers(increase, decrease, cartItemId, productVariantId, quantity, inventory) {
decrease
.querySelector('button')
.setAttribute('onclick', `updateQuantity('${cartItemId}', '${productVariantId}', '${Number(quantity) - 1}')`);
.setAttribute('onclick', `updateQuantity('${cartItemId}', '${productVariantId}', '${Number(quantity) - 1}', '${inventory}')`);
increase
.querySelector('button')
.setAttribute('onclick', `updateQuantity('${cartItemId}', '${productVariantId}', '${Number(quantity) + 1}')`);
.setAttribute('onclick', `updateQuantity('${cartItemId}', '${productVariantId}', '${Number(quantity) + 1}', '${inventory}')`);
},
updateCartBadge(count) {
const cartItemsBadge = document.getElementById('cart-items-badge');
const [cartItemsBadge, cartItemCount] = document.querySelectorAll('#cart-items-badge, #cart-items-count');
cartItemCount.innerText = `(${count} ${CART_DRAWER_TRANSLATION.itemsName})`

if (cartItemsBadge) {
cartItemsBadge.textContent = count;
Expand All @@ -136,19 +138,25 @@ const CartUI = {
};

// Events
async function updateQuantity(cartItemId, productVariantId, quantity) {
if (quantity < 1) {
return;
async function updateQuantity(cartItemId, productVariantId, quantity, inventory) {
let [parsedQuantity, parsedInventory] = [Number(quantity), Number(inventory)];

if (parsedQuantity < 1) {
// TODO: A better UX would be to prompt the seller to remove the item from cart
return;
}

if (Number.isFinite(parsedInventory) && parsedQuantity > parsedInventory) {
return notify(ADD_TO_CART_EXPECTED_ERRORS.max_quantity + inventory, 'warning');
}

load(`#loading__${cartItemId}`);
try {
const updatedCart = await CartService.updateItemQuantity(cartItemId, productVariantId, quantity);
const updatedCart = await CartService.updateItemQuantity(cartItemId, productVariantId, parsedQuantity);
const cartItem = updatedCart.items.find(item => item.id === cartItemId);
const itemSubtotal = cartItem.price * cartItem.quantity;

CartUI.updateCartItem(cartItemId, productVariantId, quantity, itemSubtotal);
CartUI.updateCartItem(cartItemId, productVariantId, parsedQuantity, itemSubtotal, inventory);
CartUI.updateTotalPrice(updatedCart.total, updatedCart.items);
} catch (e) {
notify(e.message, 'error');
Expand Down
2 changes: 1 addition & 1 deletion themes/aura/sections/main-cart.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="header-block">
<div class="header-bar">
<h2>{{ 'cart.title' | t }}</h2>
<p class='mt-1'>
<p class='mt-1' id="cart-items-count">
({{ cart.items | size }}
{{ 'cart.items_count' | t }})
</p>
Expand Down
2 changes: 1 addition & 1 deletion themes/aura/snippets/quantity-input.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
aria-label="quantity"
>
<div class='increase'>
<button onclick="increaseQuantity('{{ item.id }}', '{{ item.product_variant_id }}', '{{ item.quantity }}', {{ item.inventory }})">
<button onclick="updateQuantity('{{ item.id }}', '{{ item.product_variant_id }}', '{{ item.quantity | plus: 1 }}', {{ item.inventory }})">
+
</button>
</div>
Expand Down
4 changes: 4 additions & 0 deletions themes/aura/styles/cart.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
}
}

#cart-items-count {
text-transform: lowercase;
}

.checkout_cta {
max-width: 100%;
width: 437px;
Expand Down

0 comments on commit 5364f28

Please sign in to comment.