Viewed   87 times

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.

 Answers

3

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:

  • WC_Logger methods have been updated since WooCommerce 3: So logging can be grouped by context and severity.

  • Use WC_Logger log() method instead of add() method due to upcoming deprecation (thanks to @Vizz85).

For example:

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

Related:

  • Develop WooCommerce blog (january 2017): Improved logging in WooCommerce 3
  • Documentation on the WC_Logger available methods

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.

Sunday, November 20, 2022
3

UPDATED
As you are finding hard to locate functions.php file you can created a plugin. Create a PHP file as wh-thankyou-tracking.php under /wp-content/plugins/ and copy paste the below code to it and save it. And form admin panel Activate WH Order Tracking JS plugin

<?php
/**
 * Plugin Name: WH Order Tracking JS
 * Version: 0.1
 * Description: This plugin will add a JS tracking code to WooCommerce Thankyou page.
 * Author: Raunak Gupta
 * Author URI: https://www.webhat.in/
 * Text Domain: wh
 */
if (!defined('ABSPATH'))
{
    exit;
} // Exit if accessed directly

if (!class_exists('WooCommerce'))
{
    exit;
}// Exit if WooCommerce is not active

function wh_CustomReadOrder($order_id)
{
    //getting order object
    $order = wc_get_order($order_id);

    $items = $order->get_items();
    $product_js = [];

    foreach ($items as $item_id => $item_data)
    {
        //getting product object
        $_product = wc_get_product($item_data['item_meta']['_product_id'][0]);

        //getting all the product category
        $pro_cat_array = wp_get_post_terms($_product->ID, 'product_cat');

        $sku = $sku = $_product->get_sku();
        $qty = $item_data['item_meta']['_qty'][0];
        $pro_cat = implode(',', $pro_cat_array);
        $pro_brand = $_product->get_attribute('pa_brand'); //replace it with your brand attribute slug
        $pro_price = $item_data['item_meta']['_line_total'][0];

        //storing all the line item as a string form
        $product_js[] = '{id: "' . $sku . '",category:"' . $pro_cat . '",brand:"' . $pro_brand . '",price: "' . $pro_price . '"quantity:"' . $qty . '"}';
    }

    ?>
    <script type="text/javascript">
        order.purchase = {
            currency: 'EUR',
            transactionId: '<?= $order->id ?>',
            products: [<?= implode(',', $product_js) ?>]
        };
    </script>
    <?php
}

add_action('woocommerce_thankyou', 'wh_CustomReadOrder');

Hope this helps!

Friday, November 18, 2022
2

Hooked functions will never override other hooked functions that are using the same action or filter hook.

They are added to a kind of "hook queue" with an execution order based on priority rules:

  • If a priority is specified, they will be ordered in the queue first by hook priority and by declaration priority.
  • If there is no priority specified, they take the default priority of 10 and they will be ordered in the queue by declaration.

So you can have many hooked functions on the same hook, like for example in the Woocommerce template file content-single-product.php

Illustrated example:

In the below commented code example, you can see the execution order in the hook queue for each hooked function for the woocommerce_thankyou action hook:

// No defined priority (default priority is 10)
add_action( 'woocommerce_thankyou', 'first_custom_function_no_priority' );
function first_custom_function_no_priority( $order_id ) {
    // ==> Triggered in third position ==> [3]
}

## Default Hook "woocommerce_order_details_table" (default priority is 10)
    // ==> Triggered in second position ==> [2]

// Defined priority is 10
add_action( 'woocommerce_thankyou', 'order_created_get_skus', 10 );
function order_created_get_skus( $order_id ) {
    // ==> Triggered in Fourth position ==> [4] 
}

// Defined priority is 5
add_action( 'woocommerce_thankyou', 'third_custom_function', 5 );
function third_custom_function( $order_id ) {
    // ==> Triggered in first position ==> [1]
}

// Defined priority is 20
add_action( 'woocommerce_thankyou', 'fourth_custom_function', 20 );
function fourth_custom_function( $order_id ) {
    // ==> Triggered at last (sixth) ==> [6]
}

// No defined priority (default priority is 10)
add_action( 'woocommerce_thankyou', 'last_custom_function_no_priority' );
function last_custom_function_no_priority( $order_id ) {
    // ==> Triggered in fifth position ==> [5]
}

Lower priority is executed (or triggered) before, higher priority is executed (or triggered) after. If no priority is specified, the default priority is 10.

The hooked functions can only be removed with remove_action() or remove_filter() with a mandatory defined priority.

To see how many hooked functions are hooked on a specific hook with all necessary details, you can use the following that will give you a raw output:

global $wp_filter;

// HERE below you define the targeted hook name
$hook_name = 'woocommerce_widget_shopping_cart_buttons';

