Skip to content

Commit c93123f

Browse files
mglamanmglaman
authored andcommitted
Issue #2888231 by mglaman: Attribute widget should respect if order item has a pre-selected purchased entity
1 parent deba412 commit c93123f

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

modules/cart/tests/src/Functional/AddToCartFormTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testVariationCanonicalLinkAddToCartForm() {
7474

7575
$variation1 = $this->createEntity('commerce_product_variation', [
7676
'type' => 'default',
77-
'sku' => $this->randomMachineName(),
77+
'sku' => 'not-canonical',
7878
'price' => [
7979
'number' => '5.00',
8080
'currency_code' => 'USD',

modules/product/src/Plugin/Field/FieldWidget/ProductVariationAttributesWidget.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,17 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
128128
}
129129
// Otherwise load from the current context.
130130
else {
131-
$selected_variation = $this->variationStorage->loadFromContext($product);
132-
// The returned variation must also be enabled.
133-
if (!in_array($selected_variation, $variations)) {
134-
$selected_variation = reset($variations);
131+
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
132+
$order_item = $items->getEntity();
133+
if (!$order_item->isNew()) {
134+
$selected_variation = $order_item->getPurchasedEntity();
135+
}
136+
else {
137+
$selected_variation = $this->variationStorage->loadFromContext($product);
138+
// The returned variation must also be enabled.
139+
if (!in_array($selected_variation, $variations)) {
140+
$selected_variation = reset($variations);
141+
}
135142
}
136143
}
137144
$element['variation'] = [

0 commit comments

Comments
 (0)