{"id":1491,"date":"2020-12-28T11:08:06","date_gmt":"2020-12-28T11:08:06","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/restore-coupon-code-after-order-cancellation-magento-2\/"},"modified":"2025-05-21T17:23:02","modified_gmt":"2025-05-21T11:53:02","slug":"restore-coupon-code-after-order-cancellation-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/restore-coupon-code-after-order-cancellation-magento-2\/","title":{"rendered":"How to Restore Coupon Code after Order Cancellation in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Success of the businesses rely on developing a strategy to retain loyal customer-base. To attract and retain loyal customers, store owners&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-magento-shopping-cart-price-rules\/\" target=\"_blank\" rel=\"noreferrer noopener\">add shopping cart price rules in Magento 2<\/a>&nbsp;to facilitate users with discounts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">People love discounts, and to attract potential buyers and existing customers, Magento 2 store owners implement the strategy to offer personal coupons which expire after a single use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, in Magento 2, if a customer places the order using the personal coupon code and cancels the order due to some reason like failed payment, reconsidering product options, etc; the coupon code gets expired. Now, when the same customer tries to reorder by using the personal coupon again, the coupon code becomes invalid as it\u2019s already used once.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Store owners should consider this issue as a negative impact on shopping experience of their customers and must<strong>&nbsp;restore coupon code after order cancellation in Magento 2<\/strong>. This way, the customer can get benefit of discounts they are offered even after the order cancellation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Follow the below solution to&nbsp;<em>restore coupon code after order cancellation in Magento 2.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Programmatic Solution to Restore Coupon Code after Order Cancellation in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file at&nbsp;<strong><strong><em><strong><strong><em>Vendor\\Extension\\<\/em><\/strong><\/strong><\/em><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\n\\Magento\\Framework\\Component\\ComponentRegistrar::register(\n    \\Magento\\Framework\\Component\\ComponentRegistrar::MODULE,\n    'Vendor_Extension',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file at&nbsp;<strong><strong><em><strong><strong><em>Vendor\\Extension\\etc\\<\/em><\/strong><\/strong><\/em><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Extension\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create<strong>&nbsp;events.xml&nbsp;<\/strong>file&nbsp;at&nbsp;<strong><strong><em><strong><strong><em>Vendor\\Extension\\etc\\<\/em><\/strong><\/strong><\/em><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Event\/etc\/events.xsd\">\n    &lt;event name=\"sales_order_payment_cancel\">\n        &lt;observer name=\"observer\" instance=\"Vendor\\Extension\\Observer\\Observer\"\/>\n    &lt;\/event>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>Observer.php<\/strong>&nbsp; at<strong><strong><em>&nbsp;<strong><strong><em>Vendor\\Extension\\Observer\\<\/em><\/strong><\/strong><\/em><\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Observer;\n\nuse Magento\\SalesRule\\Model\\Coupon;\nuse Magento\\SalesRule\\Model\\RuleFactory;\n\nclass Observer\n{\n    protected $couponModel;\n    protected $rule;\n\n    public function __construct(\n        Coupon $couponModel,\n        RuleFactory $rule\n\n    )\n    {\n        $this->couponModel = $couponModel;\n        $this->rule = $rule;\n    }\n\n    public function execute($observer)\n    {\n        $event = $observer->getEvent();\n        $order = $event->getPayment()->getOrder();\n        if ($order->canCancel()) {\n            if ($code = $order->getCouponCode()) {\n                $coupon = $this->couponModel->create()->load($code, 'coupon_code');\n                $coupon->setTimesUsed($coupon->getTimesUsed() - 1);\n                $coupon->save();\n                if ($customerId = $order->getCustomerId()) {\n                    if ($customerCoupon = $this->rule->create()->load($coupon->getId())) {\n                        $customerCoupon->setTimesUsed($customerCoupon->getTimesUsed() - 1);\n                        $customerCoupon->save();\n                    }\n                }\n            }\n        }\n        $urlInterface = \\Magento\\Framework\\App\\ObjectManager::getInstance()->get('Magento\\Framework\\UrlInterface');\n        return $urlInterface->getCurrentUrl();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Voila! the coupon can be applied again!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please do consider sharing this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Success of the businesses rely on developing a strategy to retain loyal customer-base. To attract and retain loyal customers, store owners&nbsp;add shopping cart price rules&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1491","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1491"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1491\/revisions"}],"predecessor-version":[{"id":13899,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1491\/revisions\/13899"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}