Okay
  Public Ticket #3624333
Discount Rules on cart page
Open

Comments

  • Ian Haider started the conversation

    Hi,

    Is there a way to use a hook or filter so that we can show the applied discount rule under each cart line item on the cart page? Currently, the discount is shown as a total under sub total field on the cart page. But we need that to display under each line item as well. Please let me know if that is feasible. 

    Example : In the attached screenshot you can see that there is a 15% Discount ($1000+) applied to the cart total. But we need to show (split amount fees) under each line item price.

    Attached files:  Screenshot from 2024-04-09 14-35-31.png

  •  1,881
    WebWizards replied

    Hi Ian,

    Thank you for purchasing our plugin,


    B2BKing does not have a way to display cart discounts like that. However, please note the following:

    Discount rules have the following checkbox that can be enabled:

    7376782425.png

    When this is enabled, discounts are not added to cart, but rather applied as a sale price to each product individually. For example:

    4114690856.png

    (50% discount added directly as a sale price to the product)


    If you were to use this checkbox, you could also show both the regular + sale price on the cart page, by adding the snippet from here: https://www.businessbloomer.com/woocommerce-display-regularsale-price-cart-table/


    Kind regards,

    Stefan

  • Ian Haider replied

    Hi 

    Thanks for your quick response. 

    Well, I am familiar with that checkbox that you have mentioned, but I need to show all the discounts that are available on the cart not under the cart subtotal but under each line item. If this is not possible then can you please let me know if there is a function to get the array of all discounts applied inside woocommerce_cart_item_price hook. That would be really helpful.

  •  1,881
    WebWizards replied

    Thank you for clarifying,

    If you specifically need to have only cart discounts without that checkbox enabled, 


    - "I need to show all the discounts that are available on the cart not under the cart subtotal but under each line item"

    I think WooCommerce doesn't really have a concept for this technically. I mean that as far as I know, the only ways to apply a discount are either by changing the product price (Everywhere, on all pages), or by applying a discount to cart totals.


    - "If this is not possible then can you please let me know if there is a function to get the array of all discounts applied inside woocommerce_cart_item_price hook."

    I think this would indeed be the most feasible approach, to basically show a text there under each product, just saying how much of that cart discount corresponds to each item. The text would be just 'informational' and not actually change price in any way.


    You could try adding this code snippet to your site:

    add_filter('woocommerce_cart_item_name', 'add_discount_info_directly_to_cart_items', 10, 3);
    function add_discount_info_directly_to_cart_items($item_name, $cart_item, $cart_item_key) {
        $cart = WC()->cart;
        $total_discount = 0;
        $cart_total = 0;
        // Calculate the total discount from fees.
        foreach ($cart->get_fees() as $fee) {
            if ($fee->total < 0) { // Assuming discounts are negative.
                $total_discount += abs($fee->total);
            }
        }
        // Calculate the total cart value excluding shipping, taxes, and discounts.
        foreach ($cart->get_cart() as $item) {
            $cart_total += $item['line_total'];
        }
        // Avoid division by zero.
        if ($cart_total <= 0 || $total_discount <= 0) {
            return $item_name;
        }
        // Calculate this item's share of the discount.
        $item_total = $cart_item['line_total'];
        $item_discount = ($item_total / $cart_total) * $total_discount;
        // If there's a discount, format it and append it to the item name.
        if ($item_discount > 0) {
            $discount_formatted = wc_price($item_discount);
            $item_name .= sprintf('<br><small>%s discount applied.</small>', $discount_formatted);
        }
        return $item_name;
    }
    
    

    It basically goes over the cart discounts and proportionally distributes them to cart items:

    2715756971.png


    If that doesn't work for you, I can also send you the code our plugin uses to get its discounts, so you can go through them, but please note the code is a bit long / messy.

  • Ian Haider replied

    Thanks for your reply it was really helpful. Also is there are way to display the same on order email notifications?

  •  1,881
    WebWizards replied

    I believe you can do it by adding this code as well: https://pastecode.io/s/nm10w04w

    However, I'd advise to do some significant testing before using any of these, and also check the display on pages like the 'thank you' page, my account orders page, etc, as results may sometimes be otherwise unpredictable.