Skip to content

Commit ed36542

Browse files
mglamanbojanz
mglaman
authored andcommitted
Issue #2792653 by mglaman, niko-, bmcclure, Dom., xSDx, padma28, gauravjeet, chishah92, rajeshwari10, a.dmitriiev, andypost, steveoliver, markoshust, bojanz, LotharDesmet, nikathone: Checkout coupon pane implementation
1 parent 1fb7bc6 commit ed36542

File tree

13 files changed

+803
-245
lines changed

13 files changed

+803
-245
lines changed

modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php

+5
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ public function buildForm(array $form, FormStateInterface $form_state, $step_id
234234
if (empty($step_id)) {
235235
throw new \InvalidArgumentException('The $step_id cannot be empty.');
236236
}
237+
if ($form_state->isRebuilding()) {
238+
// Ensure a fresh order, in case an ajax submit has modified it.
239+
$order_storage = $this->entityTypeManager->getStorage('commerce_order');
240+
$this->order = $order_storage->load($this->order->id());
241+
}
237242

238243
$steps = $this->getVisibleSteps();
239244
$form['#tree'] = TRUE;

modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowWithPanesBase.php

+8
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,14 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
533533
*/
534534
public function buildForm(array $form, FormStateInterface $form_state, $step_id = NULL) {
535535
$form = parent::buildForm($form, $form_state, $step_id);
536+
if ($form_state->isRebuilding()) {
537+
// The order reference on the panes might be outdated due to
538+
// the form cache, so update the order manually once the
539+
// parent class reloads it.
540+
foreach ($this->panes as $pane_id => $pane) {
541+
$this->panes[$pane_id] = $pane->setOrder($this->order);
542+
}
543+
}
536544

537545
foreach ($this->getVisiblePanes($step_id) as $pane_id => $pane) {
538546
$form[$pane_id] = [

modules/checkout/src/Plugin/Commerce/CheckoutPane/CheckoutPaneBase.php

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;
44

55
use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface;
6+
use Drupal\commerce_order\Entity\OrderInterface;
67
use Drupal\Component\Utility\NestedArray;
78
use Drupal\Core\Entity\EntityTypeManagerInterface;
89
use Drupal\Core\Form\FormStateInterface;
@@ -137,6 +138,14 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
137138
*/
138139
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {}
139140

141+
/**
142+
* {@inheritdoc}
143+
*/
144+
public function setOrder(OrderInterface $order) {
145+
$this->order = $order;
146+
return $this;
147+
}
148+
140149
/**
141150
* {@inheritdoc}
142151
*/

modules/checkout/src/Plugin/Commerce/CheckoutPane/CheckoutPaneInterface.php

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;
44

5+
use Drupal\commerce_order\Entity\OrderInterface;
56
use Drupal\Component\Plugin\ConfigurablePluginInterface;
67
use Drupal\Component\Plugin\DerivativeInspectionInterface;
78
use Drupal\Component\Plugin\PluginInspectionInterface;
@@ -15,6 +16,18 @@
1516
*/
1617
interface CheckoutPaneInterface extends ConfigurablePluginInterface, PluginFormInterface, PluginInspectionInterface, DerivativeInspectionInterface {
1718

19+
/**
20+
* Sets the current order.
21+
*
22+
* Used to keep the pane order in sync with the checkout flow order.
23+
*
24+
* @param \Drupal\commerce_order\Entity\OrderInterface $order
25+
* The order.
26+
*
27+
* @return $this
28+
*/
29+
public function setOrder(OrderInterface $order);
30+
1831
/**
1932
* Gets the pane ID.
2033
*

modules/promotion/commerce_promotion.module

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ function commerce_promotion_theme() {
2020
'commerce_promotion_form' => [
2121
'render element' => 'form',
2222
],
23+
'commerce_coupon_redemption_form' => [
24+
'render element' => 'form',
25+
],
2326
];
2427
}
2528

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
commerce_checkout.commerce_checkout_pane.coupon_redemption:
2+
type: commerce_checkout_pane_configuration
3+
mapping:
4+
allow_multiple:
5+
type: boolean
6+
label: 'Allow multiple coupons'

0 commit comments

Comments
 (0)