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