-1

On single product page of variable product, when I change different variation from the dropdown, I want to show the price from another custom field instead of default variation price (which field I already created in Dashboard). But, I didn't find a way to show the price on product page.

Here's my code:

add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );

function custom_price( $price, $product )
{
    return $price;
}

Inside the function custom_price, if I get the variation id, I could do something like this:

function custom_price( $price, $product )
{
    $variation_id = ???
    return get_post_meta( $variation_id, 'custom_field', true );
}

How can I get variation id inside this function? Thanks in advance.

Jitu Raiyan
  • 498
  • 6
  • 13
  • The answer below **is obsolete** since WooCommerce 3 [`WC_Product` `get_variation_id()`](https://docs.woocommerce.com/wc-apidocs/source-class-WC_Abstract_Legacy_Product.html#457-466) **is deprecated and outdated.** It has been replaced by `$product->get_id()`… So you should unaccept the answer below, as this is not the right answer. – LoicTheAztec May 28 '19 at 18:08

1 Answers1

0

You can simply use $product->get_id();

add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );

function custom_price( $price, $product )
{
    $variation_id = $product->get_id();

    return get_post_meta( $variation_id, 'custom_field', true );
}
Sushil Adhikari
  • 764
  • 6
  • 12
  • Sorry but this answer **is obsolete** since WooCommerce 3 [`WC_Product` `get_variation_id()`](https://docs.woocommerce.com/wc-apidocs/source-class-WC_Abstract_Legacy_Product.html#457-466) **is deprecated and outdated.** – LoicTheAztec May 28 '19 at 18:11