Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce: Show product price after coupon discount WordPress

  • SOLVED

I have created several 'single product' coupons in WooCommerce. To keep things simple, these coupons apply to only one product, and give a fixed discount.

On the WooCommerce cart and checkout pages, the products are still displayed with the regular price, but then on the bottom of the table the discount is shown to make the final total correct.

I would like to show each product with the correct values, not just the correct total at the end.

This is how the cart and checkout pages look now:
Product 1: $20
Product 2: $10
Coupon-Product-1: -$5
Total: $25


This is what I want:
Product 1: $̶2̶0̶ $15
Product 2: $10
Total: $25


Thanks!

Answers (1)

2014-04-25

Dbranes answers:

You can try the following:

<strong>Step 1:</strong>


/**
* Show the product price before (striked out) and after coupon discount
* @uses wpq_9522_discount()
* @link http://www.wpquestions.com/question/showChronoLoggedIn/id/9522
*/
function wpq_9522_woocommerce_cart_item_subtotal( $subtotal, $cart_item, $cart_item_key ){
foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) :
if( in_array( $cart_item['product_id'], $coupon->product_ids )
|| in_array( $coupon->discount_type, array( 'percent_cart', 'fixed_cart' ) ) ):
$newsubtotal = wc_price( wpq_9522_discount( $cart_item['line_total'], $coupon->discount_type, $coupon->amount ) );
return sprintf( '%s %s', $subtotal, $newsubtotal );
endif;
endforeach;

return $subtotal;
}

add_filter( 'woocommerce_cart_item_subtotal', 'wpq_9522_woocommerce_cart_item_subtotal', 99, 3 );


where

function wpq_9522_discount( $price, $type, $amount ){

switch( $type ){
case 'percent_product':
$newprice = $price * ( 1 - $amount/100 );
break;
case 'fixed_product':
$newprice = $price - $amount;
break;
case 'percent_cart':
$newprice = $price * ( 1 - $amount/100 );
break;
case 'fixed_cart':
$newprice = $price - $amount;
break;
default:
$newprice = $price;
}

return $newprice;
}


<strong>Step 2:</strong>
Modify the cart subtotal:

/**
* Show the cart subtotal before (striked out) and after coupon discount
* @uses wpq_9522_discount()
* @link http://www.wpquestions.com/question/showChronoLoggedIn/id/9522
*/
function wpq_9522_woocommerce_cart_subtotal( $cart_subtotal, $compound, $obj ){
$t = 0;
foreach ( $obj->cart_contents as $key => $product ) :
$product_price = $product['line_total'];
foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) :
if( in_array( $product['product_id'], $coupon->product_ids )
|| in_array( $coupon->discount_type, array( 'percent_cart', 'fixed_cart' ) ) ):
$product_price = wpq_9522_discount( $product['line_total'], $coupon->discount_type, $coupon->amount );
endif;
endforeach;
$t += $product_price;
endforeach;

return ( $t > 0 ) ? sprintf( '%s %s', $cart_subtotal, wc_price( $t ) ) : $cart_subtotal ;
}

add_filter( 'woocommerce_cart_subtotal', 'wpq_9522_woocommerce_cart_subtotal', 99, 3 );


<strong>Step 3:</strong>

You can then remove the following part from your own <em>/checkout/review-order.php</em> and <em>/cart/cart-totals.php</em> template files:

<?php foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : ?>
<tr class="order-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>


and this one:

<?php foreach ( WC()->cart->get_coupons( 'cart' ) as $code => $coupon ) : ?>
<tr class="cart-discount coupon-<?php echo esc_attr( $code ); ?>">
<th><?php wc_cart_totals_coupon_label( $coupon ); ?></th>
<td><?php wc_cart_totals_coupon_html( $coupon ); ?></td>
</tr>
<?php endforeach; ?>


or hide it with CSS:

.order-discount {
display: none;
}


Here are some example screenshots:

cart:
[[LINK href="http://i.imgur.com/fa4CYRi.gif"]]http://i.imgur.com/fa4CYRi.gif[[/LINK]]

checkout:
[[LINK href="http://i.imgur.com/MgHGLnL.gif"]]http://i.imgur.com/MgHGLnL.gif[[/LINK]]


EricNeedsHelp comments:

This is a really great answer. Thanks for being so detailed, works perfect! :-)