John Davidson

php - Display product sale price in WooCommerce checkout issues

0 comments
Message:


I'm using this code snippet to display product sale prices at WooCommerce checkout:


function show_sale_price_at_checkout( $subtotal, $cart_item, $cart_item_key ) {

$product = $cart_item['data'];
$quantity = $cart_item['quantity'];
if ( ! $product ) {
return $subtotal;
}
$regular_price = $sale_price = $suffix = '';
if ( $product->is_taxable() ) {
if ( 'excl' === WC()->cart->tax_display_cart ) {
$regular_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) );
$sale_price = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_sale_price(), 'qty' => $quantity ) );
if ( WC()->cart->prices_include_tax && WC()->cart->tax_total > 0 ) {
$suffix .= ' ' . WC()->countries->ex_tax_or_vat() . '';
}
} else {
$regular_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) );
$sale_price = wc_get_price_including_tax( $product, array( 'price' => $product->get_sale_price(), 'qty' => $quantity ) );
if ( ! WC()->cart->prices_include_tax && WC()->cart->tax_total > 0 ) {
$suffix .= ' ' . WC()->countries->inc_tax_or_vat() . '';
}
}
} else {
$regular_price = $product->get_price() * $quantity;
$sale_price = $product->get_sale_price() * $quantity;
}
if ( $product->is_on_sale() && ! empty( $sale_price ) ) {
$price = wc_format_sale_price(
wc_get_price_to_display( $product, array( 'price' => $product->get_regular_price(), 'qty' => $quantity ) ),
wc_get_price_to_display( $product, array( 'qty' => $quantity ) )
) . $product->get_price_suffix();
} else {
$price = wc_price( $regular_price ) . $product->get_price_suffix();
}

$price = $price . $suffix;
return $price;
}
add_filter( 'woocommerce_cart_item_subtotal', 'show_sale_price_at_checkout', 10, 3 );

Screenshot: https://ibb.co/xLB60Px


However, there's 3 issues:


1. I'm unable to target the sale price individually with custom CSS. Is there a way to give the sale price a CSS class?


2. I can not change the color of the sale price properly. Setting the color to #000000 does not result into pure black, it stays grey. So I thought the sale price is a bit transparent but adding opacity: 1; did not help, it stays grey. I can not increase the opacity, only decrease it. I'm confused.


3. When the checkout page is loading it shows this ugly long message next to the products that have no sale price: https://ibb.co/PzJztck What does it mean and how to get rid of it?


Could someone please help resolve these issues?



My Answer:
>is_on_sale() ) {
$regular_price = wc_price( $product->get_regular_price() * $quantity );
$sale_price = wc_price( $product->get_sale_price() * $quantity );
$suffix = ' ' . $regular_price . '';
}
return $subtotal . '
' . $sale_price . $suffix;
}

add_filter( 'woocommerce_cart_item_subtotal', 'show_sale_price_at_checkout', 10, 3);

However, when I apply this code, the sale price is not displayed correctly at the checkout. It seems to be adding the sale price multiple times based on the quantity of the product in the cart. How can I fix this issue and display the correct sale price at the WooCommerce checkout?

Rate this post

5 of 5 based on 2942 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us