Exemplo n.º 1
0
 /**
  * Calculate quote totals for each rule and save results
  *
  * @param mixed $items
  * @param Address $address
  * @return $this
  */
 public function initTotals($items, Address $address)
 {
     $address->setCartFixedRules([]);
     if (!$items) {
         return $this;
     }
     /** @var \Magento\SalesRule\Model\Rule $rule */
     foreach ($this->_getRules() as $rule) {
         if (\Magento\SalesRule\Model\Rule::CART_FIXED_ACTION == $rule->getSimpleAction() && $this->validatorUtility->canProcessRule($rule, $address)) {
             $ruleTotalItemsPrice = 0;
             $ruleTotalBaseItemsPrice = 0;
             $validItemsCount = 0;
             foreach ($items as $item) {
                 //Skipping child items to avoid double calculations
                 if ($item->getParentItemId()) {
                     continue;
                 }
                 if (!$rule->getActions()->validate($item)) {
                     continue;
                 }
                 if (!$this->canApplyDiscount($item)) {
                     continue;
                 }
                 $qty = $this->validatorUtility->getItemQty($item, $rule);
                 $ruleTotalItemsPrice += $this->getItemPrice($item) * $qty;
                 $ruleTotalBaseItemsPrice += $this->getItemBasePrice($item) * $qty;
                 $validItemsCount++;
             }
             $this->_rulesItemTotals[$rule->getId()] = ['items_price' => $ruleTotalItemsPrice, 'base_items_price' => $ruleTotalBaseItemsPrice, 'items_count' => $validItemsCount];
         }
     }
     return $this;
 }