Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Maybe apply_filters('woocommerce_get_price', … ); is called somewhere else without the second argument? Did you search for such a case? –  toscho Nov 12 '12 at 15:10
    
It's called only once as far as I can see –  benedict_w Nov 12 '12 at 15:12
add comment

2 Answers

up vote 4 down vote accepted

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) {
    ...
}
share|improve this answer
    
Good answer Chris, exactly why I love this site. I learn something new every day. –  Stephen Nov 12 '12 at 16:10
add comment

The warning is a result of the second argument in your function custom_price(). The filter provides only one variable on the call and I can't remember if this is the product/post ID or the price. The code docs indicate that it returns a string so I'd presume it's the price.

You can hopefully find the documentation at http://wcdocs.woothemes.com/apidocs/index.html

If you need the product at this point you'll have to use:

function custom_price( $price ) {

    global $product;

}

If all else fails simply echo $price to see if you can tell if it's a product ID or a price. Then adjust your code accordingly.

share|improve this answer
    
Thanks for your insights @chrisguitarguy's anwser seems to have done the trick for me. –  benedict_w Nov 12 '12 at 15:52
    
Yeah, it's certainly a more elegant solution. –  Stephen Nov 12 '12 at 16:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.