0

I want to add a new WooCommerce notice when a user removes an item from their cart. I'm using the woocommerce_remove_cart_item hook. However, I can't access the $cart_item array using var_dump. I have tried to echo a variable $prod_id = $cart_item ['data']->get_id(); but echo doesn't display anything either?

If I add a WooCommerce notice then the variable ($prod_id) does display in the notice correctly.


    $remove_notice = __( 'ID - ' . $prod_id );

    wc_add_notice( $remove_notice, 'success' );

But I just can't seem to echo, var_dump, or print_r anything on the page using the woocommerce_remove_cart_item hook. I want to make the custom display message conditional but can't see inside the $cart_item array to see what's going on. Here's my code.


function verify_cart( $cart_item_key, $cart ) {

    $cart_item = $cart->get_cart_item( $cart_item_key );
    $product   = wc_get_product( $cart_item['data'] );
    $prod_id   = $cart_item ['data']->get_id();

    // $remove_notice = __( 'ID - ' . $prod_id );
    // wc_add_notice( $remove_notice, 'success' );

    var_dump( $cart_item );

}
add_action( 'woocommerce_remove_cart_item', 'verify_cart', 10, 2 );

Stoto
  • 41
  • 1
  • 8
  • Have you tried error_log instead of var_dump ? I user error_log(print_r(var, true)). Depending on when./how the hook is called trying to display to the screen may not work. – John3136 Apr 23 '22 at 01:56
  • @John3136 Thanks for the info. No I haven't tried error_log but will give it a go. How do you access the data once it is logged? Sorry for the noob questions, I'm new to PHP and trying to learn it. – Stoto Apr 23 '22 at 02:11
  • Ok I have found the data in Root/Logs/mysite_com.php.error.log, though its a big file and a little messy. I also found some info to have the data emailed by using `error_log( print_r( $var, true ), 1, 'name@email.com' );` Maybe not the ideal solution but better than nothing. – Stoto Apr 23 '22 at 04:07

0 Answers0