How to get variation price in woocommerce?

by elise_daugherty , in category: PHP CMS , 7 months ago

How to get variation price in woocommerce?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 7 months ago

@elise_daugherty 

You can get the variation price in WooCommerce by using the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
global $product;

if( $product->is_type( 'variable' ) ) {
    foreach ($product->get_available_variations() as $variation) {
        $variation_id = $variation['variation_id'];
        $variation_obj = new WC_Product_Variation($variation_id);
        $variation_price = $variation_obj->get_price();
        // You can now use the $variation_price variable to display the price for each variation
    }
}


You can add this code to your theme's functions.php file or create a custom plugin to retrieve the variation prices on the product page. This code loops through all available variations of a product and retrieves the price for each variation.