2016-10-12 58 views
3

我想使用ajax更改購物車magento 2購物車頁面上的一個項目的數量。Magento 2 - 在ajax更改數量後重新加載總計購物車

我已經加入此javascript:

$('.cart.item .qty').on({ 
    change: function() { 
     var post_url = $(this).attr('data-post-url'); 

     $.post(post_url, $(this).serialize(), function(data) { 
      $(".form-cart").replaceWith(data.cart_html); 
      $("#cart-totals").replaceWith(data.totals_html); 
      $("#cart-totals").trigger('contentUpdated'); 
     }, "json"); 
    } 
}); 

data.totals_html的值是

<div id="cart-totals" class="cart-totals" data-bind="scope:'block-totals'"> 
<!-- ko template: getTemplate() --><!-- /ko --> 
<script type="text/x-magento-init"> 
     { 
      "#cart-totals": { 
       "Magento_Ui/js/core/app": {"components":{"block-totals":....} 
</script> 

當我更改數量,總的成分含量沒有刷新..

任何人都有一個想法,動態更新r之後的總成分放置html代碼?

回答

0

可以在車這種方式進行磋商,以AJAX重負載塊總計:

define([ 
    "jquery", 
    'Magento_Checkout/js/action/get-payment-information', 
    'Magento_Checkout/js/model/totals' 
], function($, getPaymentInformationAction, totals) { 
    $('.cart.item .qty').on({ 
     change: function() { 
      var post_url = $(this).attr('data-post-url'); 
      $.post(post_url, $(this).serialize(), function(data) { 
       //custom your update 
       $(".form-cart").replaceWith(data.cart_html); 
       $("#cart-totals").replaceWith(data.totals_html); 
       //reload block total 
       var deferred = $.Deferred(); 
       totals.isLoading(true); 
       getPaymentInformationAction(deferred); 
       $.when(deferred).done(function() { 
        totals.isLoading(false); 
       }); 
       //end 
      }, "json"); 
     } 
    }); 
});