WooCommerce以编程方式从购物车中删除

时间:2016-02-22 19:56:48

标签: ajax wordpress woocommerce

如何使用AJAX以编程方式从Woocommerce购物车中删除商品?我尝试在我的functions.php文件中放入一个函数并访问它,但没有任何内容被删除。我试过硬编码产品299,但它没有删除。这是我做的:

的functions.php

function remove_item_from_cart() {
    $cart = WC()->instance()->cart;
    $id = 299;
    $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);
    }
    return true;
}

主题/地雷/ main.js

$.ajax({
    type: 'POST',
    dataType: 'text',
    url: "http://www.../wp/wp-content/themes/mine/functions.php",
    data: {
        action: 'remove_item_from_cart'
    },
    success: function( data ) {
        console.log(data);
    }
});

3 个答案:

答案 0 :(得分:11)

使用适当的ajax wordpress这样的方法:这对我来说很好。

//的functions.php

    function remove_item_from_cart() {
    $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);
       return true;
    } 
    return false;
    }

    add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
    add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');

// main.js

    $.ajax({
        type: "POST",
        url: 'http://localhost/your_site/wp-admin/admin-ajax.php',
        data: {action : 'remove_item_from_cart','product_id' : '4'},
        success: function (res) {
            if (res) {
                alert('Removed Successfully');
            }
        }
    });

答案 1 :(得分:2)

这似乎可行。

HTML:

<?php
  foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){
    ?<
       <button class="remove-item" data-cart-item-key="<?=$cart_item_key;?>">
          remove item
       </button>
    <?
  }
?>

Javascript:

$('.remove-item').click(function(e){
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: ajaxurl,
        data: {
                action: 'remove_item_from_cart', 
               'cart_item_key': String($(this).data('cart-item-key'))
               }
    });
});

在functions.php中,模板文件夹内:

function remove_item_from_cart() {
    $cart_item_key = $_POST['cart_item_key'];
    if($cart_item_key){
       WC()->cart->remove_cart_item($cart_item_key);
       return true;
    } 
    return false;
}
add_action('wp_ajax_remove_item_from_cart', 'remove_item_from_cart');
add_action('wp_ajax_nopriv_remove_item_from_cart', 'remove_item_from_cart');

答案 2 :(得分:0)

对于WooCommerce 3.0+,你可以使用内置的remove_cart_item()函数来实现它

function findCartItemKey($cartItems, $productId){               
  foreach($cartItems as $cartKey => $item){
    $product = $item['data'];
    if($product->get_id() == $productId){
            return $cartKey;
    }       
    return false;
  }    
}

global $woocommerce;
$cartItemKey = findCartItemKey($woocommerce->cart->get_cart())
$woocommerce->cart->remove_cart_item($cartItemKey);