/   /   /  OroCommerce for developers: coupons batch generator

Note:

For more extensions and themes visit our store

OroCommerce for developers: coupons batch generator


OroCommerce give ability to user to generate multi coupons with many code dynamically ,  so the developer can use this system for generate coupons for example for custom user or specific cases.

The class CouponGenerator is responsible for that Oro\Bundle\PromotionBundle\CouponGeneration\Coupon\CouponGenerator and the function which generate and save is generateAndSave generateAndSave(CouponGenerationOptions $options) and accept as parameters use Oro\Bundle\PromotionBundle\CouponGeneration\Options\CouponGenerationOptions;

So let’s start by inject the  CouponGenerator with DI and using in any of your class ,

  1.  
  2.     ibnab_automatic_coupon.event_listener.myclass:
  3.         class: Ibnab\Bundle\AutomaticCouponBundle\Helper\MyClass
  4.         arguments:
  5.             - '@oro_promotion.coupon_generation.coupon'
  6.  

And
  1.  
  2. <?php
  3. namespace Ibnab\Bundle\AutomaticCouponBundle\Helper;
  4. use Oro\Bundle\PromotionBundle\CouponGeneration\Coupon\CouponGenerator;
  5. use Oro\Bundle\PromotionBundle\CouponGeneration\\Options\CouponGenerationOptions;
  6. class MyClass
  7. {       
  8.     /** @var CouponGenerator */
  9.     protected $couponGenerator;
  10.     /**
  11.      * @param CouponGenerator  $couponGenerator
  12.      *      
  13.      */
  14.     public function __construct(CouponGenerator $couponGenerator)
  15.     {
  16.         $this->couponGenerator = $couponGenerator;
  17.     }
  18. }
  19.  

Let’s start by creating options for this coupon generator 
  1.  
  2.   $couponQty = 10;
  3.   $couponGenerationOptions = new CouponGenerationOptions();
  4.   $couponGenerationOptions->setCouponQuantity($couponQty);
  5.   $couponGenerationOptions->setPromotion($promotion);
  6.   $couponGenerationOptions->setValidFrom((new \DateTime()));
  7.   $couponGenerationOptions->setValidUntil((new \DateTime())->modify('+1 months'));
  8.   $couponGenerationOptions->setOwner($this->getFirstUserBusinessUnit());
  9.   $couponGenerationOptions->setDashesSequence(2);
  10.  

About:
  1.  
  2.   $couponGenerationOptions->setPromotion($promotion);
  3.  

The coupons should related to specific promotion created by user or programmatically let’s trying  load one by id should inject :
- '@oro_entity.doctrine_helper'
  1.  
  2.     public function __construct(DoctrineHelper $doctrineHelper,
  3.     CouponGenerator $couponGenerator)
  4.     {
  5.         $this->doctrineHelper = $doctrineHelper;
  6.         $this->couponGenerator = $couponGenerator;
  7.     } 
  8.  

So now load promotion:

  1.  
  2. $promotionRepository = $this->doctrineHelper->getEntityRepository(‘Oro\\Bundle\\PromotionBundle\\Entity\\Promotion’);
  3. $promotion = $promotionRepository->find(1);
  4.  

you have now your promotion instance , what’s about :

  1.  
  2.  $couponGenerationOptions->setOwner($this->getFirstUserBusinessUnit());
  3.  

let’s add a function to manage owner by get the first Business Unit:
  1.  
  2.     /**
  3.      * @return BusinessUnit
  4.      * @throws \LogicException
  5.      */
  6.     protected function getFirstUserBusinessUnit()
  7.     {
  8.         $users = $this->doctrineHelper->getEntityRepository('Oro\\Bundle\\UserBundle\\Entity\\User')->findBy([], ['id' => 'ASC'], 1);
  9.        if (!$users) {
  10.             throw new \LogicException('There are no users in system');
  11.         }
  12.         $user = reset($users);
  13.         $businessUnits = $user->getBusinessUnits();
  14.         if (!$businessUnits) {
  15.             throw new \LogicException('Can\'t find Business Unit ');
  16.         }
  17.         return isset($businessUnits[0]) ? $businessUnits[0] : null;
  18.     }
  19.  

Done is the time to generate and save you coupons by :

  1.  
  2. $this->couponGenerator->generateAndSave($couponGenerationOptions);
  3.  

So the system well add 10 coupon with different code linked to promotion with id 1.

Comments

IBNAB is a company made of a group of professionals whose work is providing secure open source solutions. Our company strives for reaching magnificent results with each experience and provides professional open source solutions that cover every part of the business process.