I’m customizing a Woocommerce WordPress site.

In the Woocommerce product class (class-wc-product.php) the get_price function applies a filter as follows:

function get_price() {
    return apply_filters('woocommerce_get_price', $this->price, $this);
}

In my functions.php I want to add a filter as follows:

add_filter('woocommerce_get_price', 'custom_price');

function custom_price($price, $product) {
    ...
}

When I call this I get the following PHP warning:

Warning: Missing argument 2 for custom_price()

Why is the second argument missing? Is $this not sent to the filter call?

2 Answers
2

It’s missing the second argument because you didn’t tell WordPress you wanted it with your add_filter call. By default, actions and filters only get one argument. Try this:

<?php
add_filter('woocommerce_get_price', 'custom_price', 10, 2);

function custom_price($price, $product) {
    ...
}

Leave a Reply

Your email address will not be published. Required fields are marked *