I’m trying to list product variations and prices for each variation outside of woocommerce templates.
Anyone can suggest how can I access that info?
I have tried to do something like this:
$tickets = new WC_Product( $product_id);
$variables = $tickets->get_available_variations();
But this doesnt work because it is outside of loop, it returns error.
Idealy I would like to get all variations like an array:
$vars = array(
array('name' => 'My var name', 'price' => '123'),
array('name' => 'My var name', 'price' => '123'),
);
Maybe even if this can be done on ‘save_post’ for every product to create new post_meta and save this for future use, which would then be available to get like:
$meta = get_post_meta( $product_id, '_my_variations' );
Any suggestion is welcome.
To get product variations outside the loop, you need to create new instance of WC_Product_Variable class, here is an example:
In this way you will have all needed info for the variations in $variables array.
This the simplest solution to get product variables using product ID, outside of loop and woocommerce template