You are here

public function PromotionOrderProcessor::process in Commerce Core 8.2

Processes an order.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

Overrides OrderProcessorInterface::process

File

modules/promotion/src/PromotionOrderProcessor.php, line 99

Class

PromotionOrderProcessor
Applies promotions to orders during the order refresh process.

Namespace

Drupal\commerce_promotion

Code

public function process(OrderInterface $order) {

  // Remove coupons that are no longer valid (due to availability/conditions.)
  $coupons_field_list = $order
    ->get('coupons');
  $constraints = $coupons_field_list
    ->validate();

  /** @var \Symfony\Component\Validator\ConstraintViolationInterface $constraint */
  foreach ($constraints as $constraint) {
    list($delta, $property_name) = explode('.', $constraint
      ->getPropertyPath());
    $coupons_field_list
      ->removeItem($delta);
  }
  $content_langcode = $this->languageManager
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();

  /** @var \Drupal\commerce_promotion\Entity\CouponInterface[] $coupons */
  $coupons = $order
    ->get('coupons')
    ->referencedEntities();
  foreach ($coupons as $index => $coupon) {
    $promotion = $coupon
      ->getPromotion();
    $promotion
      ->apply($order);
  }

  // Non-coupon promotions are loaded and applied separately.

  /** @var \Drupal\commerce_promotion\PromotionStorageInterface $promotion_storage */
  $promotion_storage = $this->entityTypeManager
    ->getStorage('commerce_promotion');
  $promotions = $promotion_storage
    ->loadAvailable($order);
  foreach ($promotions as $promotion) {
    if (!$promotion
      ->applies($order)) {
      continue;
    }

    // Ensure the promotion is in the right language, to ensure promotions
    // adjustments labels are correctly translated.
    if ($promotion
      ->hasTranslation($content_langcode)) {
      $promotion = $promotion
        ->getTranslation($content_langcode);
    }
    $promotion
      ->apply($order);
  }
}