A
A
Alexis_D2019-06-15 09:31:17
WordPress
Alexis_D, 2019-06-15 09:31:17

Why can't I get the value from each Advanced Custom Fields product?

Good day, I have an int store built on Woocommerce, I have a group of fields created using Advanced Custom Fields, I made the display of this group of fields in each product and set the default value to, say, 1500. I import products using WP All Import and these imported products are displayed with a zero price, in function.php I wrote a code that changes the price, according to the formula I need, I take one of the values ​​\u200b\u200bfrom this very group of fields from each product, I found the reason for displaying a zero price - the value (1500), which is set by default , is entered into the formula as 0, that is, now I will show an abbreviated formula for a good example:

function my_price($price, $_product){
  
  $number = get_field('number', $post->ID); 
 	$new_price = $number ;
  return $new_price; 
  
}

add_filter('woocommerce_product_get_price', 'my_price',100,2);

$number is the value from the product, which should be 1500 by default, but equals 0, I don’t understand what the problem is, but if you go to the product and click refresh, everything works, what could be the reason?
Thanks in advance for your reply!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pychev Anatoly, 2019-06-15
@pton

Well, the first thing that rushed was the absence global $post;at the beginning of the function. Although you get the $_product variable in the function arguments, this is a product object, then take $_product->get_id()it from it, and then $post is not needed.
What works, Are the products with the correct price unloaded?
Ran your code, it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question