protected function BuyXGetY::selectOrderItems in Commerce Core 8.2
Selects non-free order items that match the given conditions.
Selected order items are sorted by unit price in the specified direction.
Parameters
\Drupal\commerce_order\Entity\OrderItemInterface[] $order_items: The order items.
\Drupal\commerce\ConditionGroup $conditions: The conditions.
string $sort_direction: The sort direction. Use 'ASC' for least expensive first, 'DESC' for most expensive first.
Return value
\Drupal\commerce_order\Entity\OrderItemInterface[] The selected order items, keyed by order item ID.
1 call to BuyXGetY::selectOrderItems()
- 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 514
Class
- BuyXGetY
- Provides the "Buy X Get Y" offer for orders.
Namespace
Drupal\commerce_promotion\Plugin\Commerce\PromotionOfferCode
protected function selectOrderItems(array $order_items, ConditionGroup $conditions, $sort_direction = 'ASC') {
$selected_order_items = [];
foreach ($order_items as $index => $order_item) {
if ($order_item
->getAdjustedTotalPrice()
->isZero()) {
continue;
}
if (!$conditions
->evaluate($order_item)) {
continue;
}
$selected_order_items[$order_item
->id()] = $order_item;
}
uasort($selected_order_items, function (OrderItemInterface $a, OrderItemInterface $b) use ($sort_direction) {
if ($sort_direction == 'ASC') {
$result = $a
->getUnitPrice()
->compareTo($b
->getUnitPrice());
}
else {
$result = $b
->getUnitPrice()
->compareTo($a
->getUnitPrice());
}
return $result;
});
return $selected_order_items;
}