【问题标题】:Remove all cart items from a button on a custom page using AJAX使用 AJAX 从自定义页面上的按钮中删除所有购物车项目
【发布时间】:2018-11-01 18:11:04
【问题描述】:

我正在构建一个批量订单表单插件(用于 wordpress/woocommerce)——所有添加到购物车的功能都运行良好。我正在努力创建一个“取消订单”按钮,按下该按钮会清除所有项目行(此位有效)并从购物车中删除所有项目。

我正在尝试使用 AJAX/js、php 和标准 HTML 的组合..:

我的按钮..:

<button class="btn btn-danger btn-lg" id="cancelorder">Cancel Order</button>

我的购物车清空功能..:

add_action( 'init', 'woocommerce_clear_cart_url' );

function woocommerce_clear_cart_url() {
    global $woocommerce;
    if ( isset( $_GET['empty-cart'] ) ) {
        $woocommerce->cart->empty_cart();
    }
}

最后,我的 js 函数/ajax 调用..:

$("#cancelorder").click(function(){        
    if(confirm('Are you sure you want to clear all rows?')){
        $(".addedrow").remove(); //removes line items - not related to issue
        $.ajax({
        type: "POST",
        url: '/wp-admin/admin-ajax.php?action=woocommerce_clear_cart_url',
        data: {action : 'woocommerce_clear_cart_url'},
        success: function (res) {
            if (res) {
                alert('Removed Successfully');
                }
            }
        });
    } else {
        //back out with no action
    }
});

行已从表单中删除,但商品仍保留在购物车中。

【问题讨论】:

    标签: javascript php ajax wordpress woocommerce


    【解决方案1】:

    更新:我可以通过将上面的现有代码修改为以下内容来完成此工作..:

    Cart-Empty 功能..:

    add_action('wp_ajax_wc_woocommerce_clear_cart_url', 'wc_woocommerce_clear_cart_url');
    add_action('wp_ajax_nopriv_wc_woocommerce_clear_cart_url', 'wc_woocommerce_clear_cart_url'); 
    //added wc_ prefix in case of function name conflict
    
    function wc_woocommerce_clear_cart_url() {
    global $woocommerce;
    $returned = ['status'=>'error','msg'=>'Your order could not be emptied'];
    $woocommerce->cart->empty_cart();
    if ( $woocommerce->cart->get_cart_contents_count() == 0 ) {    
        $returned = ['status'=>'success','msg'=>'Your order has been reset!'];       
    }
    die(json_encode($returned));
    }
    

    还有 js/ajax 方面..:

    $("#cancelorder").on('click',function(){        
        if(confirm('Are you sure you want to clear all rows?')){
            $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/wp-admin/admin-ajax.php?action=wc_woocommerce_clear_cart_url',
            data: {action : 'wc_woocommerce_clear_cart_url'},
            success: function (data) {
                    if (data.status != 'success') {
                        alert(data.msg);
                    } else {
                        $('#itemrows').html('');
                        addrows();
                    }
                }   
            });
        } else {
            //back out with no action
        }
    });
    

    【讨论】:

      【解决方案2】:

      嗯,看起来你正在发送一个 POST 请求

      type: "POST"
      

      并尝试在控制器上恢复 GET 参数

      if ( isset( $_GET['empty-cart'] ) )
      

      另外,您要查找的密钥 ('empty-cart') 似乎根本不存在... 至少在您提供的这部分代码中......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-02
        • 2021-05-21
        • 2020-09-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多