Home > Net >  "starting price" only for specific categories - woocommerce
"starting price" only for specific categories - woocommerce

Time:09-16

Good morning, hope someone can help me!

how can i limit this code to a single category of variable products?

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_min', 9999, 2 );
function bbloomer_variation_price_format_min( $price, $product ) {
    $prices = $product->get_variation_prices('max');
    $min_price = ($product->get_variation_price('max'))/1000; // Max price
    $price = sprintf( __( 'A partire da: %1$s al gr', 'woocommerce' ), wc_price( $min_price ) );
    return $price;
}

CodePudding user response:

It looks like there are a couple of things wrong with your example. At the moment you are showing the maximum variation price (divided by a thousand). I think what you want to do is show the minimum variation price, as your label says 'Starting from' (A partire da).

Also at the moment you are not taking sale prices into account.

Fixing those issues and adding a category check the code snippet should look like this:

add_filter( 'woocommerce_variable_price_html', function( $price, $product ) {

    // Set the allowed categories here
    $allowed_categories = array( 'Nuts', 'Spices' );

    $terms = get_the_terms( $product->get_id(), 'product_cat' );
    foreach ( $terms as $term_key => $term ) {
        $categories[$term->term_id] = $term->name;
    }

    if ( !empty( array_intersect( $allowed_categories, $categories ) ) ) {
        $prices = $product->get_variation_prices( true );
        $min_price = current( $prices['price'] );
        $max_price = end( $prices['price'] );
        $min_reg_price = current( $prices['regular_price'] );
        $max_reg_price = end( $prices['regular_price'] );

        if ( $min_price !== $max_price || ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) ) {
            $price = sprintf( __( 'A partire da: %1$s al gr', 'woocommerce' ), wc_price( $min_price / 1000, array( 'decimals' => 3 ) ) );
        }
    }

    return $price;
 }, 10, 2 );

You can set the categories in the $allowed_categories array.

I also passed the decimals argument to the wc_price() function. As it looks you want to display the price per gram, and this can cause rounding errors. For instance if you have a product that is €15,- and you apply the above code with the default number of decimals for the currency (which is 2 for Euro) it would output:

A partire da: €0,02 al gr

CodePudding user response:

actually i solved, but not 100% enter image description here

as you can see from the image and from my code I wanted to insert the calculation of the price per gram for a category of products with the words "starting from price per gram" and for other products outside the category I want to show the words " starting from… the lowest price. ”The problem is that for the products of the" legal cannabis "category everything is correct and for the others it shows me the highest price and not the lowest one.

enter image description here

  • Related