Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Coupon.php
Go to the documentation of this file.
1 <?php
7 
9 
17 {
23  protected function _construct()
24  {
25  $this->_init('salesrule_coupon', 'coupon_id');
26  $this->addUniqueField(['field' => 'code', 'title' => __('Coupon with the same code')]);
27  }
28 
35  public function _beforeSave(AbstractModel $object)
36  {
37  if (!$object->getExpirationDate()) {
38  $object->setExpirationDate(null);
39  } elseif ($object->getExpirationDate() instanceof \DateTimeInterface) {
40  $object->setExpirationDate(
41  $object->getExpirationDate()->format('Y-m-d H:i:s')
42  );
43  }
44 
45  // maintain single primary coupon per rule
46  $object->setIsPrimary($object->getIsPrimary() ? 1 : null);
47 
48  return parent::_beforeSave($object);
49  }
50 
59  public function loadPrimaryByRule(\Magento\SalesRule\Model\Coupon $object, $rule)
60  {
61  $connection = $this->getConnection();
62 
63  if ($rule instanceof \Magento\SalesRule\Model\Rule) {
64  $ruleId = $rule->getId();
65  } else {
66  $ruleId = (int)$rule;
67  }
68 
69  $select = $connection->select()->from(
70  $this->getMainTable()
71  )->where(
72  'rule_id = :rule_id'
73  )->where(
74  'is_primary = :is_primary'
75  );
76 
77  $data = $connection->fetchRow($select, [':rule_id' => $ruleId, ':is_primary' => 1]);
78 
79  if (!$data) {
80  return false;
81  }
82 
83  $object->setData($data);
84 
85  $this->_afterLoad($object);
86  return true;
87  }
88 
95  public function exists($code)
96  {
97  $connection = $this->getConnection();
98  $select = $connection->select();
99  $select->from($this->getMainTable(), 'code');
100  $select->where('code = :code');
101 
102  if ($connection->fetchOne($select, ['code' => $code]) === false) {
103  return false;
104  }
105  return true;
106  }
107 
114  public function updateSpecificCoupons(\Magento\SalesRule\Model\Rule $rule)
115  {
116  if (!$rule || !$rule->getId() || !$rule->hasDataChanges()) {
117  return $this;
118  }
119 
120  $updateArray = [];
121  if ($rule->dataHasChangedFor('uses_per_coupon')) {
122  $updateArray['usage_limit'] = $rule->getUsesPerCoupon();
123  }
124 
125  if ($rule->dataHasChangedFor('uses_per_customer')) {
126  $updateArray['usage_per_customer'] = $rule->getUsesPerCustomer();
127  }
128 
129  $ruleNewDate = new \DateTime($rule->getToDate());
130  $ruleOldDate = new \DateTime($rule->getOrigData('to_date'));
131 
132  if ($ruleNewDate != $ruleOldDate) {
133  $updateArray['expiration_date'] = $rule->getToDate();
134  }
135 
136  if (!empty($updateArray)) {
137  $this->getConnection()->update(
138  $this->getTable('salesrule_coupon'),
139  $updateArray,
140  ['rule_id = ?' => $rule->getId()]
141  );
142  }
143 
144  return $this;
145  }
146 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
loadPrimaryByRule(\Magento\SalesRule\Model\Coupon $object, $rule)
Definition: Coupon.php:59
__()
Definition: __.php:13
updateSpecificCoupons(\Magento\SalesRule\Model\Rule $rule)
Definition: Coupon.php:114
_beforeSave(AbstractModel $object)
Definition: Coupon.php:35
$connection
Definition: bulk.php:13
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:641
$code
Definition: info.phtml:12