diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedAmountOffBase.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedAmountOffBase.php new file mode 100644 index 0000000..143db37 --- /dev/null +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedAmountOffBase.php @@ -0,0 +1,69 @@ + 0, + ] + parent::defaultConfiguration(); + } + + /** + * Gets the fixed amount, as a decimal, negated. + * + * @return string + * The amount. + */ + public function getAmount() { + return (string) $this->configuration['amount']; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form += parent::buildConfigurationForm($form, $form_state); + + $form['amount'] = [ + '#type' => 'commerce_number', + '#title' => $this->t('Amount'), + '#default_value' => $this->configuration['amount'], + '#maxlength' => 255, + '#required' => TRUE, + '#min' => 0, + '#size' => 5, + ]; + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValue($form['#parents']); + if (empty($values['amount'])) { + $form_state->setError($form, $this->t('Fixed amount cannot be empty.')); + } + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $values = $form_state->getValue($form['#parents']); + $this->configuration['amount'] = (string) ($values['amount']); + parent::submitConfigurationForm($form, $form_state); + } + +} diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedOffBase.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedOffBase.php deleted file mode 100644 index 325d78b..0000000 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/FixedOffBase.php +++ /dev/null @@ -1,69 +0,0 @@ - 0, - ] + parent::defaultConfiguration(); - } - - /** - * Gets the fixed amount, as a decimal, negated. - * - * @return string - * The amount. - */ - public function getAmount() { - return (string) $this->configuration['amount']; - } - - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $form += parent::buildConfigurationForm($form, $form_state); - - $form['amount'] = [ - '#type' => 'commerce_number', - '#title' => $this->t('Amount'), - '#default_value' => $this->configuration['amount'], - '#maxlength' => 255, - '#required' => TRUE, - '#min' => 0, - '#size' => 5, - ]; - - return $form; - } - - /** - * {@inheritdoc} - */ - public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { - $values = $form_state->getValue($form['#parents']); - if (empty($values['amount'])) { - $form_state->setError($form, $this->t('Fixed amount cannot be empty.')); - } - } - - /** - * {@inheritdoc} - */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { - $values = $form_state->getValue($form['#parents']); - $this->configuration['amount'] = (string) ($values['amount']); - parent::submitConfigurationForm($form, $form_state); - } - -} diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php new file mode 100644 index 0000000..f09b3d9 --- /dev/null +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedAmountOff.php @@ -0,0 +1,38 @@ +getOrder(); + $order_total_price = $order->getTotalPrice(); + $currency_code = $order_total_price->getCurrencyCode(); + // If fixed discount price is greater than order total price, + // make discount price same to order total price. + // So final total won't be negative. + if ($order_total_price < $this->getAmount()) { + $discount_price = new Price($order_total_price, $currency_code); + } + else { + $discount_price = new Price($this->getAmount(), $currency_code); + } + $adjustment_amount = $this->rounder->round($discount_price); + $this->applyAdjustment($order, $adjustment_amount); + } + +} diff --git a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedOff.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedOff.php deleted file mode 100644 index 3dbc54f..0000000 --- a/modules/promotion/src/Plugin/Commerce/PromotionOffer/OrderFixedOff.php +++ /dev/null @@ -1,38 +0,0 @@ -getOrder(); - $order_total_price = $order->getTotalPrice(); - $currency_code = $order_total_price->getCurrencyCode(); - // If fixed discount price is greater than order total price, - // make discount price same to order total price. - // So final total won't be negative. - if ($order_total_price < $this->getAmount()) { - $discount_price = new Price($order_total_price, $currency_code); - } - else { - $discount_price = new Price($this->getAmount(), $currency_code); - } - $adjustment_amount = $this->rounder->round($discount_price); - $this->applyAdjustment($order, $adjustment_amount); - } - -} diff --git a/modules/promotion/tests/src/Kernel/PromotionOfferTest.php b/modules/promotion/tests/src/Kernel/PromotionOfferTest.php index fa4de87..d0f436d 100644 --- a/modules/promotion/tests/src/Kernel/PromotionOfferTest.php +++ b/modules/promotion/tests/src/Kernel/PromotionOfferTest.php @@ -129,9 +129,9 @@ class PromotionOfferTest extends CommerceKernelTestBase { } /** - * Tests order fixed off. + * Tests order fixed amount off. */ - public function testOrderFixedOff() { + public function testOrderFixedAmountOff() { // Use addOrderItem so the total is calculated. $order_item = OrderItem::create([ 'type' => 'test', @@ -152,7 +152,7 @@ class PromotionOfferTest extends CommerceKernelTestBase { 'stores' => [$this->store->id()], 'status' => TRUE, 'offer' => [ - 'target_plugin_id' => 'commerce_promotion_order_fixed_off', + 'target_plugin_id' => 'commerce_promotion_order_fixed_amount_off', 'target_plugin_configuration' => [ 'amount' => '1.0', ], @@ -167,9 +167,7 @@ class PromotionOfferTest extends CommerceKernelTestBase { $promotion->apply($this->order); $this->assertEquals(1, count($this->order->getAdjustments())); - $this->assertEquals(new Price('39.00', 'USD'), $this->order->getTotalPrice - ()); - + $this->assertEquals(new Price('39.00', 'USD'), $this->order->getTotalPrice()); } /**