Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/211e487ae09f7d949acc3951998fc02b to your computer and use it in GitHub Desktop.
Save xlplugins/211e487ae09f7d949acc3951998fc02b to your computer and use it in GitHub Desktop.
display strike-through price for cart item subtotal
class Fkcart_Cart_item_cut_Price {
public function __construct() {
add_action( 'fkcart_after_header', [ $this, 'attach_action' ] );
}
public function attach_action() {
add_filter( 'woocommerce_cart_item_subtotal', [ $this, 'display_strike_price' ], 999, 2 );
}
public function display_strike_price( $price, $cart_item ) {
$product = $cart_item['data'];
$qty = $cart_item['quantity'];
/**
* @var $product WC_Product;
*/
$regular = $product->get_regular_price() * $qty;
$price = $this->get_product_subtotal( $product, $qty );
if ( $regular > 0 && ( round( $price, 2 ) !== round( $regular, 2 ) ) ) {
if ( $price > $regular ) {
$price_html = wc_price( $price );
} else {
$price_html = wc_format_sale_price( $regular, $price );
}
} else {
$price_html = wc_price( $price );
}
return $price_html;
}
public function get_product_subtotal( $product, $quantity = 1 ) {
$price = $product->get_price();
if ( $product->is_taxable() ) {
if ( WC()->cart->display_prices_including_tax() ) {
$row_price = wc_get_price_including_tax( $product, array( 'qty' => $quantity ) );
} else {
$row_price = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) );
}
} else {
$row_price = (float) $price * (float) $quantity;
}
return $row_price;
}
}
new Fkcart_Cart_item_cut_Price();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment