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 ,
ibnab_automatic_coupon.event_listener.myclass: class: Ibnab\Bundle\AutomaticCouponBundle\Helper\MyClass arguments: - '@oro_promotion.coupon_generation.coupon'
And
<?php namespace Ibnab\Bundle\AutomaticCouponBundle\Helper; use Oro\Bundle\PromotionBundle\CouponGeneration\Coupon\CouponGenerator; use Oro\Bundle\PromotionBundle\CouponGeneration\\Options\CouponGenerationOptions; class MyClass { /** @var CouponGenerator */ protected $couponGenerator; /** * @param CouponGenerator $couponGenerator * */ public function __construct(CouponGenerator $couponGenerator) { $this->couponGenerator = $couponGenerator; } }
Let’s start by creating options for this coupon generator
$couponQty = 10; $couponGenerationOptions = new CouponGenerationOptions(); $couponGenerationOptions->setCouponQuantity($couponQty); $couponGenerationOptions->setPromotion($promotion); $couponGenerationOptions->setValidFrom((new \DateTime())); $couponGenerationOptions->setValidUntil((new \DateTime())->modify('+1 months')); $couponGenerationOptions->setOwner($this->getFirstUserBusinessUnit()); $couponGenerationOptions->setDashesSequence(2);
About:
$couponGenerationOptions->setPromotion($promotion);
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'
public function __construct(DoctrineHelper $doctrineHelper, CouponGenerator $couponGenerator) { $this->doctrineHelper = $doctrineHelper; $this->couponGenerator = $couponGenerator; }
So now load promotion:
$promotionRepository = $this->doctrineHelper->getEntityRepository(‘Oro\\Bundle\\PromotionBundle\\Entity\\Promotion’); $promotion = $promotionRepository->find(1);
you have now your promotion instance , what’s about :
$couponGenerationOptions->setOwner($this->getFirstUserBusinessUnit());
let’s add a function to manage owner by get the first Business Unit:
/** * @return BusinessUnit * @throws \LogicException */ protected function getFirstUserBusinessUnit() { $users = $this->doctrineHelper->getEntityRepository('Oro\\Bundle\\UserBundle\\Entity\\User')->findBy([], ['id' => 'ASC'], 1); if (!$users) { throw new \LogicException('There are no users in system'); } $businessUnits = $user->getBusinessUnits(); if (!$businessUnits) { throw new \LogicException('Can\'t find Business Unit '); } }
Done is the time to generate and save you coupons by :
$this->couponGenerator->generateAndSave($couponGenerationOptions);
So the system well add 10 coupon with different code linked to promotion with id 1.
Comments