Echo console.log未产生任何输出

问题描述 投票:1回答:2

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

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);
  }

最后,我有一种相当复杂的方法来计算“成本”,但我无法调试它,因为该回显线在chrome控制台中不产生任何输出。有什么想法吗?

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

php wordpress debugging woocommerce
2个回答
1
投票

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

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

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

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

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

error_log( print_r( $variable, true ) );

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


0
投票

第一个Creat PHP帮助程序功能

 function console_output($data) {
 $output = $data;
if (is_array($output))
    $output = implode(',', $output);

  echo "<script>console.log('Debug Objects: " . $output . "' );</script>";
 }

然后您可以像这样使用它:

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'
     );
   console_output("Calculating shipping");
    $this->add_rate($rate);
   }

这将创建如下输出:

  Debug Objects: Calculating shipping
© www.soinside.com 2019 - 2024. All rights reserved.