public function ProductBundleItemsWidget::addBundleItemSelections in Commerce Product Bundle 8
Entity builder: updates the order item with the bundle item selections.
Parameters
string $entity_type: The entity type.
\Drupal\commerce_order\Entity\OrderItemInterface $order_item: The entity updated with the submitted values.
array $form: The complete form array.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
File
- src/
Plugin/ Field/ FieldWidget/ ProductBundleItemsWidget.php, line 400
Class
- ProductBundleItemsWidget
- Plugin implementation of the 'commerce_product_bundle_items' widget.
Namespace
Drupal\commerce_product_bundle\Plugin\Field\FieldWidgetCode
public function addBundleItemSelections($entity_type, OrderItemInterface $order_item, array $form, FormStateInterface $form_state) {
$bundle_order_items = [];
foreach ($form_state
->getValue('purchased_entity')[0]['bundle_items'] as $item => $selection) {
// Cast values to string like OrderItem does for other fields.
// Value type (string) and order are required when matching order items.
// @see \Drupal\commerce_cart\OrderItemMatcher
/** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
$variation = $this->variationStorage
->load($selection['variation']);
/** @var \Drupal\commerce_product_bundle\Entity\BundleItemInterface $bundle_item */
$bundle_item = $selection['bundle_item'];
$bundle_order_item = $this->bundleItemOrderItemStorage
->create([
'title' => $bundle_item
->getTitle(),
'bundle_item' => $bundle_item,
'purchased_entity' => $variation,
'unit_price' => $bundle_item
->getUnitPrice(),
'quantity' => (string) $selection['qty'],
]);
$bundle_order_items[] = $bundle_order_item;
}
$order_item
->set('bundle_item_order_items', $bundle_order_items);
}