php – Magento 2 – 在ajax更改数量后重新加载总计

前端之家收集整理的这篇文章主要介绍了php – Magento 2 – 在ajax更改数量后重新加载总计前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我想使用ajax来更改购物车magento 2购物车页面上的一个商品的数量.

添加了这个javascript

@H_502_7@$('.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的值是

@H_502_7@

当我更改数量时,总组件内容不刷新..

任何人都有想法在更换HTML代码后动态更新总组件?

最佳答案
ajax更改数量后重新加载总计

>一步

在你的自定义中创建它们(Magento_Theme / layout / checkout_cart_index.xml)

@H_502_7@

2.Step

creat js.phtml文件(Magento_Theme / templates / js.phtml)

@H_502_7@

3.步骤
主题Web文件夹中创建custom.js文件(Namespace / Yourtheme / web / js / custom.js)

@H_502_7@ define([ 'jquery','Magento_Checkout/js/action/get-totals','Magento_Customer/js/customer-data' ],function ($,getTotalsAction,customerData) { $(document).ready(function(){ $(document).on('change','input[name$="[qty]"]',function(){ var form = $('form#form-validate'); $.ajax({ url: form.attr('action'),data: form.serialize(),showLoader: true,success: function (res) { var parsedResponse = $.parseHTML(res); var result = $(parsedResponse).find("#form-validate"); var sections = ['cart']; $("#form-validate").replaceWith(result); // The mini cart reloading customerData.reload(sections,true); // The totals summary block reloading var deferred = $.Deferred(); getTotalsAction([],deferred); },error: function (xhr,status,error) { var err = eval("(" + xhr.responseText + ")"); console.log(err.Message); } }); }); }); });

4.Step(映射你的js文件)

主题根目录上创建requirejs-config.js(Namespace / yourtheme / requirejs-config.js)

@H_502_7@var config = { map: { '*': { custom:'js/custom' } } };

现在使用ajax进行qty更新工作
如有任何问题请在评论中提问.

猜你在找的jQuery相关文章