1

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;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Mathias Beck
  • 128
  • 2
  • 12
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Aksen P Mar 02 '20 at 12:07
  • Which version of WC are you using? I copied your function and do not get that error. – Howard E Mar 03 '20 at 00:57
  • WooCommerce 3.9.2 - try editing the variations. This is when i get the error and the variations are just blank. – Mathias Beck Mar 03 '20 at 15:30

0 Answers0