Home » WooCommerce: See What’s Inside the Cart Array (print_r)

WooCommerce: See What’s Inside the Cart Array (print_r)

by Tutor Aspire

So many times I needed to “play” with Cart contents in order to modify the default behaviour (such as removing a columns, hide the sale price to prices, hide an item, etc.).

There is a great PHP function that always helps – it’s called print_r and you can use this to “see” what’s inside the cart array, so that you can return certain information in a message for example.

A bit advanced, but as you grow your WooCommerce coding skills, you will use this a lot 🙂

WooCommerce: print Cart array

PHP Snippet: Print WooCommerce Cart Array

/**
 * @snippet       See what is inside the Cart array - WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @sourcecode    https://tutoraspire.com/?p=21941
 * @author        Tutor Aspire
 * @compatible    WC 2.6.14, WP 4.7.2, PHP 5.5.9
 */

add_action( 'woocommerce_before_cart', 'tutoraspire_print_cart_array' );

function tutoraspire_print_cart_array() {
$cart = WC()->cart->get_cart();
print_r($cart);
}

You may also like