0
votes

I'm using a coupon for a calculation in the Woocommerce cart. It automatically adds a discount to the total so the right amount can be sent to payments gateways.

I'd like to hide all infos about this coupons/discount from visitors.

Problem: The only method I've found (see below) hides the coupon field, row (from totals) and messages, but also disable the coupon...

add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field' );
function hide_coupon_field( $enabled ) {
    if ( is_cart() || is_checkout() ) {
        $enabled = false;
    }
    return $enabled;
}

Is there a hook allowing to hide everything related to the discount without canceling the coupon?


EDIT: Looks like it's impossible to simply remove the discount line in the order-details. So a simple solution, inspired by helgatheviking suggestion, might be to remove all the totals generated by this part

<?php if ( $totals = $order->get_order_item_totals() ) foreach ( $totals as $total ) : ?>
            <tr>
                <th scope="row"><?php echo $total['label']; ?></th>
                <td><?php echo $total['value']; ?></td>
            </tr>
    <?php endforeach; ?>

And then to echo them one by one the way I need it. I'm already able to show the order total with this

<td><?php echo number_format($order->get_total(),2,'.','')."&#8364;"; ?></td>

but now I'm trying to retrieve the order subtotal, and this code

<td><?php echo number_format($order->get_item_subtotal(),2,'.','')."&#8364;"; ?></td>

gives me a Warning: Missing argument 1 for WC_Order::get_item_subtotal().

I'm not sure if get_item_subtotal() is the right way to get the order subtotal. And if so, what argument is missing? Or should I search around get_line_subtotal or get_subtotal_to_display?

1
If you look at the get_item_subtotal() method in the WC_Order class you will see that an $item argument is required.helgatheviking

1 Answers

1
votes

No, there does not seem to be as there is no filter in the get_coupons() method of the cart class. If you went to the WooCommerce git repo and sent a pull request with a filter here and an explanation as to why it should be there, they might consider merging it in. I've done that a few times.

You could also, copy the checkout/review-order.php and cart/cart-totals.php templates into your theme and remove the following two blocks of code:

<?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; ?>

and

<?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; ?>

Keep in mind that this prevents the display of ALL coupon discounts and will end up looking like the following screenshots:

Hide coupons in CartHide coupons in Checkout

I'm not a fan of overriding the more complex WC templates... especially not the ones pertaining to the checkout process. I've had to fix many sites that stopped working when their theme template overrides became obsolete as WooCommerce develops.

Edit

I tracked down the Discount row in the order/order-details.php template. It is from the function $order->get_order_item_totals()... this returns an array of rows and can be filtered. So, this removes the row from the order received page:

function so_25714509_get_order_item_totals( $total_rows ){
    unset( $total_rows['order_discount'] );
    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'so_25714509_get_order_item_totals' );