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.
Answer
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
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?