简体   繁体   English

如何调试 WooCommerce 中的购物车内容?

[英]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?简单的问题:我在哪里添加这个 php 代码?

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.最好它会打开一个新的小型浏览器 window 或小工具来显示“原始”数据。

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.然后,您需要在活动(子)主题的functions.php文件中插入自定义代码。

If it's a staging/debug site you can use the woocommerce_before_cart hook to print the contents of the variables.如果它是一个登台/调试站点,您可以使用woocommerce_before_cart挂钩来打印变量的内容。 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相关答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM