daily pastebin goal
42%
SHARE
TWEET

Untitled

a guest Oct 13th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
  3.     <event name="email_order_set_template_vars_before">
  4.         <observer name="add_Custom_variable_to_Order"
  5.             instance="[Vendor][ModuleName]ObserverObserverforAddCustomVariable" />
  6.     </event>
  7. </config>
  8.    
  9. <?php
  10. namespace [Vendor][ModuleName]Observer;
  11. use MagentoFrameworkEventObserverInterface;
  12. use MagentoFrameworkAppRequestDataPersistorInterface;
  13. use MagentoFrameworkAppObjectManager;
  14.  
  15. class ObserverforAddCustomVariable implements ObserverInterface
  16. {
  17.  
  18.     protected $ruleFactory
  19.  
  20.     public function __construct(MagentoSalesRuleModelRuleFactory $ruleFactory) {
  21.         $this->rulesFactory = $ruleFactory;
  22.     }
  23.  
  24.     /**
  25.      *
  26.      * @param MagentoFrameworkEventObserver $observer
  27.      * @return void
  28.      */
  29.     public function execute(MagentoFrameworkEventObserver $observer)
  30.     {
  31.         /** @var MagentoFrameworkAppActionAction $controller */
  32.         $transport = $observer->getTransport();
  33.         $couponCode = $this->createOneCoupon();
  34.         if($couponCode){
  35.             $transport['free-coupon'] = $couponCode;
  36.         }
  37.     }
  38.         protected function createOneCoupon()
  39.     {
  40.         $ruleModel = $this->ruleFactory->create();
  41.         $ruleModel->load({RulesID});
  42.          try {
  43.                 $data = array(
  44.                         'rule_id' => 1,
  45.                         'qty' => 1,
  46.                         'length' => '12',
  47.                         'format' => 'alphanum',
  48.                         'prefix' => 'free-shipping',
  49.                         'suffix' => '',
  50.                         'dash'=>0
  51.                         );
  52.  
  53.  
  54.                 /** @var $generator MagentoSalesRuleModelCouponMassgenerator */
  55.                 $generator = $this->_objectManager->get('MagentoSalesRuleModelCouponMassgenerator');
  56.                 if (!$generator->validateData($data)) {
  57.                    return false;
  58.                 } else {
  59.                     $generator->setData($data);
  60.                     $generator->generatePool();
  61.                     $generated = $generator->getGeneratedCount();
  62.                     $codes = $generator->getGeneratedCodes();
  63.                     return $codes[0];
  64.  
  65.                 }
  66.             } catch (MagentoFrameworkExceptionLocalizedException $e) {
  67.                 $this->_objectManager->get('PsrLogLoggerInterface')->critical($e);
  68.                 return false;
  69.             } catch (Exception $e) {
  70.  
  71.                 $this->_objectManager->get('PsrLogLoggerInterface')->critical($e);
  72.                 return false;
  73.             }
  74.     }
  75.  
  76. }
RAW Paste Data
Top