0

I want to remove discount applied(Coupon code applied) when order is placed. Any help would be appreciated.

My controller code:

class Success extends \Magento\Checkout\Controller\Onepage implements HttpGetActionInterface
{
    /**
     * Order success action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $session = $this->getOnepage()->getCheckout();

        if (!$this->_objectManager->get(\Magento\Checkout\Model\Session\SuccessValidator::class)->isValid()) {
            return $this->resultRedirectFactory->create()->setPath('checkout/cart');
        }
        $session->clearQuote();


        //@todo: Refactor it to match CQRS
        $resultPage = $this->resultPageFactory->create();
        $this->_eventManager->dispatch(
            'checkout_onepage_controller_success_action',
            [
                'order_ids' => [$session->getLastOrderId()],
                'order' => $session->getLastRealOrder()
            ]
        );
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
        $checkoutSession->getQuote()->setCouponCode('NEW10')->collectTotals()->save();

        return $resultPage;
    }
}

2 Answers 2

0

In your case, do this things :

$checkoutSession->getQuote()->setCouponCode('')->collectTotals()->save();
6
  • I tried it on place order controller as well as on checkout_onepage_controller_success_action event . It is not working.
    – mechanic
    Commented Sep 9, 2021 at 13:27
  • share the code what you have try. Commented Sep 9, 2021 at 13:29
  • I have added the code to my question.
    – mechanic
    Commented Sep 9, 2021 at 13:34
  • I update the answer, Kindly check. Commented Sep 9, 2021 at 13:36
  • Okay. Let me try
    – mechanic
    Commented Sep 9, 2021 at 13:37
0

Create a Custom Module Vendor_ModuleName and add the Following Files :

app/code/Vendor/ModuleName/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <type name="Magento\SalesRule\Model\RulesApplier">
        <plugin name="remove_coupon_on_order" type="Vendor\ModuleName\Plugin\SalesRule\Model\RulesApplier" sortOrder="1" disabled="false"/>
    </type>
</config>

app\code\Vendor\ModuleName\Plugin\SalesRule\Model\RulesApplier.php

<?php
 
namespace Vendor\ModuleName\Plugin\SalesRule\Model;
 
class RulesApplier
{
 
    private $rules;
 
    public function __construct(
        \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $rulesFactory
    ) {
        $this->ruleCollection = $rulesFactory;
    }
 
    public function aroundApplyRules(
        \Magento\SalesRule\Model\RulesApplier $subject,
        \Closure $proceed,
        $item,
        $rules,
        $skipValidation,
        $couponCode
    ) {
        $rules = $this->ruleCollection->create()->addFieldToFilter("rule_id", ["eq"=> ""]);
        $result = $proceed($item, $rules, $skipValidation, $couponCode);
        return $result;
    }
}

Hope It Will Help You !!

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.