Display cart item compare at price on ajax-cart (timber drawer)

Highlighted
New Member
4 0 0

Hi,


I'm trying to get the line item compare at price for each product/variant in cart. As this is not a part of the cart.json, I added below snippet from another post in here. After doing so, i defined the compare at price in the items array in ajax-cart.js.

 

Here is the addComparePrice function, which is called just at the top of buildCart function (before the items are defined ofc).

 

function addCompareAtPrice(cart) {

    cart.items.forEach(function(item) {
        $.ajax({
            url: '/products/' + item.handle + '.js',
            dataType: 'json',
            async: false,
            success: function(product) {
                item["compare_at_price"] = 0;
                product.variants.forEach(function(variant) {
                    if (variant.compare_at_price != null && variant.id == item.variant_id) {
                        item["compare_at_price"] = variant.compare_at_price;
                    }
                });
            }
        });
    });

    return cart;
}

 

Here is the handlebars items, where i define the compareAtPrice to be used in my ajax-cart-template.

      item = {
        key: cartItem.key,
        line: index + 1, // Shopify uses a 1+ index in the API
        url: cartItem.url,
        img: prodImg,
        name: cartItem.product_title,
        variation: cartItem.variant_title,
        productId: cartItem.product_id,
        variantId: cartItem.variant_id,
        properties: cartItem.properties,
        productNameClass: cartItem.product_title.replace(/\s+/g, '-').toLowerCase().substring(0, 13),
        itemAdd: cartItem.quantity + 1,
        itemMinus: cartItem.quantity - 1,
        itemQty: cartItem.quantity,
        price: Shopify.formatMoney(
          cartItem.original_line_price,
          settings.moneyFormat
        ),
        compareAtPrice: Shopify.formatMoney(
          cartItem.compare_at_price,
          settings.moneyFormat
        ),
        discountedPrice: Shopify.formatMoney(
          cartItem.final_line_price,
          settings.moneyFormat
        ),
        discounts: cartItem.line_level_discount_allocations,
        discountsApplied:
          cartItem.line_level_discount_allocations.length === 0 ? false : true,
        vendor: cartItem.vendor
      };

 

What i want to achieve is:

a) Display the compare at price for the correct variant in cart for each line item
b) Compare at price should calculate with the quantity to reflect the correct total compare price.

The compare price is display not calculated with the qty of the cart item.

Secondly:
 We wish to calculate how much in currency you have saved in total and display - how can we calculate this?

Thank you in advance!

0 Likes