I want in my website to have variation products for ex. books and ebooks and i want to display it, i figured out how to do this for all the cases except one. When the price of book is lower from the ebook it messed up.
i found and modified this code but i cannot find a way to make it correct for the situation i described above.
function wc_ninja_custom_variable_price( $price, $product ) {
// Main Price
$prices1 = array( $product->get_variation_price( ‘min’, true ), $product->get_variation_price( ‘max’, true ) );
$price = $prices1[0] !== $prices1[1] ? sprintf( __( ‘Βιβλιο: %2$s <br> Ebook: %1$s’, ‘woocommerce’,’woocommerce’ ), wc_price( $prices1[0]), wc_price( $prices1[1])) : wc_price( $prices1[0] );
// Sale Price
$prices2 = array( $product->get_variation_regular_price( ‘min’, true ), $product->get_variation_regular_price( ‘max’, true ) );
sort( $prices2 );
$saleprice = $prices2[0] !== $prices2[1] ? sprintf( __( ‘Βιβλιο : %2$s– %3$s <br> Ebook : %1$s ‘, ‘woocommerce’,’woocommerce’,’woocommerce’ ), wc_price( $prices2[0]),wc_price( $prices2[1]),wc_price( $prices1[1]) ) : wc_price( $prices2[0] );
$saleprice2 = $prices2[0] !== $prices2[1] ? sprintf( __( ‘Βιβλιο : %2$s– %3$s <br> Ebook : %1$s – %4$s ‘, ‘woocommerce’,’woocommerce’,’woocommerce’,’woocommerce’ ), wc_price( $prices2[0]),wc_price( $prices2[1]),wc_price( $prices1[1]),wc_price( $prices1[0]) ) : wc_price( $prices2[0] );
if ( $price !== $saleprice ) {
if($prices2[0] !== $prices1[0]){
$price = ” . $saleprice2 .’ ‘;
}
if(($prices2[0] == $prices1[0])&&($prices2[1] !== $prices1[1]) ){
$price = ‘ ‘ . $saleprice . ”;
}
else{
$price = ‘ ‘ . $price . ”;
}
}
return $price;
}
add_filter( ‘woocommerce_variable_sale_price_html’, ‘wc_ninja_custom_variable_price’, 10, 2 );
add_filter( ‘woocommerce_variable_price_html’, ‘wc_ninja_custom_variable_price’, 10, 2 );
question from:
https://stackoverflow.com/questions/65888997/wordpress-i-want-to-display-variations-prices-in-product-page-and-in-product-l 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…