Magento 2
Explain usage with examples in comments
Free Preview
👍 Alleviate confusion by adding comments to explain usage & return values
/**
 * Returns the total quantity of all items in cart as a floating point value.
 * Example: There is 1 of SKU "foo" in the cart, and 2 of SKU "bar" in the cart.
 * This function will return 3 because there are three total items in the cart.
 *
 * @returns {Number}
 */
getItemsQty: function () {
    return parseFloat(this.totals['items_qty']);
},

/**
 * Returns the total count of cart line items as an integer.
 * Example: There is 1 of SKU "foo" in the cart, and 2 of SKU "bar" in the cart.
 * This function will return 2 because there are two "line items" in the cart.
 *
 * @returns {Number}
 */
getCartLineItemsCount: function () {
    return parseInt(totals.getItems()().length, 10);
},

/**
 * Returns the total count of items to be used for the cart summary, dependant
 * upon which value the configuration is set to.
 * Example: useQty = true returns the total quantity of all items in cart
 * Example: useQty = false returns the total count of cart line items
 *
 * @returns {Number}
 */
getCartSummaryItemsCount: function () {
    return useQty ? this.getItemsQty() : this.getCartLineItemsCount();
},
👎 Confusion & ambiguity makes usage & return values hard to understand
/**
 * Returns cart items qty
 *
 * @returns {Number}
 */
getItemsQty: function () {
    return parseFloat(this.totals['items_qty']);
},

/**
 * Returns count of cart line items
 *
 * @returns {Number}
 */
getCartLineItemsCount: function () {
    return parseInt(totals.getItems()().length, 10);
},

/**
 * Returns shopping cart items summary (includes config settings)
 *
 * @returns {Number}
 */
getCartSummaryItemsCount: function () {
    return useQty ? this.getItemsQty() : this.getCartLineItemsCount();
},
Want to learn more?

Visit M.academy to learn much more about Magento, Laravel, PHP, Javascript, & Docker.

M.academy logo