Getting product variations from WC_Product_Variation
I have an online store WooCommerce
that sells T-shirts. When a person looks at the cart, I would like to give them the ability to change the size on the shirt. Currently, the dropdown menu I made looks like this:
<?php
if ( $item->is_type( 'variation' ) ){
?>
<select name="" class="product-size">
<?php
foreach ( $item->get_available_variations() as $variation ):
?>
<option value="">test</option>
<?php
endforeach;
?>
</select>
<?php
}
? >
However, this gives me the following error:
Fatal error: Uncaught Error: Call to undefined method WC_Product_Variation::get_available_variations()
So it hit me; my variable $item
is already a variation. Is there a way to get other options for the same product?
thank
+3
source to share
1 answer
If you are checking the type of your object, this is the WC_Order_Item_Product class. You can use this
$variationId = $item->get_variation_id();
$variableProduct = new WC_Product_Variable($variationId)
$allVariations = $variableProduct->get_available_variations();
====================================
Oh, I see. You can try this.
$parentData = $item->get_parent_id();
$variableProduct = new WC_Product_Variable($parentData)
$allVariations = $variableProduct->get_available_variations();
+1
source to share