php - Call WC_Product get_price()?

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

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.

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.

Share Improve this question asked Aug 2, 2019 at 14:35 Peter WesterlundPeter Westerlund 1,0775 gold badges14 silver badges31 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745274341a4619943.html

相关推荐

  • php - Call WC_Product get_price()?

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

    1小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信