3

I need to generate coupon code for a specific rule after the order is placed. I am checking the sale-rule module. It will helped me if anyone know which class is actually I should use.

  • How about your issue? – Khoa TruongDinh Feb 19 '17 at 11:03
  • @KhoaTruongDinh I post my solution below – iamkdev Feb 19 '17 at 11:57
3

We need to know:

  • \Magento\SalesRule\Api\CouponManagementInterface - \Magento\SalesRule\Model\Service\CouponManagementService to generate coupon code.

  • \Magento\SalesRule\Api\Data\CouponGenerationSpecInterface - \Magento\SalesRule\Model\Data\CouponGenerationSpec to define a coupon code generator.

Remember to inject these classes in your constructor.

/** @var \Magento\SalesRule\Model\Service\CouponManagementService $couponManagement **/

// Define a coupon code generator
// Get the rule in question
// Look at \Magento\SalesRule\Model\Data\CouponGenerationSpec $generator for options

$generator->setRuleId($ruleId)

if( !empty($parameters['format']) ){
  switch( strtolower($parameters['format']) ){
    case 'alphanumeric':
    case 'alphanum':
      $generator->setFormat(\Magento\SalesRule\HelperCoupon::COUPON_FORMAT_ALPHANUMERIC);
      break;
    case 'alphabetical':
    case 'alpha':
      $generator->setFormat(\Magento\SalesRule\HelperCoupon::COUPON_FORMAT_ALPHABETICAL);
      break;
    case 'numeric':
    case 'num':
      $generator->setFormat(\Magento\SalesRule\HelperCoupon::COUPON_FORMAT_NUMERIC );
      break;
  }
}

$generator->setLength(!empty($parameters['length'])? (int) $parameters['length'] : 6);
$generator->setPrefix(!empty($parameters['prefix'])? $parameters['prefix'] : '');
$generator->setSuffix(!empty($parameters['suffix'])? $parameters['suffix'] : '');

// Set the generator, and coupon type so it's able to generate
$couponManagement->generate($generator);
| improve this answer | |
  • Works, also had to do "setQuantity()" & "setDelimiterAtEvery()" Also "setFormat('alphanum')" – 00-BBB Feb 20 '19 at 14:37
1

The below code works for me.

$code = $generator->generateCode();
$expirationDate = strtotime("+30 day");      

$coupon = $this->_objectManager->create('Magento\SalesRule\Model\Coupon');        
$coupon->setId(null)->setRuleId($ruleId)->setUsageLimit(1000)->setUsagePerCustomer(10000)->setExpirationDate($expirationDate)->setType(1)->setCode($code)->save();
| improve this answer | |
  • 2
    Where does "$generator" come from? – 00-BBB Feb 20 '19 at 14:09
0

I used the following to generate random code only.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$couponGenerator = $objectManager->get('\Magento\SalesRule\Model\Coupon\Codegenerator'); // magnento coupon code generator

$couponHelper = $objectManager->get('\Magento\SalesRule\Helper\Coupon'); //coupon code helper to ger format
$couponGenerator->setFormat($couponHelper::COUPON_FORMAT_ALPHANUMERIC); // set format or random code

$couponGenerator->setLength(8); // length of coupon code upto 32
echo $couponCode = $couponGenerator->generateCode(); //generate code

Output - zO544NsV
| improve this answer | |

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.