add message to woocommerce variation based on stock quantity

I succesfully added a filter to woocommmerce single product’s short description that displays a different message based on stock quantity and availability. The code is as follows:

add_filter( 'woocommerce_short_description', 'custom_stock_messages', 25 );
function custom_stock_messages($desc) {
    global $product;
    $prodquantity = $product->get_stock_quantity();
    if ( ! $product->is_in_stock() ) {
        $desc .= '<p>この商品は完売しました。</p>';
        return $desc;
    }
    else {
        if ( $prodquantity < 1 ) {
            $desc .= '<p>この商品はご注文を受けてから製作に入るため、発送まで10日~2週間頂きます。</p>';
            return $desc;
        }
        else {
            return $desc;
        }
    }
}

It works like a charm for simple products, but for variable products it only works if the stock is managed at a product level. If I, instead, choose to manage it at a variation level, the messages do not change when selecting the variation. I tried and tried but I can’t figure this out, as I’m still not an expert programmer. Thanks in advance
Alessio

Related posts

Leave a Reply