diff -u b/modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php b/modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php --- b/modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php +++ b/modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php @@ -89,8 +89,7 @@ $plugin_definition, $container->get('commerce_price.rounder'), $container->get('commerce_order.price_splitter'), - $container->get('plugin.manager.commerce_condition'), - $container->get('commerce_cart.cart_manager') + $container->get('plugin.manager.commerce_condition') ); } @@ -269,7 +268,6 @@ } $get_conditions = $this->buildConditionGroup($this->configuration['get_conditions']); - // @todo Make this behavior optional based on an additional promotion // setting. if ($get_purchasable_entity = $this->findSinglePurchasableEntity($get_conditions)) { @@ -410,9 +408,13 @@ } /** + * Find the configured purchasable entity amongst the given conditions. + * * @param \Drupal\commerce\ConditionGroup $conditions + * The condition group. * * @return \Drupal\commerce\PurchasableEntityInterface|null + * The purchasable entity, NULL if not found in the conditions. */ protected function findSinglePurchasableEntity(ConditionGroup $conditions) { foreach ($conditions->getConditions() as $condition) { @@ -429,10 +431,18 @@ } /** + * 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. + * * @param \Drupal\commerce\PurchasableEntityInterface $get_purchasable_entity + * The "get" purchasable entity. * @param \Drupal\commerce_order\Entity\OrderItemInterface[] $order_items + * The order items. * - * @return \Drupal\commerce_order\Entity\OrderItemInterface|null + * @return \Drupal\commerce_order\Entity\OrderItemInterface + * An order item referencing the given purchasable entity. */ protected function findOrCreateOrderItem(PurchasableEntityInterface $get_purchasable_entity, array $order_items) { foreach ($order_items as $order_item) { @@ -452,10 +462,15 @@ } /** + * Calculates the expected get quantity. + * * @param array $buy_quantities + * An array of buy quantities. * @param \Drupal\commerce_order\Entity\OrderItemInterface $order_item + * The order item. * * @return string + * The expected get quantity. */ protected function calculateExpectedGetQuantity($buy_quantities, OrderItemInterface $order_item) { $expected_get_quantity = '0';