0

I am trying to create auto generated coupon codes for a specific rule programmatically. But facing problems continuously. Can you please help me out?

1 Answer 1

0
    use Magento\Framework\App\Bootstrap;
    require __DIR__ . '/../app/bootstrap.php';

    $bootstrap = Bootstrap::create(BP, $_SERVER);

    $obj = $bootstrap->getObjectManager();

    $state = $obj->get('Magento\Framework\App\State');
    $state->setAreaCode('frontend');
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 


    $rule_id = 11 ;
    $rule=$objectManager->create('Magento\SalesRule\Model\Rule')->load($rule_id); 
    $isactive = $rule->getIsActive(); 

    if($isactive){

        $expirationDate = strtotime("+30 day");
        $code ="FEOMCEE12";
        $coupon = $objectManager->create('Magento\SalesRule\Model\Coupon');        
        $coupon->setId(null)->setRuleId($rule_id)->setType(1)->setCode($code)->save();

    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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