• Resolved diekoralle

    (@diekoralle)


    Hi there,

    I have the following problem: I have a variable product which has two variations. However, the number of variations is controlled by the product. So if a variation “A” is sold, the number of the total product goes down by one and not only for the variation “A”.
    Now when I am on the product page, I have to select a variation before I can add it to the shopping cart. When I have selected a variation, I am also shown the number of variations available. However, I would like to have this display already on the product page and not only when the variation has been selected. How can I implement this? Since the number depends on the product and not on the variations, this should be possible, right?

    Thanks a lot for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi @diekoralle,

    I would like to have this display already on the product page and not only when the variation has been selected

    Thanks for your question! Please add this PHP snippet and you’ll see the stock number of all variations displayed in the product page:

    add_action( 'woocommerce_before_add_to_cart_button', 'display_stock_variations_loop' );
    function display_stock_variations_loop(){
        global $product;
        if ( $product->get_type() == 'variable' ) {
            foreach ( $product->get_available_variations() as $key ) {
                $attr_string = array();
                foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                    $attr_string[] = $attr_value;
                }
                if ( $key['max_qty'] > 0 ) { 
            echo '' . implode( ', ', $attr_string ) . ': ' . $key['max_qty'] . ' in stock<br>'; 
                } else { 
            echo '' . implode(', ', $attr_string ) . ': out of stock<br>'; 
                }
        }
        echo '<br>';
        }
    }

    Add the code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin.

    Hope this helps!

    Thread Starter diekoralle

    (@diekoralle)

    Hi Mirko,

    Very cool! Thanks a lot! That solves exactly my problem ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Number of remaining tickets of variable products on the product page’ is closed to new replies.