انتقل إلى المحتوى الرئيسي

Cart Module

qumra.cart provides methods to manage the shopping cart. All mutation methods emit events and return the updated Cart object.

Methods

qumra.cart.add(productId, quantity, options?)

Add a product to cart.

const cart = await qumra.cart.add('PRODUCT_ID', 2);

// With variant options
const cart = await qumra.cart.add('PRODUCT_ID', 1, ['OPT1', 'OPT2']);

Events: cart:addingcart:added / cart:error


qumra.cart.update(itemId, quantity)

Update cart item quantity.

const cart = await qumra.cart.update('ITEM_ID', 5);

Events: cart:updatingcart:updated / cart:error


qumra.cart.remove(itemId)

Remove item from cart.

const cart = await qumra.cart.remove('ITEM_ID');

Events: cart:removingcart:removed / cart:error


qumra.cart.clear()

Clear all items from cart.

const cart = await qumra.cart.clear();

Events: cart:cleared / cart:error


qumra.cart.fetch()

Fetch fresh cart from server (updates local state).

const cart = await qumra.cart.fetch();

qumra.cart.get()

Get cached cart state (no network request).

const cart = qumra.cart.get();
if (cart) {
console.log(cart.totalPrice, cart.totalQuantity);
}

qumra.cart.has(productId)

Check if a product is in the cart.

if (qumra.cart.has('PRODUCT_ID')) {
showRemoveButton();
}

qumra.cart.getQuantity(productId)

Get total quantity of a product in the cart.

const qty = qumra.cart.getQuantity('PRODUCT_ID'); // 2

Cart Response

All mutation methods (add, update, remove, clear, fetch) return a Cart object:

{
success: true,
_id: "...",
status: "ACTIVE",
items: [CartItem],
totalPrice: 2025,
totalCompareAtPrice: 2025,
totalSavings: 0,
totalQuantity: 1,
couponApplied: false,
couponDiscount: 0,
isFastOrder: false,
offers: {
applied: [],
totalsBefore: { totalPrice, totalCompareAtPrice, totalSavings, totalQuantity },
totalsAfter: { totalPrice, totalCompareAtPrice, totalSavings, totalQuantity }
},
createdAt: "2026-02-06T19:09:50.991Z",
updatedAt: "2026-02-08T02:30:02.032Z"
}

CartItem

Each item in cart.items:

{
_id: "...",
productId: "...",
variantId: null, // or variant ID string
quantity: 1,
price: 2025,
compareAtPrice: 2025,
totalPrice: 2025,
totalCompareAtPrice: 2025,
totalSavings: 0,
displayPrice: 2025,
displayTotalPrice: 2025,
itemOfferDiscount: 0,
productData: {
title: "Product Name",
slug: "product-name",
price: 2025,
image: {
_id: "...",
fileUrl: "https://cdn.qumra.cloud/media/..."
}
},
createdAt: "...",
updatedAt: "..."
}

Usage Example

const cart = await qumra.cart.add('PRODUCT_ID', 1);

// Cart totals
document.getElementById('cart-total').textContent = cart.totalPrice;
document.getElementById('cart-count').textContent = cart.totalQuantity;

// Render items
cart.items.forEach(item => {
console.log(item.productData.title); // "Product Name"
console.log(item.productData.image.fileUrl); // CDN URL
console.log(item.quantity); // 1
console.log(item.totalPrice); // 2025
});

// Check offers
if (cart.offers.applied.length > 0) {
const saved = cart.offers.totalsBefore.totalPrice - cart.offers.totalsAfter.totalPrice;
console.log('You saved:', saved);
}

AJAX Comparison

OperationAJAXSDK
Add to cartfetch('/ajax/cart/add', { method: 'POST', body: ... })qumra.cart.add(id, qty)
Update quantityfetch('/ajax/cart/change', { method: 'POST', body: ... })qumra.cart.update(itemId, qty)
Remove itemfetch('/ajax/cart/remove', { method: 'POST', body: ... })qumra.cart.remove(itemId)
Clear cartfetch('/ajax/cart/clear', { method: 'POST' })qumra.cart.clear()
Get cartfetch('/ajax/cart')qumra.cart.fetch() / qumra.cart.get()

SDK advantages:

  • Automatic event emission (analytics, UI updates)
  • Built-in error handling with cart:error events
  • Local state caching with get()
  • Helper methods: has(), getQuantity()