1

In order to return an array of available variations for the current product in WooCommerce, I would use something like this:

 global $product;
 
 $variations = $product->get_available_variations();

I am trying to achieve the same for when a product is added as a variable subscription product and was under the impression that since subscription products are an extension of WooCommerce products shouldn't the same methods work?

Chris

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Evakos
  • 152
  • 2
  • 11

1 Answers1

2

As Subscription Variable product extend WooCommerce WC_Product_Variable Class, so they can use mostly all available methods that WooCommerce Variable product can use. But they have also their own methods described in WC_Product_Variable_Subscription Class php file. Their product type is "variable-subscription".

For Product Subscription variation it's similar as they extend WooCommerce WC_Product_Variation Class, so they can use mostly all available methods that WooCommerce Product variation can use. But they have also their own methods described in WC_Product_Subscription_Variation Class php file. Their product type is "subscription_variation".

So your code will be:

global $product;

// Only for Variable subscription product
if ( $product->is_type('variable-subscription') ) {
 
    $available_variations = $product->get_available_variations();
 
    // Loop through available subscription variation products data
    foreach ( $available_variations as $variation_data ) {
        // Output formatted raw data (array)
        echo '<pre>'; print_r( $variation_data ); echo '</pre>';
    }
}

Tested and works

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks that is the correct answer to my original question as I wasn't testing for the type, however I should have added that I'm trying to modify the add to cart form within a function: `function woocommerce_variable_add_to_cart()`, so using the modified code within that function doesn't appear to work. All I'm trying to do which works with standard product variations is display them in tabs. I'm not sure if I should modify the original question? – Evakos Oct 11 '20 at 07:38
  • 1
    @Evakos Sorry, but I just remember you that `add_to_cart()` is not a product method, but a `WC_Cart` method… **Also there is no add_to_cart for variable products** as on variable product pages, customer adds a product variation. So to add_to_cart a Product Subscription **variation**, it works the same way than a normal WooCommerce Product Variation as follows: `WC()->cart->add_to_cart( int $variable_product_id, int $quantity, int $variation_id, array $variation_attributes, array $additional_item_data );` where 3 first arguments are required for variations and the 4th argument is recommende… – LoicTheAztec Oct 11 '20 at 09:13