Skip to content
Advertisement

How to check the value of a WooCommerce filter hook parameter?

In WooCommerce you have many hooks & filters. How can I check what is inside of a filter parameter? For example, I want to unset some shipping rates based on certain conditions. How can I check what is inside my $rates or $package parameter?

add_filter('woocommerce_package_rates', 'wbgoe_shipping_rates', 20, 2);

function wbgoe_shipping_rates($rates, $package) {
    print_r($rates); \ How to check what's inside of $rates??
    return $rates;
}

Advertisement

Answer

You can first enable WordPress debug log by editing 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 );

Now you can use error_log() function in your code as follows:

add_filter('woocommerce_package_rates', 'check_shipping_rates', 20, 2);

function check_shipping_rates($rates, $package) {
    error_log( print_r( $rates, true ) );
    return $rates;
}

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

Related: How to debug in WooCommerce 3

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement