Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Usage.php
Go to the documentation of this file.
1 <?php
7 
14 {
20  protected function _construct()
21  {
22  $this->_init('salesrule_coupon_usage', 'coupon_id');
23  }
24 
32  public function updateCustomerCouponTimesUsed($customerId, $couponId)
33  {
34  $connection = $this->getConnection();
35  $select = $connection->select();
36  $select->from(
37  $this->getMainTable(),
38  ['times_used']
39  )->where(
40  'coupon_id = :coupon_id'
41  )->where(
42  'customer_id = :customer_id'
43  );
44 
45  $timesUsed = $connection->fetchOne($select, [':coupon_id' => $couponId, ':customer_id' => $customerId]);
46 
47  if ($timesUsed > 0) {
48  $this->getConnection()->update(
49  $this->getMainTable(),
50  ['times_used' => $timesUsed + 1],
51  ['coupon_id = ?' => $couponId, 'customer_id = ?' => $customerId]
52  );
53  } else {
54  $this->getConnection()->insert(
55  $this->getMainTable(),
56  ['coupon_id' => $couponId, 'customer_id' => $customerId, 'times_used' => 1]
57  );
58  }
59  }
60 
69  public function loadByCustomerCoupon(\Magento\Framework\DataObject $object, $customerId, $couponId)
70  {
71  $connection = $this->getConnection();
72  if ($connection && $couponId && $customerId) {
73  $select = $connection->select()->from(
74  $this->getMainTable()
75  )->where(
76  'customer_id =:customet_id'
77  )->where(
78  'coupon_id = :coupon_id'
79  );
80  $data = $connection->fetchRow($select, [':coupon_id' => $couponId, ':customet_id' => $customerId]);
81  if ($data) {
82  $object->setData($data);
83  }
84  }
85  if ($object instanceof \Magento\Framework\Model\AbstractModel) {
86  $this->_afterLoad($object);
87  }
88  return $this;
89  }
90 }
loadByCustomerCoupon(\Magento\Framework\DataObject $object, $customerId, $couponId)
Definition: Usage.php:69
updateCustomerCouponTimesUsed($customerId, $couponId)
Definition: Usage.php:32
$connection
Definition: bulk.php:13
_afterLoad(\Magento\Framework\Model\AbstractModel $object)
Definition: AbstractDb.php:641