View source
<?php
namespace Drupal\commerce_promotion\Entity;
use Drupal\commerce\Entity\CommerceContentEntityBase;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
class Coupon extends CommerceContentEntityBase implements CouponInterface {
use EntityChangedTrait;
protected function urlRouteParameters($rel) {
$uri_route_parameters = parent::urlRouteParameters($rel);
$uri_route_parameters['commerce_promotion'] = $this
->getPromotionId();
return $uri_route_parameters;
}
public function getPromotion() {
return $this
->getTranslatedReferencedEntity('promotion_id');
}
public function getPromotionId() {
return $this
->get('promotion_id')->target_id;
}
public function getCode() {
return $this
->get('code')->value;
}
public function setCode($code) {
$this
->set('code', $code);
return $this;
}
public function getCreatedTime() {
return $this
->get('created')->value;
}
public function setCreatedTime($timestamp) {
$this
->set('created', $timestamp);
return $this;
}
public function getUsageLimit() {
return $this
->get('usage_limit')->value;
}
public function setUsageLimit($usage_limit) {
$this
->set('usage_limit', $usage_limit);
return $this;
}
public function getCustomerUsageLimit() {
return $this
->get('usage_limit_customer')->value;
}
public function setCustomerUsageLimit($usage_limit_customer) {
$this
->set('usage_limit_customer', $usage_limit_customer);
return $this;
}
public function isEnabled() {
return (bool) $this
->getEntityKey('status');
}
public function setEnabled($enabled) {
$this
->set('status', (bool) $enabled);
return $this;
}
public function available(OrderInterface $order) {
if (!$this
->isEnabled()) {
return FALSE;
}
if (!$this
->getPromotion()
->available($order)) {
return FALSE;
}
$usage_limit = $this
->getUsageLimit();
$usage_limit_customer = $this
->getCustomerUsageLimit();
if (!$usage_limit && !$usage_limit_customer) {
return TRUE;
}
$usage = \Drupal::service('commerce_promotion.usage');
if ($usage_limit && $usage_limit <= $usage
->loadByCoupon($this)) {
return FALSE;
}
if ($usage_limit_customer) {
$email = $order
->getEmail();
if ($email && $usage_limit_customer <= $usage
->loadByCoupon($this, $email)) {
return FALSE;
}
}
return TRUE;
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$promotion = $this
->getPromotion();
if ($promotion && !$promotion
->hasCoupon($this)) {
$promotion
->addCoupon($this);
$promotion
->save();
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
$usage = \Drupal::service('commerce_promotion.usage');
$usage
->deleteByCoupon($entities);
foreach ($entities as $coupon) {
$coupons_id[] = $coupon
->id();
}
$promotions = \Drupal::entityTypeManager()
->getStorage('commerce_promotion')
->loadByProperties([
'coupons' => $coupons_id,
]);
foreach ($promotions as $promotion) {
foreach ($entities as $entity) {
$promotion
->removeCoupon($entity);
}
$promotion
->save();
}
}
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['promotion_id'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Promotion'))
->setDescription(t('The parent promotion.'))
->setSetting('target_type', 'commerce_promotion')
->setReadOnly(TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['code'] = BaseFieldDefinition::create('string')
->setLabel(t('Coupon code'))
->setDescription(t('The unique, machine-readable identifier for a coupon.'))
->addConstraint('CouponCode')
->setSettings([
'max_length' => 50,
'text_processing' => 0,
])
->setDefaultValue('')
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -4,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -4,
])
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Created'))
->setDescription(t('The time when the coupon was created.'));
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
->setDescription(t('The time when the coupon was last edited.'))
->setDisplayConfigurable('view', TRUE);
$fields['usage_limit'] = BaseFieldDefinition::create('integer')
->setLabel(t('Usage limit'))
->setDescription(t('The maximum number of times the coupon can be used. 0 for unlimited.'))
->setDefaultValue(0)
->setDisplayOptions('form', [
'type' => 'commerce_usage_limit',
'weight' => 4,
]);
$fields['usage_limit_customer'] = BaseFieldDefinition::create('integer')
->setLabel(t('Customer usage limit'))
->setDescription(t('The maximum number of times the coupon can be used by a customer. 0 for unlimited.'))
->setDefaultValue(0)
->setDisplayOptions('form', [
'type' => 'commerce_usage_limit',
'weight' => 4,
]);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Status'))
->setDescription(t('Whether the coupon is enabled.'))
->setDefaultValue(TRUE)
->setRequired(TRUE)
->setSettings([
'on_label' => t('Enabled'),
'off_label' => t('Disabled'),
])
->setDisplayOptions('form', [
'type' => 'options_buttons',
'weight' => 0,
]);
return $fields;
}
}