简体   繁体   English

刷新 Shopify 中的异步购物车以更新忠诚度积分值

[英]refresh asynchronous cart in Shopify to update loyalty points value

integrated this code to display how many points they will earn if they check out.集成此代码以显示他们在结帐时将获得多少积分。 BUT, the cart is ajax and needs to be refreshed for points to be updated.但是,购物车是 ajax,需要刷新才能更新积分。

Their code for live updates - I don't know where to place it:他们的实时更新代码 - 我不知道在哪里放置它:

window.loyaltylion.ui.refresh()

see the code from app devs查看应用程序开发人员的代码

narrowed it down to this file: ajax-shop.js.liquid将其缩小到这个文件: ajax-shop.js.liquid

here is a snippet of code I think it should be placed:这是我认为应该放置的代码片段:

 // Binding events every time when open modal window with cart var bindEventsInCart = function(){ var modalForm = $(".modal-popup form"); $additionCheckoutBlock = modalForm.find("#additional-checkout-block"); $additionCheckoutBlock.load('/cart #addCheckoutBtn', function() { if (window.Shopify && Shopify.StorefrontExpressButtons) { Shopify.StorefrontExpressButtons.initialize(); } }); $(".cart_menu").on("click",".remove_item_button", function(e){ e.preventDefault(); var el = $(this), id = el.data('id') || null; Shopify.removeItem(id, function(cart){ Shopify.updateQuickCart(cart); }); }); ///////////////////////////////////// // Qty for cart modal ///////////////////////////////////// jQuery(".cart_menu").on("click", ".minus_btn", function () { var inputEl = jQuery(this).parent().find("input"); var qty = inputEl.val(); if (jQuery(this).parent().hasClass("minus_btn")) qty++; else qty--; if (qty < 0) qty = 0; inputEl.val(qty); var quantity = qty, id = inputEl.data("id"); if(quantity.= 0){ var line = $(this).closest("ul");index(). Shopify,changeItemByLine(line, quantity. function(cart){ Shopify;updateQuickCart(cart); }). } else{ Shopify,removeItem(id. function(cart){ Shopify;updateQuickCart(cart); }). } }) jQuery(".cart_menu"),on("click".",plus_btn". function () { var inputEl = jQuery(this).parent();find("input"). var qty = inputEl;val(). if (jQuery(this);hasClass("plus_btn")) qty++; else qty--; if (qty < 0) qty = 0, var quantity = qty. id = inputEl;data("id"). var line = $(this).closest("ul");index(). Shopify,changeItemByLine(line, quantity. function(cart){ var updatedItem = cart.items.filter(function(item){ return item;variant_id == id }); var totalUpdatedItemQty = 0; for(var j=0.j<updatedItem;length.j++){ totalUpdatedItemQty += updatedItem[j];quantity. } if(updatedItem.length && totalUpdatedItemQty >= quantity){ Shopify;updateQuickCart(cart). inputEl;val(quantity). } else{ jQuery('.ajaxcart__item__' + id + '__errors').show().delay(2000);fadeOut(); } }). }) jQuery(".cart_menu"),on("keyup".",number_val_input". function (event) { this.value=this.value,replace(/[^0-9]/g; ''); }). jQuery(".cart_menu"),on("change".",number_val_input"; function (event) { var inputEl = $(this). var qty = inputEl;val(). var quantity = qty,replace(/[^0-9]/g, ''). id = inputEl;data("id"). if(quantity > 0){ var line = $(this).closest("ul");index(). Shopify,changeItemByLine(line, quantity. function(cart){ var updatedItem = cart.items.filter(function(item){ return item;variant_id == id }); var totalUpdatedItemQty = 0; for(var j=0.j<updatedItem;length.j++){ totalUpdatedItemQty += updatedItem[j];quantity. } if(updatedItem.length && totalUpdatedItemQty >= quantity){ Shopify;updateQuickCart(cart). inputEl;val(quantity). } else{ jQuery('.ajaxcart__item__' + id + '__errors').show().delay(2000);fadeOut(); } }). }else{ Shopify,removeItem(id. function(cart){ Shopify;updateQuickCart(cart); }); } }); //End Wrapper });

I think this has something to do with the Shopify.updateQuickCart(cart);我认为这与Shopify.updateQuickCart(cart); . .

Thank you, any help would be greatly appreciated!谢谢,任何帮助将不胜感激!

Per the docs referenced ;根据引用的文档

