0
votes

I have a piece of code below that works where it removes the 'continue shopping' button after adding a product to cart.

add_action( 'woocommerce_after_cart', 'verify_all_products_in_cart' );

    
function verify_all_products_in_cart() {
    
    $all_products_array  = wc_get_products( array( 'return' => 'ids', 'limit' => -1 ) );
    $products_in_cart_array = array();
    $visible = 'visible';
    
  // Loop over $cart items
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {    
    $products_in_cart_array[]=$cart_item['product_id'];
}
    
    if(count($all_products_array) === count($products_in_cart_array)){
        $visible = 'hidden';
    }
    
    echo '<style>a[name="hpy_cs_continue"]{ visibility:'. $visible .';}</style>';
  
}

However, I notice there are certain actions in the wecbsite I need this code to run. I would want the function above to run if an item is removed from cart and if the user clicks 'undo' from the woocommerce message.

I tried to add an action hook in the code above:

add_action( 'woocommerce_cart_item_removed', 'verify_all_products_in_cart' );

The issue is that it removes the whole contents fo the cart when I add this. So I don't know if this is the correct hook but basically the problem is how do I run the function verify_all_products_in_cart() code when an item is removed from cart or the user clicks undo in the woo commerce message? Currently the code only runs if I add a product to cart of if I refresh the page.

1

1 Answers

0
votes

Use 'woocommerce_remove_cart_item' instead of 'woocommerce_cart_item_removed' because this hook call after the item is removed, so there is no way to get any information about the product

function verify_all_products_in_cart( $cart_item_key, $cart ) {
    // your code goes here
};
add_action( 'woocommerce_remove_cart_item', 'verify_all_products_in_cart', 10, 2 );