protected function BuyXGetY::findOrCreateOrderItem in Commerce Core 8.2
Attempt to find the given purchasable entity amongst the given order items.
If the given purchasable entity isn't referenced by any order item, create an order item referencing it so we can automatically add it to the order.
Parameters
\Drupal\commerce\PurchasableEntityInterface $get_purchasable_entity: The "get" purchasable entity.
\Drupal\commerce_order\Entity\OrderItemInterface[] $order_items: The order items.
Return value
\Drupal\commerce_order\Entity\OrderItemInterface An order item referencing the given purchasable entity.
1 call to BuyXGetY::findOrCreateOrderItem()
- BuyXGetY::apply in modules/
promotion/ src/ Plugin/ Commerce/ PromotionOffer/ BuyXGetY.php - Applies the offer to the given entity.
File
- modules/
promotion/ src/ Plugin/ Commerce/ PromotionOffer/ BuyXGetY.php, line 576
Class
- BuyXGetY
- Provides the "Buy X Get Y" offer for orders.
Namespace
Drupal\commerce_promotion\Plugin\Commerce\PromotionOfferCode
protected function findOrCreateOrderItem(PurchasableEntityInterface $get_purchasable_entity, array $order_items) {
foreach ($order_items as $order_item) {
$purchased_entity = $order_item
->getPurchasedEntity();
if ($purchased_entity
->getEntityTypeId() == $get_purchasable_entity
->getEntityTypeId() && $purchased_entity
->id() == $get_purchasable_entity
->id()) {
return $order_item;
}
}
/** @var \Drupal\commerce_order\OrderItemStorageInterface $storage */
$storage = $this->entityTypeManager
->getStorage('commerce_order_item');
$order_item = $storage
->createFromPurchasableEntity($get_purchasable_entity, [
'quantity' => 0,
]);
return $order_item;
}