woocommerce hook delete product from the cart

2019-09-08 17:46发布

问题:

what are you doing for it? i wrote this string in the end of file (wp-content/plugins/woocommerce/woocommerce.php):

add_filter('woocommerce_before_cart_item_quantity_zero', 'wordpress_before_cart_item_quantity_zero', 10, 1);

function wordpress_before_cart_item_quantity_zero($item) {
    global $wpdb;
    global $woocommerce;

    $cart = $woocommerce->cart;

    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $id_product = $cart_item['product_id'];
        $wpdb->prepare("DELETE FROM wp_block_product WHERE product_id = %d", $id_product);
        $preparing_query = $wpdb->query($preparing_query);
    }

    var_dump($wpdb);
    exit;
}

but when i delete item from the cart it's not working!

i was try do so:

add_action('woocommerce_before_cart_item_quantity_zero', 'wordpress_before_cart_item_quantity_zero');

but it's not working too

回答1:

According to the source, you're looking for 'woocommerce_cart_item_removed', which runs when an item is removed from the cart:

function so31115243_after_remove_product($cart_item_key) {
    // Your custom function
}
add_action( 'woocommerce_cart_item_removed', 'so31115243_after_remove_product' );


回答2:

There is also a hook that runs before removing the item, which is woocommerce_remove_cart_item.

I believe that is what Danya is looking for in the comment above in case anyone runs into a similar issue.



回答3:

@rnevius solution works but need to change the priority of the action. For me it was 21 but i think it can change regarding plugins using this.

add_action( 'woocommerce_cart_item_removed', 'so31115243_after_remove_product', 21 );