@shopgate/pwa-common-commerce
Version:
Commerce library for the Shopgate Connect PWA.
10 lines • 1.64 kB
JavaScript
/**
* Returns the amount of items are counted towards the
* displayed cart quantity.
* @param {Object} product The product object.
* @param {Array} cartItems Existing cart items to check for.
* @returns {number}
*/export var getDisplayedProductQuantity=function getDisplayedProductQuantity(_ref){var productId=_ref.productId,_ref$product=_ref.product,product=_ref$product===void 0?{}:_ref$product,fulfillment=_ref.fulfillment,quantity=_ref.quantity;var cartItems=arguments.length>1&&arguments[1]!==undefined?arguments[1]:[];// Products with custom units like (kg, lbs, ..)
// are counted as 1 per line item.
if(product.unit&&product.hasCatchWeight){// If product is already existing in cart we only count them once.
var existingCartItem=cartItems.find(function(cartItem){var _cartItem$fulfillment,_fulfillment$location,_cartItem$fulfillment2,_cartItem$fulfillment3;var sameProduct=productId===cartItem.product.id;var sameFulfillmentMethod=(fulfillment===null||fulfillment===void 0?void 0:fulfillment.type)===((_cartItem$fulfillment=cartItem.fulfillment)===null||_cartItem$fulfillment===void 0?void 0:_cartItem$fulfillment.type)&&(fulfillment===null||fulfillment===void 0?void 0:(_fulfillment$location=fulfillment.location)===null||_fulfillment$location===void 0?void 0:_fulfillment$location.code)===((_cartItem$fulfillment2=cartItem.fulfillment)===null||_cartItem$fulfillment2===void 0?void 0:(_cartItem$fulfillment3=_cartItem$fulfillment2.location)===null||_cartItem$fulfillment3===void 0?void 0:_cartItem$fulfillment3.code);return sameProduct&&sameFulfillmentMethod;});if(!existingCartItem){return 1;}return 0;}return quantity;};