I’ve developed custom single product template for Woocommerce for products which works correctly and as intended. The problem is that now the customer wants some different variations of products that are being sold and I can’t work out how to add this functionality.
Currently on the product page I have used functions such as the_post_thumbail() and get_post_meta() to display various information about the product such as price and excerpt entered in the product post type. I then use do_action(‘woocommerce_simple_add_to_cart’) along with a few other buttons.
This all works fine but the problem is now with the variation feature. I found the following code which echos out the product variations ID and Variation Type but I’m not sure how to implement this into a working system. Any help or guidance would be appreciated.
global $product, $post;
$variations = $product->get_available_variations();
foreach ($variations as $key => $value) {
echo 'variation ID'.$value['variation_id'];
foreach ($value['attributes'] as $attr_key => $attr_value) {
echo $attr_key.': '.$attr_value;
}
}
I found the answer so I’m just posting this here in case anyone else has the same question. It was as simple as changing do_action(‘woocommerce_simple_add_to_cart’) to do_action(‘woocommerce_variable_add_to_cart’). This displays the product variations in a list box and has the add to cart button.