How to debug in WooCommerce 3+



PHP Snippet 1:

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

PHP Snippet 2:

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

PHP Snippet 3:

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

PHP Snippet 4:

error_log( print_r( $variable, true ) );

PHP Snippet 5:

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

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

PHP Snippet 6:

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

PHP Snippet 7:

  Debug Objects: Calculating shipping