D
D
DMax9212018-11-29 15:05:23
WordPress
DMax921, 2018-11-29 15:05:23

Price format in a category from variable products, how to decide?

Hello!
Please tell me what is wrong.
Here the filter displays the minimum price of the variation in the product category.

add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);

function custom_variation_price( $price, $product ) {
 $price = '';
 $price .= woocommerce_price($product->get_price());
 return $price;
}

But it doesn't display the discount if this variation has one.
And here is the code that displays the minimum price and the discount if there is one, but somehow strange, it takes the price from the ceiling.
add_filter( 'woocommerce_variable_price_html', 'custom_min_max_variable_price_html', 10, 2 );
function custom_min_max_variable_price_html( $price, $product ) {
    $prices = $product->get_variation_prices( true );
    $min_price = current( $prices['price'] );
    $max_price = end( $prices['price'] );

    $min_keys = current(array_keys( $prices['price'] ));
    $min_price_regular = $prices['regular_price'][$min_keys];
    $min_price_html = wc_price( $min_price ) . $product->get_price_suffix();

    if( $min_price_regular != $min_price ){ // When min price is on sale (Can be removed)
        $min_price_regular_html = '<del>' . wc_price( $min_price_regular ) . $product->get_price_suffix() . '</del>';
        $min_price_html = $min_price_regular_html .'<ins>' . $min_price_html . '</ins>';
    }
    $price = sprintf( __( '%1$s', 'woocommerce' ), $min_price_html );

    return $price;
}

What comes out, the first code prints the minimum price of the variation 271 - right! (old price 362)
The second code displays the minimum price of the same variation 288 - how is that? (old price 360)
Please help me blind...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2018-11-30
@deniscopro

And so?

add_filter('woocommerce_variable_price_html', 'dco_woocommerce_variable_price_html');

function dco_woocommerce_variable_price_html($price) {
    $array = explode(' &ndash; ', $price);
    return '<span class="price-from">от</span> ' . $array[0];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question