0

How to apply custom discount of some percentage like (10%,20%) of cart total once he chose use custom discount button and if he want to remove the custom discount from the cart total in front end how can i proceed.

I want to show same as apply discount code section.in frontend

thanks in advance

3
  • Do you want to apply discount programmatically ? Commented Dec 2, 2017 at 10:12
  • Yes, @Sameer Bhayani Commented Dec 4, 2017 at 4:04
  • I have updated answer. Hopefully it will help you Commented Dec 4, 2017 at 6:10

3 Answers 3

2

etc/sales.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
    <section name="quote">
        <group name="totals">
            <item name="testdiscount" instance="MagePal\TestDiscount\Model\Quote\Discount" sort_order="500"/>
        </group>
    </section>
</config>

Discount.php Model file

public function collect(
        \Magento\Quote\Model\Quote $quote,
        \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
        \Magento\Quote\Model\Quote\Address\Total $total
    ) {
        parent::collect($quote, $shippingAssignment, $total);
        //$address             = $shippingAssignment->getShipping()->getAddress();
        $label               = 'My Custom Discount';
        $discountAmount      = -10;   
        $appliedCartDiscount = 0;
        if($total->getDiscountDescription()) {
            // If a discount exists in cart and another discount is applied, the add both discounts.
            $appliedCartDiscount = $total->getDiscountAmount();
            $discountAmount      = $total->getDiscountAmount()+$discountAmount;
        $label               = $total->getDiscountDescription().', '.$label;
        }    

        $total->setDiscountDescription($label);
    $total->setDiscountAmount($discountAmount);
    $total->setBaseDiscountAmount($discountAmount);
        $total->setSubtotalWithDiscount($total->getSubtotal() + $discountAmount);
        $total->setBaseSubtotalWithDiscount($total->getBaseSubtotal() + $discountAmount);

       if(isset($appliedCartDiscount)) {
        $total->addTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
        $total->addBaseTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
    } else {
        $total->addTotalAmount($this->getCode(), $discountAmount);
        $total->addBaseTotalAmount($this->getCode(), $discountAmount);
    }

        return $this;
    }

checkout_cart_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.cart.totals">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="block-totals" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="before_grandtotal" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="testdiscount" xsi:type="array">
                                            <item name="config" xsi:type="array">
                                                <item name="title" xsi:type="string" translate="true">My Discount</item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

checkout_index_index.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="checkout.root">
            <arguments>
                <argument name="jsLayout" xsi:type="array">
                    <item name="components" xsi:type="array">
                        <item name="checkout" xsi:type="array">
                            <item name="children" xsi:type="array">
                                <item name="sidebar" xsi:type="array">
                                    <item name="children" xsi:type="array">
                                        <item name="summary" xsi:type="array">
                                            <item name="children" xsi:type="array">
                                                <item name="totals" xsi:type="array">
                                                    <item name="children" xsi:type="array">
                                                        <item name="testdiscount" xsi:type="array">
                                                            <item name="config" xsi:type="array">
                                                                <item name="title" xsi:type="string" translate="true">My Discount</item>
                                                            </item>
                                                        </item>
                                                    </item>
                                                </item>
                                            </item>
                                        </item>
                                    </item>
                                </item>
                            </item>
                        </item>
                    </item>
                </argument>
            </arguments>
        </referenceBlock>
    </body>
</page>

You can also refer https://github.com/magepal/stackexchange/tree/develop/104112

2
  • how can it be performed on any action? It should not be mandatory to discount the cart. Commented Mar 27, 2021 at 19:39
  • It will trigger each time when your total / quote update on any event. Commented Mar 30, 2021 at 8:20
1

From admin panel go to Marketing > Promotions > Cart Price Rules and create a discount rule there.

0

If you want an advanced function my suggestion:

https://github.com/hodongnhut/magento2-payment-discount

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.