0

I'm trying to look over the options to hook into a product's price to be able to change the regular price.

This is in my case for variable products where I wanna be able to in the end get price and other datas from a post connected to this variation where I have saved price and other details as that posts's meta data.

That would make it possible to have like a "General price" to retrieve on demand for the variation and use as price. But if the variation has it's own price set, that would be used instead.

But anyway, this coding needs to be done. And I have seen that I can hook into get_prop() in WooCommerce. It would be a start at least. But somehow that won't work for me.

I tried with a test:

function test_change($value, $prop) { return 666; } 
add_filter('woocommerce_product_get_price', 'test_change');

But as I said, it doesn't seems to work. Cause the $product['display_price'] doesn't show 666.

So then I just wanted to figure out how everything works. So I wanted to call the get_price() function/method that is the function that calls get_prop(). And what I can see that function (get_price) should be able to call from a product object (WC_Product). But it doesn't work. I have tried this code to test:

$prodfac = new WC_Product_Factory;
$prodf = $prodfac->get_product($var['product_id']);

That is basically (what I understand) what wc_get_product() does. And I have seen that wc_get_product() is used in other places in woocommerce plugin core code and then calls get_price(). So I don't understand why it won't work when I'm trying.

I have tried to look in woocommerce core code and I just can't see anywhere that get_price() would be blocked or something. It is a public function also so. I just can't understand why it won't work for me to call that function. Or why the hook in get_prop() doesn't work for me.

1

Regarding the filter, you tried, not working, my hunch is that it' not working, because it's missing (priority and) accepted arg count parameter(s). At the moment

add_filter('woocommerce_product_get_price', 'test_change');

Defaults to priority 10 and 1 parameter for your callback, but you're using two args in

function test_change($value, $prop) { return 666; }

Thus you'd need to change the filter like this,

add_filter('woocommerce_product_get_price', 'test_change', 10, 2);

You could also use some bigger or smaller number than 10 to make your filter fire later or earlier depending on your needs.

  • You are right. It was the problem with that the hook call was coded the wrong way buy me and that caused the site to break when I tried to call get_price(). It's now working. – Peter Westerlund Aug 3 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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