if( isset($wp_filter[$hook_name]) ) {
    echo '<pre>';
    print_r($wp_filter[$hook_name]);
    echo '</pre>';
} else {
    echo '<p>Hook "'.$hook_name.'" is not used yet!</p>';
}

There is 2 kind of hooks, as you have noticed may be, which are filter hooks and action hooks.

  1. Action hook:

    • Action hook execution point (trigger): with do_action()
    • Attaching a function to an action hook (triggered): with add_action(): the function is executed and can have optional arguments.
  2. Filter hook:

    • Filter hook execution point (trigger): with apply_filters()
    • Attaching a function to a filter hook (filtering / triggered): with add_filter(): a mandatory argument (a variable) is filtered and returned from the "hooked" function

Hooks and their hooked functions can be located anywhere like in the function.php file of your active child theme (or active theme) and also in any plugins php files.


Related:

  • WooCommerce action hooks and overriding templates
  • How to add custom hooks to a custom plugin for Woocommerce
  • WordPress: Difference Between Filter and Action Hooks?
Tuesday, August 16, 2022
 
2

Try instead this revisited similar code using template_redirect action hook:

add_action( 'template_redirect', 'auto_add_product_to_cart' );
function auto_add_product_to_cart() {
    if ( is_admin() ) return;

    // Product Id of the free product which will get added to cart;
    $product_id = 99999;

    if ( WC()->cart->is_empty() ) 
    {
        // No products in cart, we add it
        WC()->cart->add_to_cart( $product_id ); 
    } 
    else
    {
        $found  = false;

        //check if product already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['data']->get_id() == $product_id )
                $found = true;
        }

        // if the product is not in cart, we add it
        if ( ! $found )
            WC()->cart->add_to_cart( $product_id ); 
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Note: The init hook is not correct here and not to be used for this…

Friday, August 12, 2022
 
5

I'm selling a plugin that would enable to use Custom Post Type as "product" of WooCommerce. I did a lot of work on that.

But this is the most important part.
You have to create your own data store, like this:

first create a class that extends to WC_Product_Data_Store_CPT. The idea is to overwrite the existing function of this class that check the post type. I found read and get_product_type that does the checking.

class WCCPT_Product_Data_Store_CPT extends WC_Product_Data_Store_CPT {

    /**
     * Method to read a product from the database.
     * @param WC_Product
     */

    public function read( &$product ) {

        $product->set_defaults();

        if ( ! $product->get_id() || ! ( $post_object = get_post( $product->get_id() ) ) || ! in_array( $post_object->post_type, array( 'birds', 'product' ) ) ) { // change birds with your post type
            throw new Exception( __( 'Invalid product.', 'woocommerce' ) );
        }

        $id = $product->get_id();

        $product->set_props( array(
            'name'              => $post_object->post_title,
            'slug'              => $post_object->post_name,
            'date_created'      => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null,
            'date_modified'     => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null,
            'status'            => $post_object->post_status,
            'description'       => $post_object->post_content,
            'short_description' => $post_object->post_excerpt,
            'parent_id'         => $post_object->post_parent,
            'menu_order'        => $post_object->menu_order,
            'reviews_allowed'   => 'open' === $post_object->comment_status,
        ) );

        $this->read_attributes( $product );
        $this->read_downloads( $product );
        $this->read_visibility( $product );
        $this->read_product_data( $product );
        $this->read_extra_data( $product );
        $product->set_object_read( true );
    }

    /**
     * Get the product type based on product ID.
     *
     * @since 3.0.0
     * @param int $product_id
     * @return bool|string
     */
    public function get_product_type( $product_id ) {
        $post_type = get_post_type( $product_id );
        if ( 'product_variation' === $post_type ) {
            return 'variation';
        } elseif ( in_array( $post_type, array( 'birds', 'product' ) ) ) { // change birds with your post type
            $terms = get_the_terms( $product_id, 'product_type' );
            return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
        } else {
            return false;
        }
    }
}

after that, add a filter to woocommerce_data_stores and use your class.

add_filter( 'woocommerce_data_stores', 'woocommerce_data_stores' );

function woocommerce_data_stores ( $stores ) {      
    $stores['product'] = 'WCCPT_Product_Data_Store_CPT';
    return $stores;
}

with that, you'll be able to add a post type of birds to cart. But will not actually be a success add to cart. Because there's no price, cart will reject it.

To solve that, you need another filter. Below is a simple way of adding the price.

add_filter('woocommerce_product_get_price', 'woocommerce_product_get_price', 10, 2 );
function woocommerce_product_get_price( $price, $product ) {

    if ($product->get_id() == 815 ) {
        $price = 10;        
    }
    return $price;
}

Once that's done, you'll have success adding to cart.

Monday, August 15, 2022
 
piotrp
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share