Javascript Ajax购物车更新停止工作

Javascript Ajax购物车更新停止工作,javascript,php,ajax,wordpress,woocommerce,Javascript,Php,Ajax,Wordpress,Woocommerce,我发现了一个功能,可以在商品数量发生变化时自动更新购物车,它一直在工作,直到WooCommerce的3.2.0更新(最新更新3.2.1)。我很确定这段代码中发生了一些变化: add_action('woocommerce_cart_updated', 'wac_update'); function wac_update() { // is_wac_ajax: flag defined on wooajaxcart.js if ( !empty($_POST['is_wac_aj

我发现了一个功能,可以在商品数量发生变化时自动更新购物车,它一直在工作,直到WooCommerce的3.2.0更新(最新更新3.2.1)。我很确定这段代码中发生了一些变化:

add_action('woocommerce_cart_updated', 'wac_update');
function wac_update() {
    // is_wac_ajax: flag defined on wooajaxcart.js

    if ( !empty($_POST['is_wac_ajax'])) {
        $resp = array();
        $resp['update_label'] = __( 'Update Cart', 'woocommerce' );
        $resp['price'] = 0;

        // render the cart totals (cart-totals.php)
        ob_start();
        do_action( 'woocommerce_after_cart_table' );
        do_action( 'woocommerce_cart_collaterals' );
        do_action( 'woocommerce_after_cart' );
        $resp['html'] = ob_get_clean();

        // calculate the item price
        if ( !empty($_POST['cart_item_key']) ) {
            $items = WC()->cart->get_cart();
            $cart_item_key = $_POST['cart_item_key'];

            if ( array_key_exists($cart_item_key, $items)) {
                $cart_item = $items[$cart_item_key];
                $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
                $price = apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
                $resp['price'] = $price;
            }
        }

        echo json_encode($resp);
        exit;
    }
}
我的Javascript仍在运行,但此处仅供参考:

function refreshCart() {
    jQuery('.cart-builder .qty').on('change', function(){
        var form = jQuery(this).closest('form');

        // emulates button Update cart click
        jQuery("<input type='hidden' name='update_cart' id='update_cart' value='1'>").appendTo(form);

        // plugin flag
        jQuery("<input type='hidden' name='is_wac_ajax' id='is_wac_ajax' value='1'>").appendTo(form);

        var el_qty = jQuery(this);
        var matches = jQuery(this).attr('name').match(/cart\[(\w+)\]/);
        var cart_item_key = matches[1];
        form.append( jQuery("<input type='hidden' name='cart_item_key' id='cart_item_key'>").val(cart_item_key) );

        // get the form data before disable button...
        var formData = form.serialize();

        jQuery("input[name='update_cart']").val('Updating...').prop('disabled', true);

        jQuery.post( form.attr('action'), formData, function(resp) {
                // ajax response
                jQuery('.cart-collaterals').html(resp.html);

                el_qty.closest('.cart_item').find('.product-subtotal').html(resp.price);

                console.log(resp.test);

                jQuery('#update_cart').remove();
                jQuery('#is_wac_ajax').remove();
                jQuery('#cart_item_key').remove();

                jQuery("input[name='update_cart']").val(resp.update_label).prop('disabled', false);
            },
            'json'
        );
    });
}
函数refreshCart(){
函数()上的jQuery('.cart builder.qty'){
var form=jQuery(this).closest('form');
//模拟按钮更新购物车单击
jQuery(“”)附录(表格);
//插件标志
jQuery(“”)附录(表格);
var el_qty=jQuery(此);
var matches=jQuery(this).attr('name').match(/cart\[(\w+)\]/);
var cart_item_key=匹配[1];
表单.append(jQuery(“”.val(购物车项目键));
//在禁用按钮之前获取表单数据。。。
var formData=form.serialize();
jQuery(“输入[name='update\u cart']”).val('updateing…').prop('disabled',true);
jQuery.post(form.attr('action')、formData、function(resp){
//ajax响应
jQuery('.cart collaterals').html(resp.html);
el_数量最接近('.cart_item').find('.product subtotal').html(对应价格);
控制台日志(分别测试);
jQuery(“#更新购物车”).remove();
jQuery(“#is_wac_ajax”).remove();
jQuery('#购物车\商品\密钥').remove();
jQuery(“输入[name='update\u cart']”).val(分别更新标签).prop('disabled',false);
},
“json”
);
});
}

我一直在查看更改日志,但现在找不到冲突的内容。正如我所说,它在更新之前工作得非常好。

好的,这里有一个更简单的解决方案,我只是在购物车页面的底部添加一个脚本,但您也可以使用
wp\u enqueue\u script
函数将其排队,这是最好的方法。它所做的一切都是模拟按下更新购物车按钮

function cart_update_qty_script() {
    if (is_cart()) :
        ?>
        <script type="text/javascript">
            (function($){
                $(function(){
                    $('div.woocommerce').on( 'change', '.qty', function(){
                        $("[name='update_cart']").trigger('click');
                    });
                });
            })(jQuery);
        </script>
        <?php
    endif;
}

add_action( 'wp_footer', 'cart_update_qty_script' );
功能车更新数量脚本(){
如果(is_cart()):
?>
(函数($){
$(函数(){
$('div.com')。在('change','qty',function()上{
$(“[name='update_cart']”)。触发器('click');
});
});
})(jQuery);

您是否在控制台中看到任何javascript错误?没有错误,我将每个响应添加到控制台日志中,并将其所有内容都添加到工作属性中。似乎价格没有更新。这在购物车页面上是否正确?我现在正在查看它…这对于onChange函数是正确的,我必须将选择器从“.cart builder.qty”更改为到“.woocommerce cart form.qty”。它触发javascript,但它更新总额时使用的价格是一个项目的价格,而不是乘以的价格。全局get_cart()当我修改购物车上的数量时,函数仅返回购物车项目的数量1,这就是问题所在。我不想使用此解决方案,因为使用的购物车模板不在实际购物车页面上,而是一个小部件。因此,这样做将触发刷新,而不是ajax。