【问题标题】:After adding a custom button, how to update the cart total onclick?添加自定义按钮后,如何更新购物车总数?
【发布时间】:2020-05-12 07:44:49
【问题描述】:

想象一下,我在购物车页表上添加了一个自定义按钮。每个产品下方都有一个按钮(产品表中列出的购物车)。

Onclick 按钮需要在 ajax 上更新购物车总数。我尝试了以下功能。

添加了这个钩子“wp_footer”。您可以在input.qty(change keyup mouseup) 上看到它运行良好。但是点击a.btn.plus 不起作用。

console.log(jQuery('input.qty').val());正在打印数量,但触发功能不起作用。

如果我在 onclick 之外添加触发功能,它可以工作吗?

jQuery(document).on('click', 'a.btn.plus', function () {
    if (timeout != undefined) clearTimeout(timeout); 

    if (jQuery('input.qty').val() == '') return;

    timeout = setTimeout(function () {

        //jQuery('[name="update_cart"]').trigger('click');

        console.log(jQuery('input.qty').val());

        jQuery('button.primary.mt-0.pull-left.small').trigger('click');

    }, 1000);

});

jQuery('div.woocommerce').on('change keyup mouseup', 'input.qty', function () {

    if (timeout != undefined) clearTimeout(timeout); 

    if (jQuery(this).val() == '') return; 

    timeout = setTimeout(function () {

        jQuery('[name="update_cart"]').trigger('click');

    }, 1000);

});

【问题讨论】:

    标签: jquery wordpress woocommerce


    【解决方案1】:
       `<script type="text/javascript">
        var timeout;
        jQuery('div.woocommerce').on('click', 'a.btn.plus', function () {
           if (timeout != undefined) clearTimeout(timeout);
            timeout = setTimeout(function () {
           jQuery(jQuery('body').find('[name="update_cart"]')).prop('disabled',false);
           jQuery(jQuery('body').find('[name="update_cart"]')).trigger('click');
           }, 1000);
        });
        </script>`
    

    【讨论】:

    • 而不是找到 name="update_cart" 首先选择正文然后找到名称工作......
    猜你喜欢
    • 2021-04-21
    • 2019-01-24
    • 1970-01-01
    • 2018-05-08
    • 2021-02-03
    • 2016-09-05
    • 2021-03-10
    • 2015-12-04
    相关资源
    最近更新 更多