skip to Main Content

I use a function to add the price to the variation in the variation-dropdown in woocommerce. The problem is, when the variation name contains a space like "20 kg" the price is not showing. When i there is no space like "200kg" its working. What could be the problem?

function display_price_in_variation_options( $term ) {
    $product = wc_get_product();
    $id      = $product->get_id();
    if ( empty( $term ) || empty( $id ) ) {
        return $term;
    }
    if ( $product->is_type( 'variable' ) ) {
        $product_variations = $product->get_available_variations();
    } else {
        return $term;
    }
    foreach ( $product_variations as $variation ) {
        if ( count( $variation['attributes'] ) > 1 ) {
            return $term;
        }
        $attribute = array_values( $variation['attributes'] )[0];
        if ( $attribute === $term ) {
            $term .= ' (' . wp_strip_all_tags( wc_price( $variation['display_price'] ) ) . ')';
        }
    }
    return $term;
}
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_options' );

2

Answers


  1. Update your code

    function display_price_in_variation_options( $term ) {
        $product = wc_get_product();
        $id      = $product->get_id();
        if ( empty( $term ) || empty( $id ) ) {
            return $term;
        }
        if ( $product->is_type( 'variable' ) ) {
            $product_variations = $product->get_available_variations();
        } else {
            return $term;
        }
        foreach ( $product_variations as $variation ) {
            if ( count( $variation['attributes'] ) > 1 ) {
                return $term;
            }
            
            $attribute = str_replace(' ', '', $array_values( $variation['attributes'] )[0]);
            if ( $attribute === $term ) {
                $term .= ' (' . wp_strip_all_tags( wc_price( $variation['display_price'] ) ) . ')';
            }
        }
        return $term;
    }
    add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_options' );
    
    Login or Signup to reply.
  2. Variable $term returns a name of the attribute, whereas variable $attribute returns a slug. This is what caused a problem when you are trying to compare those 2 variables within IF statement.

    You can pass additional parameter $term_obj to your function, which returns an object of the term.

    Then you can get an actual slug of the term:
    $term_obj->slug

    And finally utilize it inside if statement: if ( $attribute === $term_obj->slug )

    Revised function would look like:

    function display_price_in_variation_options( $term, $term_obj ) {
        $product = wc_get_product();
        $id      = $product->get_id();
        if ( empty( $term ) || empty( $id ) ) {
            return $term;
        }
        if ( $product->is_type( 'variable' ) ) {
            $product_variations = $product->get_available_variations();
        } else {
            return $term;
        }
    
        foreach ( $product_variations as $variation ) {
            if ( count( $variation['attributes'] ) > 1 ) {
                return $term;
            }
            $attribute = array_values( $variation['attributes'] )[0];
    
            if ( $attribute === $term_obj->slug ) {
                $term .= ' (' . wp_strip_all_tags( wc_price( $variation['display_price'] ) ) . ')';
            }
        }
        return $term;
    }
    add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_options', 10, 2 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search