Danya Andreew
Danya Andreew

Reputation: 1

woocommerce hook delete product from the cart

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

Upvotes: 0

Views: 8192

Answers (4)

Turbo Future
Turbo Future

Reputation: 1

function so_27030769_maybe_empty_cart() {

global $woocommerce;
$woocommerce->cart->empty_cart();}
add_filter( 'woocommerce_add_to_cart_validation_custom', 'so_27030769_maybe_empty_cart', 10, 3 );
// Usage
apply_filters('woocommerce_add_to_cart_validation_custom','','');

Upvotes: 0

Jérome Obbiet
Jérome Obbiet

Reputation: 131

@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 );

Upvotes: 0

Matt
Matt

Reputation: 91

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.

Upvotes: 0

rnevius
rnevius

Reputation: 27092

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' );

Upvotes: 4

Related Questions