diff --git a/modules/promotion/commerce_promotion.install b/modules/promotion/commerce_promotion.install index 33b231ab..5ecf6489 100644 --- a/modules/promotion/commerce_promotion.install +++ b/modules/promotion/commerce_promotion.install @@ -115,3 +115,38 @@ function commerce_promotion_update_8203() { ]); $entity_definition_update->installFieldStorageDefinition('usage_limit', 'commerce_promotion_coupon', 'commerce_promotion', $storage_definition); } + +/** + * Add the display_name field to promotions. + */ +function commerce_promotion_update_8204() { + $entity_definition_update = \Drupal::entityDefinitionUpdateManager(); + + $storage_definition = BaseFieldDefinition::create('string') + ->setLabel(t('Display Name')) + ->setDescription(t('The promotion name to display to the customer, if not specified, the name will be used.')) + ->setRequired(FALSE) + ->setTranslatable(TRUE) + ->setSettings([ + 'default_value' => '', + 'max_length' => 255, + ]) + ->setDisplayOptions('form', [ + 'type' => 'string_textfield', + 'weight' => 0, + ]) + ->setDisplayConfigurable('form', TRUE); + $entity_definition_update->installFieldStorageDefinition('display_name', 'commerce_promotion', 'commerce_promotion', $storage_definition); + + $storage_definition = BaseFieldDefinition::create('boolean') + ->setLabel(t('Display Entered Coupon')) + ->setDescription(t('If checked, the entered coupon will be displayed to the customer.')) + ->setRequired(FALSE) + ->setTranslatable(TRUE) + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'weight' => 0, + ]) + ->setDisplayConfigurable('form', TRUE); + $entity_definition_update->installFieldStorageDefinition('display_coupon_name', 'commerce_promotion', 'commerce_promotion', $storage_definition); +} diff --git a/modules/promotion/src/Entity/Promotion.php b/modules/promotion/src/Entity/Promotion.php index 71fb6078..08b1a8b2 100644 --- a/modules/promotion/src/Entity/Promotion.php +++ b/modules/promotion/src/Entity/Promotion.php @@ -82,6 +82,36 @@ class Promotion extends CommerceContentEntityBase implements PromotionInterface return $this; } + /** + * {@inheritdoc} + */ + public function getDisplayName() { + return $this->get('display_name')->value; + } + + /** + * {@inheritdoc} + */ + public function setDisplayName($display_name) { + $this->set('display_name', $display_name); + return $this; + } + + /** + * {@inheritdoc} + */ + public function getDisplayCouponName() { + return $this->get('display_coupon_name')->value; + } + + /** + * {@inheritdoc} + */ + public function setDisplayCouponName($display_name) { + $this->set('display_coupon_name', $display_name); + return $this; + } + /** * {@inheritdoc} */ @@ -483,10 +513,10 @@ class Promotion extends CommerceContentEntityBase implements PromotionInterface /** * {@inheritdoc} */ - public function apply(OrderInterface $order) { + public function apply(OrderInterface $order, CouponInterface $coupon = NULL) { $offer = $this->getOffer(); if ($offer->getEntityTypeId() == 'commerce_order') { - $offer->apply($order, $this); + $offer->apply($order, $this, $coupon); } elseif ($offer->getEntityTypeId() == 'commerce_order_item') { $order_item_conditions = array_filter($this->getConditions(), function ($condition) { @@ -497,12 +527,24 @@ class Promotion extends CommerceContentEntityBase implements PromotionInterface // Apply the offer to order items that pass the conditions. foreach ($order->getItems() as $order_item) { if ($order_item_conditions->evaluate($order_item)) { - $offer->apply($order_item, $this); + $offer->apply($order_item, $this, $coupon); } } } } + /** + * {@inheritdoc} + */ + public function preSave(EntityStorageInterface $storage) { + parent::preSave($storage); + + // Display name is required, but just use the name if it is not specified. + if (empty($this->getDisplayName())) { + $this->setDisplayName($this->getName()); + } + } + /** * {@inheritdoc} */ @@ -561,6 +603,32 @@ class Promotion extends CommerceContentEntityBase implements PromotionInterface ->setDisplayConfigurable('view', TRUE) ->setDisplayConfigurable('form', TRUE); + $fields['display_name'] = BaseFieldDefinition::create('string') + ->setLabel(t('Display Name')) + ->setDescription(t('The promotion name to display to the customer, if not specified, the name will be used.')) + ->setRequired(FALSE) + ->setTranslatable(TRUE) + ->setSettings([ + 'default_value' => '', + 'max_length' => 255, + ]) + ->setDisplayOptions('form', [ + 'type' => 'string_textfield', + 'weight' => 0, + ]) + ->setDisplayConfigurable('form', TRUE); + + $fields['display_coupon_name'] = BaseFieldDefinition::create('boolean') + ->setLabel(t('Display Entered Coupon')) + ->setDescription(t('If checked, the entered coupon will be displayed to the customer.')) + ->setRequired(FALSE) + ->setTranslatable(TRUE) + ->setDisplayOptions('form', [ + 'type' => 'boolean_checkbox', + 'weight' => 0, + ]) + ->setDisplayConfigurable('form', TRUE); + $fields['description'] = BaseFieldDefinition::create('string_long') ->setLabel(t('Description')) ->setDescription(t('Additional information about the promotion to show to the customer')) diff --git a/modules/promotion/src/Entity/PromotionInterface.php b/modules/promotion/src/Entity/PromotionInterface.php index c68fd316..68a65a91 100644 --- a/modules/promotion/src/Entity/PromotionInterface.php +++ b/modules/promotion/src/Entity/PromotionInterface.php @@ -34,6 +34,42 @@ interface PromotionInterface extends ContentEntityInterface, EntityStoresInterfa */ public function setName($name); + /** + * Gets the promotion customer facing display name. + * + * @return string + * The promotion display name. + */ + public function getDisplayName(); + + /** + * Sets the promotion customer facing display name. + * + * @param string $display_name + * The promotion display name. + * + * @return $this + */ + public function setDisplayName($display_name); + + /** + * Gets the promotion customer facing display coupon name. + * + * @return string + * The promotion display name. + */ + public function getDisplayCouponName(); + + /** + * Sets the promotion customer facing display coupon name. + * + * @param bool $display_coupon_name + * Display or not flag. + * + * @return $this + */ + public function setDisplayCouponName($display_coupon_name); + /** * Gets the promotion description. * @@ -352,7 +388,10 @@ interface PromotionInterface extends ContentEntityInterface, EntityStoresInterfa * * @param \Drupal\commerce_order\Entity\OrderInterface $order * The order. + * @param \Drupal\commerce_promotion\Entity\CouponInterface $coupon + * The coupon. + * */ - public function apply(OrderInterface $order); + public function apply(OrderInterface $order, CouponInterface $coupon = NULL); } diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php index 8a65e52c..1832d94d 100644 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php @@ -3,6 +3,7 @@ namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer; use Drupal\commerce_order\Adjustment; +use Drupal\commerce_promotion\Entity\CouponInterface; use Drupal\commerce_promotion\Entity\PromotionInterface; use Drupal\Core\Entity\EntityInterface; @@ -20,7 +21,7 @@ class OrderFixedAmountOff extends FixedAmountOffBase { /** * {@inheritdoc} */ - public function apply(EntityInterface $entity, PromotionInterface $promotion) { + public function apply(EntityInterface $entity, PromotionInterface $promotion, CouponInterface $coupon = NULL) { $this->assertEntity($entity); /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $entity; @@ -35,10 +36,19 @@ class OrderFixedAmountOff extends FixedAmountOffBase { $adjustment_amount = $subtotal_price; } + if (!$coupon) { + $label = $promotion->getDisplayName(); + } + else { + $label = $promotion->getDisplayCouponName() + ? $this->t('Coupon @code', ['@code' => $coupon->getCode()]) + : $promotion->getDisplayName(); + } + + $order->addAdjustment(new Adjustment([ 'type' => 'promotion', - // @todo Change to label from UI when added in #2770731. - 'label' => t('Discount'), + 'label' => $label, 'amount' => $adjustment_amount->multiply('-1'), 'source_id' => $promotion->id(), ])); diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.php index b33c5864..e7c889b1 100644 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.php +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemFixedAmountOff.php @@ -3,6 +3,7 @@ namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer; use Drupal\commerce_order\Adjustment; +use Drupal\commerce_promotion\Entity\CouponInterface; use Drupal\commerce_promotion\Entity\PromotionInterface; use Drupal\Core\Entity\EntityInterface; @@ -20,7 +21,7 @@ class OrderItemFixedAmountOff extends FixedAmountOffBase { /** * {@inheritdoc} */ - public function apply(EntityInterface $entity, PromotionInterface $promotion) { + public function apply(EntityInterface $entity, PromotionInterface $promotion, CouponInterface $coupon = NULL) { $this->assertEntity($entity); /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ $order_item = $entity; @@ -34,10 +35,18 @@ class OrderItemFixedAmountOff extends FixedAmountOffBase { $adjustment_amount = $unit_price; } + if (!$coupon) { + $label = $promotion->getDisplayName(); + } + else { + $label = $promotion->getDisplayCouponName() + ? $this->t('Coupon @code', ['@code' => $coupon->getCode()]) + : $promotion->getDisplayName(); + } + $order_item->addAdjustment(new Adjustment([ 'type' => 'promotion', - // @todo Change to label from UI when added in #2770731. - 'label' => t('Discount'), + 'label' => $label, 'amount' => $adjustment_amount->multiply('-1'), 'source_id' => $promotion->id(), ])); diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemPercentageOff.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemPercentageOff.php index 60827753..b873bfde 100644 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemPercentageOff.php +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderItemPercentageOff.php @@ -3,6 +3,7 @@ namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer; use Drupal\commerce_order\Adjustment; +use Drupal\commerce_promotion\Entity\CouponInterface; use Drupal\commerce_promotion\Entity\PromotionInterface; use Drupal\Core\Entity\EntityInterface; @@ -20,17 +21,25 @@ class OrderItemPercentageOff extends PercentageOffBase { /** * {@inheritdoc} */ - public function apply(EntityInterface $entity, PromotionInterface $promotion) { + public function apply(EntityInterface $entity, PromotionInterface $promotion, CouponInterface $coupon = NULL) { $this->assertEntity($entity); /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */ $order_item = $entity; $adjustment_amount = $order_item->getUnitPrice()->multiply($this->getPercentage()); $adjustment_amount = $this->rounder->round($adjustment_amount); + if (!$coupon) { + $label = $promotion->getDisplayName(); + } + else { + $label = $promotion->getDisplayCouponName() + ? $this->t('Coupon @code', ['@code' => $coupon->getCode()]) + : $promotion->getDisplayName(); + } + $order_item->addAdjustment(new Adjustment([ 'type' => 'promotion', - // @todo Change to label from UI when added in #2770731. - 'label' => t('Discount'), + 'label' => $label, 'amount' => $adjustment_amount->multiply('-1'), 'percentage' => $this->getPercentage(), 'source_id' => $promotion->id(), diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderPercentageOff.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderPercentageOff.php index 9574c1f4..a785dc24 100644 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderPercentageOff.php +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderPercentageOff.php @@ -3,6 +3,7 @@ namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer; use Drupal\commerce_order\Adjustment; +use Drupal\commerce_promotion\Entity\CouponInterface; use Drupal\commerce_promotion\Entity\PromotionInterface; use Drupal\Core\Entity\EntityInterface; @@ -20,17 +21,25 @@ class OrderPercentageOff extends PercentageOffBase { /** * {@inheritdoc} */ - public function apply(EntityInterface $entity, PromotionInterface $promotion) { + public function apply(EntityInterface $entity, PromotionInterface $promotion, CouponInterface $coupon = NULL) { $this->assertEntity($entity); /** @var \Drupal\commerce_order\Entity\OrderInterface $order */ $order = $entity; $adjustment_amount = $order->getSubtotalPrice()->multiply($this->getPercentage()); $adjustment_amount = $this->rounder->round($adjustment_amount); + if (!$coupon) { + $label = $promotion->getDisplayName(); + } + else { + $label = $promotion->getDisplayCouponName() + ? $this->t('Coupon @code', ['@code' => $coupon->getCode()]) + : $promotion->getDisplayName(); + } + $order->addAdjustment(new Adjustment([ 'type' => 'promotion', - // @todo Change to label from UI when added in #2770731. - 'label' => t('Discount'), + 'label' => $label, 'amount' => $adjustment_amount->multiply('-1'), 'percentage' => $this->getPercentage(), 'source_id' => $promotion->id(), diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/PromotionOfferInterface.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/PromotionOfferInterface.php index 68c020b6..489b08f9 100644 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/PromotionOfferInterface.php +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/PromotionOfferInterface.php @@ -29,8 +29,10 @@ interface PromotionOfferInterface extends ConfigurablePluginInterface, PluginFor * @param \Drupal\Core\Entity\EntityInterface $entity * The entity. * @param \Drupal\commerce_promotion\Entity\PromotionInterface $promotion - * THe parent promotion. + * The parent promotion. + * @param \Drupal\commerce_promotion\Entity\CouponInterface $coupon + * The coupon. */ - public function apply(EntityInterface $entity, PromotionInterface $promotion); + public function apply(EntityInterface $entity, PromotionInterface $promotion, CouponInterface $coupon = NULL); } diff --git a/modules/promotion/src/PromotionOrderProcessor.php b/modules/promotion/src/PromotionOrderProcessor.php index f4656733..843cda3a 100644 --- a/modules/promotion/src/PromotionOrderProcessor.php +++ b/modules/promotion/src/PromotionOrderProcessor.php @@ -39,7 +39,7 @@ class PromotionOrderProcessor implements OrderProcessorInterface { foreach ($coupons as $index => $coupon) { $promotion = $coupon->getPromotion(); if ($coupon->available($order) && $promotion->applies($order)) { - $promotion->apply($order); + $promotion->apply($order, $coupon); } else { // The promotion is no longer available (end date, usage, etc).