cancel
Showing results for 
Search instead for 
Did you mean: 

Coupon Code Issue

Coupon Code Issue

Hi,

 

I want to create a coupon code which is applicable only one time(rule is working fine but it's not checking the order status if customer failed to pay then it can't usable on next time) so customer able to use again & again until they make successful credit card order. 

 

Please let me know how can i make this. 

2 REPLIES 2

Re: Coupon Code Issue

Hello,

 

I can help you to build a logic for that.

 

You can create plugin (Interceptor) or event observer which will call after each status change of order.

 

so at that time you can revert or rollback you database changes regarding coupon code.

 

I would like to to tell you that it will be great for you if find database tables for that your self instead of direct code.

 

Enjoy your coding !!! Smiley Happy

 

If this helps you then click on Kudos and Accept as Answer.

 

Thank you

Hiren Patel

Re: Coupon Code Issue

Hello @gkarthick87 

 

Use the event observer for the coupon code issue for the solution.

 

Create config.xml file at Meetanshi\CouponCode\etc\config.xml
        <?xml version="1.0"?>
            <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
                <event name="sales_order_payment_cancel">
                    <observer name="observer" instance="Meetanshi\PlaceOrder\Observer\Observer" />
                </event>
            </config>
Now, create observer file at Meetanshi\CouponCode\Observer\Observer.php
<?php
namespace Meetanshi\PlaceOrder\Observer;
use Magento\SalesRule\Model\Coupon;
use Magento\SalesRule\Model\RuleFactory;
class Observer
{
    protected $couponModel;
    protected $rule;
    public function __construct(
        Coupon $couponModel,
        RuleFactory $rule
    ) {
        $this->couponModel = $couponModel;
        $this->rule = $rule;
    }
    public function execute($observer)
    {
        $event = $observer->getEvent();
        $order = $event->getPayment()->getOrder();
        if ($order->canCancel()) {
            if ($code = $order->getCouponCode()) {
                $coupon =  $this->couponModel->create()->load($code,'coupon_code');
                $coupon->setTimesUsed($coupon->getTimesUsed() - 1);
                $coupon->save();
                if($customerId = $order->getCustomerId()){
                    if($customerCoupon = $this->rule->create()->load($coupon->getId())){
                        $customerCoupon->setTimesUsed($customerCoupon->getTimesUsed() - 1);
                        $customerCoupon->save();
                    }
                }
            }
        }
        $urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');
        return $urlInterface->getCurrentUrl();
    }
}
That's it.
 
I hope it helps.
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.