I am trying to display a custom text next to a variation, in the select menu. I am using the following code. it works, but throws errors:
Uncaught Error: Call to a member function get_available_variations()
I have used some other code before that greys out the out of stock variation, but we want the user to be able to select the variation, as we have a waitlist for our products.
The code:
add_filter( 'woocommerce_variation_option_name', 'customizing_variations_terms_name', 10, 1 );
function customizing_variations_terms_name( $term_name ) {
global $product;
// Get available product variations
$product_variations = $product->get_available_variations();
foreach ( $product_variations as $product_variation ) {
if( isset( $product_variation['attributes'] ) ) {
$key = array_search($term_name, $product_variation['attributes']);
if( $key !== false && ! $product_variation['is_in_stock'] ) {
return $term_name . ' - Out of Stock';
}
}
}
return $term_name;
}