【问题标题】:Remove product in the cart using ajax in woocommerce在woocommerce中使用ajax删除购物车中的产品
【发布时间】:2014-03-20 23:40:19
【问题描述】:

我想在不点击链接的情况下使用 ajax 删除 woocommerce 购物车中的产品。

如果您遇到过此类功能,请帮助我们。

add_action( 'wp_footer', 'add_js_to_wp_wcommerce');

function add_js_to_wp_wcommerce(){ ?>
    <script type="text/javascript">
    jQuery('.remove-product').click(function(){
        var product_id = jQuery(this).attr("data-product_id");
        jQuery.ajax({
            type: 'POST',
            dataType: 'json',
            url: "/wp-admin/admin-ajax.php",
            data: { action: "product_remove", 
                    product_id: product_id
            },success: function(data){
                console.log(data);
            }
        });
        return false;
    });
    </script>
<?php }

add_action( 'wp_ajax_product_remove', 'product_remove' );
add_action( 'wp_ajax_nopriv_product_remove', 'product_remove' );
function product_remove() {
    global $wpdb, $woocommerce;
    session_start();
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item){
        if($cart_item['product_id'] == $_POST['product_id'] ){
            // Remove product in the cart using  cart_item_key.
            $woocommerce->cart->get_remove_url($cart_item_key);
        }
    }
    print_r($woocommerce->cart->get_cart());
    //echo json_encode(array('status' => 0));
    exit();
}

【问题讨论】:

    标签: ajax wordpress woocommerce


    【解决方案1】:

    你可以使用 WC_Cart set_quantity 方法

    并在你的 php 中这样做:

    $cart = WC()->instance()->cart;
    $id = $_POST['product_id'];
    $cart_id = $cart->generate_cart_id($id);
    $cart_item_id = $cart->find_product_in_cart($cart_id);
    
    if($cart_item_id){
       $cart->set_quantity($cart_item_id,0);
    }
    

    【讨论】:

    • 请注意,此代码不适用于可变产品。在这种情况下,您必须将 $variation_id 添加到函数 generate_cart_id() 或传递 $cart_id(实际上它已经从 ajax 脚本中正确调用了 $cart_item_key
    • 我似乎无法让它适用于可变产品。正如文档所述,我已将我的变体 ID 作为参数传递给 generate_cart_id,但没有任何反应。我是不是忘记了什么?
    • 你能在这里明确一下这个函数的放置位置吗?
    • 您应该将该代码添加到 ajax 中调用的 php 中。我必须说,我不确定这仍然适用于最近版本的 woo commerce
    • 任何人都可以实现这个变量产品? stackoverflow.com/questions/39286294/…
    【解决方案2】:

    使用这个:

    $cart = $woocommerce->cart;
    
    foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item){
        if($cart_item['product_id'] == $_POST['product_id'] ){
            // Remove product in the cart using  cart_item_key.
            $cart->remove_cart_item($cart_item_key);
        }
    }
    

    【讨论】:

      【解决方案3】:

      试试这个:

      foreach ( $woocommerce->cart->cart_contents as $cart_item_key => $cart_item ) {
      
       if($cart_item['product_id'] == $product_id){
      
        unset($cartdetails->cart_contents[$cart_item_key]);
      
       }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-10
        • 1970-01-01
        • 1970-01-01
        • 2018-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-29
        • 2014-02-06
        相关资源
        最近更新 更多