user689881
user689881

Reputation: 67

How to debug cart content in WooCommerce?

Found the following code in another thread:

<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
    foreach($items as $item => $values) { 
        $_product =  wc_get_product( $values['data']->get_id()); 
        echo "<b>".$_product->get_title().'</b>  <br> Quantity: '.$values['quantity'].'<br>'; 
        $price = get_post_meta($values['product_id'] , '_price', true);
        echo "  Price: ".$price."<br>";
    } 
?>

I'm a rookie when it comes to adding code.

Simple question: Where do I add this php code?

I have a test site, and would like to see the cart data (in order to try add some other customized code).

Will it simply print onto the page? Will it replacing the standard cart view? Can I view both? Optimally it would open a new small browser window or gadget to show the "raw" data.

Upvotes: 0

Views: 563

Answers (1)

Vincenzo Di Gaetano
Vincenzo Di Gaetano

Reputation: 4110

To add custom code to your site you first need to create a child theme. Then, you will need to insert the custom code inside the functions.php file of your active (child) theme.

If it's a staging/debug site you can use the woocommerce_before_cart hook to print the contents of the variables. Another check that you could add is to check if the current user is an administrator, so as not to see the data to other users of the site.

So it will be something like this:

add_action( 'woocommerce_before_cart', 'wc_cart_debug' );
function wc_cart_debug( $cart ) {

    if ( ! current_user_can('administrator') ) {
        return;
    }

    // Your code here.

}

RELATED ANSWERS

Upvotes: 2

Related Questions