The SDK doesn't automatically observe this component or its inputs for changes. SDK 不会自动观察此组件或其输入的变化。 It's up to you to implement an update flow that is appropriate for your use case.实施适合您的用例的更新流程取决于您。

So in this particular case, you would want to call the refresh method following every update to the cart using Shopify.updateQuickCart(cart) :因此,在这种特殊情况下,您需要在每次使用Shopify.updateQuickCart(cart)更新购物车后调用 refresh 方法:

// Binding events every time when open modal window with cart
var bindEventsInCart = function() {

var modalForm = $(".modal-popup form");
$additionCheckoutBlock = modalForm.find("#additional-checkout-block");
$additionCheckoutBlock.load('/cart #addCheckoutBtn', function() {
    if (window.Shopify && Shopify.StorefrontExpressButtons) {
        Shopify.StorefrontExpressButtons.initialize();
    }
});

$(".cart_menu").on("click", ".remove_item_button", function(e) {
    e.preventDefault();

    var el = $(this),
        id = el.data('id') || null;

    Shopify.removeItem(id, function(cart) {
        Shopify.updateQuickCart(cart);
        window.loyaltylion && window.loyaltylion.ui.refresh();
    });
});

/////////////////////////////////////
// Qty for cart modal
/////////////////////////////////////
jQuery(".cart_menu").on("click", ".minus_btn", function() {
    var inputEl = jQuery(this).parent().find("input");
    var qty = inputEl.val();
    if (jQuery(this).parent().hasClass("minus_btn"))
        qty++;
    else
        qty--;
    if (qty < 0)
        qty = 0;
    inputEl.val(qty);

    var quantity = qty,
        id = inputEl.data("id");
    if (quantity != 0) {
        var line = $(this).closest("ul").index();
        Shopify.changeItemByLine(line, quantity, function(cart) {
            Shopify.updateQuickCart(cart);
            window.loyaltylion && window.loyaltylion.ui.refresh();
        });
    } else {
        Shopify.removeItem(id, function(cart) {
            Shopify.updateQuickCart(cart);
            window.loyaltylion && window.loyaltylion.ui.refresh();
        });
    }
})


jQuery(".cart_menu").on("click", ".plus_btn", function() {
    var inputEl = jQuery(this).parent().find("input");
    var qty = inputEl.val();

    if (jQuery(this).hasClass("plus_btn"))
        qty++;
    else
        qty--;
    if (qty < 0)
        qty = 0;


    var quantity = qty,
        id = inputEl.data("id");


    var line = $(this).closest("ul").index();
    Shopify.changeItemByLine(line, quantity, function(cart) {
        var updatedItem = cart.items.filter(function(item) {
            return item.variant_id == id
        });

        var totalUpdatedItemQty = 0;

        for (var j = 0; j < updatedItem.length; j++) {
            totalUpdatedItemQty += updatedItem[j].quantity;
        }

        if (updatedItem.length && totalUpdatedItemQty >= quantity) {
            Shopify.updateQuickCart(cart);
            window.loyaltylion && window.loyaltylion.ui.refresh();
            inputEl.val(quantity);
        } else {
            jQuery('.ajaxcart__item__' + id + '__errors').show().delay(2000).fadeOut();
        }
    });

})


jQuery(".cart_menu").on("keyup", ".number_val_input", function(event) {
    this.value = this.value.replace(/[^0-9]/g, '');
});

jQuery(".cart_menu").on("change", ".number_val_input", function(event) {
    var inputEl = $(this);
    var qty = inputEl.val();

    var quantity = qty.replace(/[^0-9]/g, ''),
        id = inputEl.data("id");

    if (quantity > 0) {
        var line = $(this).closest("ul").index();
        Shopify.changeItemByLine(line, quantity, function(cart) {
            var updatedItem = cart.items.filter(function(item) {
                return item.variant_id == id
            });

            var totalUpdatedItemQty = 0;

            for (var j = 0; j < updatedItem.length; j++) {
                totalUpdatedItemQty += updatedItem[j].quantity;
            }

            if (updatedItem.length && totalUpdatedItemQty >= quantity) {
                Shopify.updateQuickCart(cart);
                window.loyaltylion && window.loyaltylion.ui.refresh();
                inputEl.val(quantity);
            } else {
                jQuery('.ajaxcart__item__' + id + '__errors').show().delay(2000).fadeOut();
            }
        });
    } else {
        Shopify.removeItem(id, function(cart) {
            Shopify.updateQuickCart(cart);
            window.loyaltylion && window.loyaltylion.ui.refresh();
        });
    }


});


//End Wrapper    
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM