如何在 WooCommerce 3 中进行调试 [英] How to debug in WooCommerce 3

查看:28
本文介绍了如何在 WooCommerce 3 中进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用本教程为 Woocommerce 创建自定义运输方式 https://docs.woocommerce.com/document/shipping-method-api/ 但我在调试时遇到问题.每当用户更新运输方式时,Woocommerce 调用计算运输.我已经用以下内容覆盖了这个功能.

公共函数calculate_shipping( $package ) {//这是您添加费率的地方$rate = 数组('想法' =>$this->id,'标签' =>$this->title,'成本' =>'90.00','calc_tax' =>'每件');echo "<script>console.log('计算运费');</script>";$this->add_rate($rate);}

最后,我有一种计算成本"的相当复杂的方法,但我无法调试它,因为该 echo 行在 chrome 控制台中没有产生任何输出.知道这里发生了什么吗?

任何帮助将不胜感激.谢谢.

解决方案

由于这是服务器端的后台进程,请勿使用 javascript.

1).WC 日志和 WC_Logger 在 WooCommerce 中进行更好的调试

要从仪表板轻松访问日志结果,您可以登录到 WC 记录器而不是错误日志.

您可以通过转到 WooCommerce > 来访问错误日志.系统状态 >日志.

然后您将能够选择并查看"您需要的错误日志文件,为您提供所需的调试详细信息.错误日志也位于站点安装中的/wc-logs 文件夹中.

对捕获的异常运行堆栈跟踪(示例):

//将任何异常记录到 WC 记录器$log = new WC_Logger();$log_entry = print_r( $e, true );$log_entry .= '异常跟踪:' .print_r( $e->getTraceAsString(), true );$log->log('new-woocommerce-log-name', $log_entry);

<块引用>

注意事项:

例如:

$logger = wc_get_logger();$logger->debug('debug message', array('source' => 'my-extension'));

相关:


2).使用 WordPress 调试 WP_DEBUG 日志 (作为替代)

a) 首先编辑您的 wp-config.php 文件,添加以下几行以启用调试(如果这些已经定义,请编辑值):

define( 'WP_DEBUG', true );定义('WP_DEBUG_LOG',真);定义('WP_DEBUG_DISPLAY',假);

当错误被记录下来时,它们应该出现在 wp-content/debug.log 中.您可以在文本编辑器中打开此文件.

b) 在您的代码上:使用以下内容(其中 $variable 是要显示在错误日志中的变量:

error_log( print_r( $variable, true ) );

现在您将获得用于调试的数据.

I am creating a custom shipping method for Woocommerce using this tutorial https://docs.woocommerce.com/document/shipping-method-api/ but I am having issues debugging. Whenever shipping methods get updated by user, Woocommerce calls calculate shipping. I have overridden this function with the following.

public function calculate_shipping( $package ) {
    // This is where you'll add your rates
    $rate = array(
      'idea' => $this->id,
      'label' => $this->title,
      'cost' => '90.00',
      'calc_tax' => 'per_item'
    );
    echo "<script>console.log('Calculating shipping');</script>";
    $this->add_rate($rate);
  }

In the end I have a fairly complex way of calculating the "cost" but I have no way of debugging it because that echo line produces no output in the chrome console. Any ideas what is going on here?

Any help would be much appreciated. Thank you.

解决方案

As this is a background process on server side, don't use javascript.

1). WC Logs and the WC_Logger Class in WooCommerce for better debugging

To access the results of the log easily from the dashboard, you can log to a WC logger rather than the error log.

You can access error logs by going to WooCommerce > System Status > Logs.

Then you will be able to choose and "view"the error log file you need, giving you the debugging details that you need. Error logs are also located in the /wc-logs folder within your site install.

Running a stack trace on a caught exception (example):

// Log any exceptions to a WC logger
$log = new WC_Logger();
$log_entry = print_r( $e, true );
$log_entry .= 'Exception Trace: ' . print_r( $e->getTraceAsString(), true );
$log->log( 'new-woocommerce-log-name', $log_entry );

Notes:

For example:

$logger = wc_get_logger();
$logger->debug( 'debug message', array( 'source' => 'my-extension' ) );

Related:


2). Debugging with WordPress WP_DEBUG Log (as an alternative)

a) First edit your wp-config.php file adding the following lines to enable debug (if these are already defined, edit the values):

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );

As errors are logged, they should appear in wp-content/debug.log. You can open this file in a text editor.

b) On your code: Use the following (where $variable is the variable to be displayed in the error log:

error_log( print_r( $variable, true ) );

Now you will get the data for debugging.

这篇关于如何在 WooCommerce 3 中进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