Javascript for loop problem including compare at price at cart

Venturesome
Tourist
11 0 1

Greetings community

 

Recently i had a problem with displaying compare_at_price in the cart. I ended up solving it by adding an ajax response inside the function that updates my cart . The problem that i now have is that i want to display it only if it exists and if not just the regural price. I inserted an if statement with a straightforward conditon in order to display the correct price always inside the loop that goes through every cart item.The problem that came up is that is works fine until you add a product that doesnt have a discount and vice verca .And by that i mean that if i add a product with a compare at price it displays it as i want it with the compare at price deleted and then the regular price but if i add then a product with no compare price the price dissapears because apparently it passes the if statement that the first product's price corresponds to. The same thing happens if i do it in the reverse way, add a product with no compare at price and then then add a product with compare at price .For some reason it will meet the condition that the first product meets. i know its not very clear, this is the code that do it and maybe it will help people wanting to add compare at price to their cart .The if statement that determines which of the prices i want to display is at the bottom.

Any suggestions are highly appreaciated 

thanks in advance

  doUpdateDropdownCart: function (cart) {
        
            var template = '<li class="item" id="cart-item-{ID}" data-product-id="{ID}"><a href="{URL}" class="product-image"><img src="{IMAGE}" alt="{TITLE}"></a><div class="product-details"><a class="product-name" href="{URL}">{TITLE}</a><p id="variant-dropdown1" >Χρώμα:{VARIANT1}</p><p id="variant-dropdown2">Μέγεθος:{VARIANT}</p><div class="quantity-dropdown"><p id="qty-dropdown">Ποσότητα:{QUANTITY}</p></div><div class="cart-collateral"><span class="origin-price price" id="dropdown-cart-original-price" >{PRICE}</span><span class="compare-price comprice" id="compare-price-dropdown">{PRICE1}</span><span class="black-price price" id="dropdown-original-price">{PRICE}</span></div></li>';
    
                  
            $('[data-cart-count]').text(cart.item_count);
            $('.wrapper-cartCount').show();
            dropdownCart.find('.summary .price').html(Shopify.formatMoney(cart.total_price, window.money_format));
            $('#sidebar-cart').find('.cart-footer .notranslate').html(Shopify.formatMoney(cart.total_price, window.money_format));
        
            if (cart.item_count > 0) {
                var item_id = $('.item.cur--change').data('product-id');
                var line_item = parseInt($('.item.cur--change').find('input[name="quantity"]').data('line'));
                var quantity = parseInt($('.item.cur--change').find('input[name="quantity"]').val());
                for (var i = 0; i < cart.items.length; i++) {
                    var item = template;
                    var cur_id = cart.items[i].id;
                    var arr = [];
                       $.ajax({
            url: '/cart?view=data',
  		 	async: false, 
           success: function(response) {
           json_response = JSON.parse(response);
           console.log( 'response', json_response );
          },
             error: function(status) {
           console.warn( 'ERROR', status );
          }
          });
                        
                    if (i == 0) {
                    miniProductList.html('');
                    }                                      
                                       
                    item = item.replace(/\{ID\}/g, cart.items[i].id);
                    item = item.replace(/\{URL\}/g, cart.items[i].url);
                    item = item.replace(/\{TITLE\}/g, ella.translateText(cart.items[i].product_title));
                    item = item.replace(/\{VARIANT\}/g, cart.items[i].variant_options[1] || '');
                    item = item.replace(/\{VARIANT1\}/g, cart.items[i].variant_options[0] || '');
                    item = item.replace(/\{QUANTITY\}/g, cart.items[i].quantity);
                    item = item.replace(/\{IMAGE\}/g, Shopify.resizeImage(cart.items[i].image, '160x'));
                    // if (cart.cart_level_discount_applications.length > 0) {
                    //   item = item.replace(/\{PRICE\}/g, Shopify.formatMoney(cart.items[i].discounted_price, window.money_format));
                    // } else {                 
                   // }
                    item = item.replace(/\{PRICE1\}/g, Shopify.formatMoney(json_response.items[i].compare_at_price, window.money_format));
                    item = item.replace(/\{PRICE\}/g, Shopify.formatMoney(cart.items[i].price, window.money_format));
                  
                    miniProductList.append(item);

                    if (quantity > line_item && line_item > 0 ) {
                        $('#dropdown-cart [data-product-id="' + item_id + '"]').find('.error-message-input').html('<i class="fa fa-exclamation-circle"></i>' + window.inventory_text.warning_quantity + ': ' + line_item);
                    } else {
                        $('#dropdown-cart [data-product-id="' + item_id + '"]').find('.error-message-input').text('');
                    }
                    $.ajax({
                        url: "/products/" + cart.items[i].handle + "?view=cart_edit",
                        success: function(data){
                            var json = $(data).find('.get_script_content').data('array_json');
                            arr = json.filter(function(cur){
                                return parseInt(cur.id) === cur_id
                            });
                            if (arr.length) {
                                $('#dropdown-cart [data-product-id="' + cur_id + '"]').find('[name="quantity"]').attr('data-line', arr[0].value);
                            }
                        }
                    });

                    if (cart.cart_level_discount_applications.length > 0 && $('[data-cart-discount-wrapper]').length) {
                        var cartDiscounts = cart.cart_level_discount_applications;
                        var title = cartDiscounts[0].title;
                        var total_discount = cartDiscounts[0].total_allocated_amount;
                        $('[data-cart-discount-wrapper]').show();
                        $('[data-cart-discount-title]').text(title);
                        $('[data-cart-discount-amount]').html(Shopify.formatMoney(total_discount, window.money_format))
                    } else {
                        $('[data-cart-discount-wrapper]').hide();
                    }
                if  (json_response.items[i].compare_at_price == cart.items[i].price || json_response.items[i].compare_at_price == null) { 
                     $('.comprice').hide();
                     $('.origin-price').hide();
                                     }  
                        else if (json_response.items[i].compare_at_price > cart.items[i].price) {
                      $('.black-price').hide(); 
                   }
                  $('#dropdown-view-cart').html("Καλαθι" + '&nbsp;' + cart.item_count);
                }

 

0 Likes