filters – woocommerce_variation_option_name hook doesn’t work

Question

I have following code inserted into functions.php:

add_filter( 'woocommerce_variation_option_name','display_price_in_variation_option_name');

function display_price_in_variation_option_name( $term ) {
    global $product;

    if ( empty( $term ) ) {
        return $term;
    }
    if ($product == null) {
        return $term;
    }

    $variation_id = $product->get_children();

    foreach ( $variation_id as $id ) {
        $_product       = new WC_Product_Variation( $id );
        $variation_data = $_product->get_variation_attributes();

        foreach ( $variation_data as $key => $data ) {
            if ( $data == $term ) {
                $html = $term;
                $html .= $_product->get_stock_quantity() > 0 ? ' - Fast Delivery' : '';
                return $html;
            }
        }
    }

    return $term;
}

It has to display ‘Fast delivery’ option right next to the size if the size in stock right now.

Maybe there is an issue in the way I add attributes to the product, because this code works before. Previously I added attributes like this:
enter image description here

Then I decided to make this attribute global so my search filter works well. And now I simply choose Size attribute instead of Create custom attribute option.

So my attribute looks like this:
enter image description here

And this:

enter image description here

Do you have some ideas how to make this code alive?

0
Aleksei .C 4 months 2022-07-29T05:58:42-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse