Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/7fc5f029914fdb78a12c2efcbcedd565 to your computer and use it in GitHub Desktop.
Save xlplugins/7fc5f029914fdb78a12c2efcbcedd565 to your computer and use it in GitHub Desktop.
Display Regular Price with Order total, sub total in mini cart final
class WFACP_Display_Regular_price_In_Mini_Cart {
public $regular_price = '';
public $tmp_reg_price = [];
public $is_on_sale_active = '';
public function __construct() {
add_filter( 'wfacp_woocommerce_cart_item_subtotal_except_subscription', '__return_false' );
add_action( 'wfacp_woocommerce_cart_item_subtotal_except_subscription_placeholder', [ $this, 'display_cut_price' ], 10, 3 );
add_filter( 'wfacp_subscription_price_display', [ $this, 'display_cut_price_subscription' ], 10, 4 );
add_action( 'wfacp_template_load', [ $this, 'action' ] );
add_action( 'woocommerce_review_order_before_order_total', [ $this, 'add_discount_row' ], 9999 );
}
public function add_discount_row() {
$cart_contents = WC()->cart->get_cart_contents();
$regular_price = 0;
foreach ( $cart_contents as $content ) {
$product = $content['data'];
if ( $product instanceof WC_Product ) {
$quantity = $content['quantity'];
$product = wc_get_product( $product->get_id() );
$regular_price = $regular_price + ( $quantity * $product->get_regular_price() );
}
}
$total = WC()->cart->get_cart_contents_total();
if ( $regular_price > $total ) {
?>
<tr class="order-total1">
<th><?php _e( '<span style=font-size:15px;font-weight:600;>Special Offer Applied.<br/>You Save:</span>', 'woocommerce' ); ?></th>
<td><?php echo wc_price( - ( $regular_price - $total ) ); ?></td>
</tr>
<?php
}
}
public function action() {
add_filter( 'woocommerce_cart_subtotal', [ $this, 'display_cart_subtotal_with_regular' ], 10, 3 );
add_filter( 'woocommerce_cart_totals_order_total_html', [ $this, 'display_cart_total_with_regular' ], 10 );
}
public function display_cut_price( $_product, $cart_item, $cart_item_key ) {
$quantity = $cart_item['quantity'];
$product_id = $_product->get_id();
$product = wc_get_product( $product_id );
$product_regular_price = $product->get_regular_price();
$product_regular_price *= $quantity;
$subtotal = WFACP_Common::get_product_subtotal( $_product, $cart_item, true );
if ( $product_regular_price > 0 && ( round( $subtotal, 2 ) !== round( $product_regular_price, 2 ) ) ) {
if ( $subtotal > $product_regular_price ) {
echo wc_price( $subtotal );
} else {
echo wc_format_sale_price( $product_regular_price, $subtotal );
}
} else {
echo wc_price( $subtotal );
}
}
public function display_cut_price_subscription( $price, $_product, $cart_item, $cart_item_key ) {
$qty = 1;
if ( isset( $cart_item['quantity'] ) ) {
$qty = $cart_item['quantity'];
}
if ( is_string( $cart_item_key ) && '' !== $cart_item_key && isset( WC()->cart->cart_contents[ $cart_item_key ] ) ) {
// calculate price data for cart item
$price_data = WFACP_Common::get_cart_product_price_data( $_product, $cart_item, $qty );
$s_price_data = $price_data;
$s_price_data['price'] = $s_price_data['regular_org'];
$main_product_price = WFACP_Common::get_subscription_price( $_product, $s_price_data );
if ( '' !== $cart_item_key ) {
$price_html = $price_data['price'];
} else {
$price_html = WFACP_Common::get_subscription_price( $_product, $price_data );
}
if ( $main_product_price == $price_html || $price_html > $main_product_price ) {
$price = wc_price( $price_html );
} else {
$price = wc_format_sale_price( $main_product_price, $price_html );
}
}
return $price;
}
public function display_cart_subtotal_with_regular( $cart_subtotal, $compound, $cart ) {
$cart_contents = $cart->cart_contents;
$subtotal = 0;
$product_regular_price = 0;
$total_ragular_price1=0;
$tmp_reg_price = [];
$tax = [];
foreach ( $cart_contents as $content ) {
$quantity = $content['quantity'];
$product = $content['data'];
$subtotal += round( $content['line_subtotal'], 2 );
// $subtotal += round( $content['line_subtotal_tax'], 2 );
$product = $content['data'];
$raw_data = $product->get_data();
$ragular_price = $raw_data['regular_price'];
$tax[]=round( $content['line_subtotal_tax'], 2 );
$tmp_reg_price[] = $ragular_price;
$this->tmp_reg_price[] = $ragular_price;
$total_ragular_price1 += $ragular_price * $quantity;
if ( $product->is_on_sale() ) {
$this->is_on_sale_active = 'is_on_sale';
$total_ragular_price = $ragular_price * $quantity;
$product_regular_price += $total_ragular_price;
} else {
$product_regular_price += $ragular_price;
}
}
$shipping = floatval(WC()->cart->get_shipping_total());
if($shipping > 0){
$total_ragular_price1+=$shipping;
}
if(is_array($tax) && count($tax) > 0){
$total_ragular_price1+=array_sum($tax);
}
$this->regular_price = wc_price( $total_ragular_price1 );
if ( $product_regular_price > 0 && ( round( $subtotal, 2 ) !== round( $product_regular_price, 2 ) ) ) {
if ( $subtotal > $product_regular_price ) {
$cart_subtotal = wc_price( $subtotal );
} else {
$cart_subtotal = wc_format_sale_price( $product_regular_price, $subtotal );
}
} else {
$cart_subtotal = wc_price( $subtotal );
}
return $cart_subtotal;
}
public function display_cart_total_with_regular( $total ) {
if ( empty( $this->is_on_sale_active ) ) {
return $total;
}
if ( ! empty( $this->regular_price ) ) {
$total = "<del style=margin-right:10px;>" . $this->regular_price . "</del>" . $total;
}
return $total;
}
}
new WFACP_Display_Regular_price_In_Mini_Cart();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment