Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddSalesRuleNameToOrderObserver.php
Go to the documentation of this file.
1 <?php
7 
10 
12 {
16  protected $_ruleFactory;
17 
21  protected $_coupon;
22 
27  public function __construct(
28  \Magento\SalesRule\Model\RuleFactory $ruleFactory,
29  \Magento\SalesRule\Model\Coupon $coupon
30  ) {
31  $this->_ruleFactory = $ruleFactory;
32  $this->_coupon = $coupon;
33  }
34 
41  public function execute(EventObserver $observer)
42  {
43  $order = $observer->getOrder();
44  $couponCode = $order->getCouponCode();
45 
46  if (empty($couponCode)) {
47  return $this;
48  }
49 
50  $this->_coupon->loadByCode($couponCode);
51  $ruleId = $this->_coupon->getRuleId();
52 
53  if (empty($ruleId)) {
54  return $this;
55  }
56 
58  $rule = $this->_ruleFactory->create()->load($ruleId);
59  $order->setCouponRuleName($rule->getName());
60 
61  return $this;
62  }
63 }
$coupon
$order
Definition: order.php:55
__construct(\Magento\SalesRule\Model\RuleFactory $ruleFactory, \Magento\SalesRule\Model\Coupon $coupon)