Skip to content

Instantly share code, notes, and snippets.

@corsonr
Created January 28, 2019 12:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save corsonr/c8358a65039209594d48526faec5104f to your computer and use it in GitHub Desktop.
Save corsonr/c8358a65039209594d48526faec5104f to your computer and use it in GitHub Desktop.
WooCommerce: display products discounted price in cart table
<?php
/*
* Display discounted products prices in the cart table.
*
* Modification of code provided here https://businessbloomer.com/woocommerce-display-cart-item-subtotal-coupon-discount/.
*/
add_filter( 'woocommerce_cart_item_subtotal', 'if_coupon_slash_item_subtotal', 99, 3 );
add_filter( 'woocommerce_cart_item_price', 'if_coupon_slash_item_subtotal', 99, 3 );
/**
* Display months discounted prices on cart page table.
*
*/
function if_coupon_slash_item_subtotal( $subtotal, $cart_item, $cart_item_key ) {
global $woocommerce;
// Check if cart has coupon.
if ( ! $woocommerce->cart->has_discount() ) {
return $subtotal;
}
// Get coupons.
$coupons = WC()->cart->get_applied_coupons();
// Loop through coupons.
foreach ( $coupons as $coupon ) {
// Create coupon object.
$coupon = new WC_Coupon( $coupon );
// Check if specific coupon is applied.
if ( $woocommerce->cart->has_discount( $coupon->get_code() ) ) {
// Check if products in cart are from a specific category).
if ( has_term( 'my-category-slug', 'product_cat', $cart_item['product_id'] ) ) {
// If coupon type is fixed_product.
if ( 'fixed_product' === $coupon->get_discount_type() ) {
$newsubtotal = wc_price( $cart_item['data']->get_price() - $coupon->get_amount() * $cart_item['quantity'] );
} else { // If coupon type is percentage.
$newsubtotal = wc_price( $cart_item['data']->get_price() * ( 1 - ( '0.' . $coupon->get_amount() ) ) * $cart_item['quantity'] );
}
$subtotal = sprintf( '<s>%s</s> %s', $subtotal, $newsubtotal );
}
}
}
return $subtotal;
}
@mugukamil
Copy link

Thanks man,
on line: 36, we can use if ( has_term( $coupon->product_categories, 'product_cat', $cart_item['product_id'] ) ) {

to automatically change subtotal on applied product categories

@diogenesjup
Copy link

Nice trick! Works for me

@DECEiFER
Copy link

Here's an updated function based on the above with a few extra checks, including product ID inclusions and exclusions as well as product cat inclusions and exclusions. It also checks if the calling filter is the subtotal one for applying the quantity multiplier in the equations at the end.

function if_coupon_slash_item_subtotal($subtotal, $cart_item, $cart_item_key){
	// Check if cart has coupon.
	if (!WC()->cart->has_discount()){
		return $subtotal;
	}

	// Get coupons.
	$coupons = WC()->cart->get_applied_coupons();

	// Loop through coupons.
	foreach ($coupons as $coupon){
		// Create coupon object.
		$coupon = new WC_Coupon($coupon);

		// Check if products are not resticted from this coupon
		$included_products = true;
		$included_cats = true;

		if (count($product_ids = $coupon->get_product_ids()) > 0){
			if (!in_array($cart_item['product_id'], $product_ids, false)){
				$included_products = false;
			}
		}
		if (count($exluded_product_ids = $coupon->get_excluded_product_ids()) > 0){
			if (in_array($cart_item['product_id'], $exluded_product_ids, false)){
				$included_products = false;
			}
		}

		if (count($product_cats = $coupon->get_product_categories()) > 0){
			if (!has_term($product_cats, 'product_cat', $cart_item['product_id'])){
				$included_cats = false;
			}
		}
		if (count($excluded_product_cats = $coupon->get_excluded_product_categories()) > 0){
			if (has_term($excluded_product_cats, 'product_cat', $cart_item['product_id'])){
				$included_cats = false;
			}
		}

		if ($included_products && $included_cats){
			$filter = current_filter();

			// If coupon type is fixed_product.
			if ($coupon->get_discount_type() === "fixed_product"){
				$newsubtotal = wc_price(($cart_item['data']->get_price() - $coupon->get_amount()) * (int)($filter === 'woocommerce_cart_item_subtotal' ? $cart_item['quantity'] : 1));
			} else{ // If coupon type is percentage.
				$newsubtotal = wc_price($cart_item['data']->get_price() * (float)(1 - ('0.' . $coupon->get_amount())) * (int)($filter === 'woocommerce_cart_item_subtotal' ? $cart_item['quantity'] : 1));
			}
		}
	}

	return $subtotal;
}
add_filter('woocommerce_cart_item_subtotal', 'if_coupon_slash_item_subtotal', PHP_INT_MAX, 3);
add_filter('woocommerce_cart_item_price', 'if_coupon_slash_item_subtotal', PHP_INT_MAX, 3);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment