Home > Software design >  WoCommerce get all variation with price
WoCommerce get all variation with price

Time:10-07

I am trying to get all product variation along with price. I am using below code but price is not coming

$terms = wc_get_product_terms( $product->get_id(), $attribute, array( 'fields' => 'all' ) );

Any idea how i can get price as well along with price as DESC order

Thanks

CodePudding user response:

Depending on your need use

$product = wc_get_product(1234); // product ID 
$variations = $product->get_available_variations('objects'); // get variations as obj

//Both return array
$variations = $product->get_available_variations();
$variations = $product->get_available_variations('array');

CodePudding user response:

You can use this code for variation with the price. I check here if product type variable then it shows.

function show_product_variation(){
    global $product;
    if($product->product_type == 'variable' ){
        $available_variations = $product->get_available_variations();
        $count = count($available_variations)-1;
        echo "<pre>";
        $total = count($available_variations);
        $total -= 1;
        for($i=0;$i<$total;$i  ){
            echo $available_variations[$i]['attributes']['attribute_pa_color']."=>".$available_variations[$i]['display_price']."<br>";
        }
        echo "</pre>";
    }
}
add_action('woocommerce_share','show_product_variation');

Here I attach also a screenshot. enter image description here

Note: If you want to show it another location you learn about single product hook.

  • Related