Пример #1
0
 /**
  * Fetch totals
  *
  * @param \Magento\Sales\Model\Quote\Address $address
  * @return $this
  */
 public function fetchTotals(\Magento\Sales\Model\Quote\Address $address)
 {
     $amount = $address->getShippingAmount();
     if ($amount != 0) {
         $title = __('Shipping & Handling');
         if ($address->getShippingDescription()) {
             $title .= sprintf(' (%s)', $address->getShippingDescription());
         }
         $address->addTotal(array('code' => 'shipping', 'title' => $title, 'value' => $address->getShippingAmount()));
     }
     return $this;
 }
Пример #2
0
 /**
  * @param Address $address
  * @return mixed
  */
 public function getShippingPriceExclTax($address)
 {
     return $this->formatPrice($address->getShippingAmount());
 }
Пример #3
0
 /**
  * Apply discounts to shipping amount
  *
  * @param Address $address
  * @return $this
  */
 public function processShippingAmount(Address $address)
 {
     $shippingAmount = $address->getShippingAmountForDiscount();
     if ($shippingAmount !== null) {
         $baseShippingAmount = $address->getBaseShippingAmountForDiscount();
     } else {
         $shippingAmount = $address->getShippingAmount();
         $baseShippingAmount = $address->getBaseShippingAmount();
     }
     $quote = $address->getQuote();
     $appliedRuleIds = array();
     foreach ($this->_getRules() as $rule) {
         /* @var \Magento\SalesRule\Model\Rule $rule */
         if (!$rule->getApplyToShipping() || !$this->validatorUtility->canProcessRule($rule, $address)) {
             continue;
         }
         $discountAmount = 0;
         $baseDiscountAmount = 0;
         $rulePercent = min(100, $rule->getDiscountAmount());
         switch ($rule->getSimpleAction()) {
             case \Magento\SalesRule\Model\Rule::TO_PERCENT_ACTION:
                 $rulePercent = max(0, 100 - $rule->getDiscountAmount());
                 // break is intentionally omitted
             // break is intentionally omitted
             case \Magento\SalesRule\Model\Rule::BY_PERCENT_ACTION:
                 $discountAmount = ($shippingAmount - $address->getShippingDiscountAmount()) * $rulePercent / 100;
                 $baseDiscountAmount = ($baseShippingAmount - $address->getBaseShippingDiscountAmount()) * $rulePercent / 100;
                 $discountPercent = min(100, $address->getShippingDiscountPercent() + $rulePercent);
                 $address->setShippingDiscountPercent($discountPercent);
                 break;
             case \Magento\SalesRule\Model\Rule::TO_FIXED_ACTION:
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $shippingAmount - $quoteAmount;
                 $baseDiscountAmount = $baseShippingAmount - $rule->getDiscountAmount();
                 break;
             case \Magento\SalesRule\Model\Rule::BY_FIXED_ACTION:
                 $quoteAmount = $quote->getStore()->convertPrice($rule->getDiscountAmount());
                 $discountAmount = $quoteAmount;
                 $baseDiscountAmount = $rule->getDiscountAmount();
                 break;
             case \Magento\SalesRule\Model\Rule::CART_FIXED_ACTION:
                 $cartRules = $address->getCartFixedRules();
                 if (!isset($cartRules[$rule->getId()])) {
                     $cartRules[$rule->getId()] = $rule->getDiscountAmount();
                 }
                 if ($cartRules[$rule->getId()] > 0) {
                     $quoteAmount = $quote->getStore()->convertPrice($cartRules[$rule->getId()]);
                     $discountAmount = min($shippingAmount - $address->getShippingDiscountAmount(), $quoteAmount);
                     $baseDiscountAmount = min($baseShippingAmount - $address->getBaseShippingDiscountAmount(), $cartRules[$rule->getId()]);
                     $cartRules[$rule->getId()] -= $baseDiscountAmount;
                 }
                 $address->setCartFixedRules($cartRules);
                 break;
         }
         $discountAmount = min($address->getShippingDiscountAmount() + $discountAmount, $shippingAmount);
         $baseDiscountAmount = min($address->getBaseShippingDiscountAmount() + $baseDiscountAmount, $baseShippingAmount);
         $address->setShippingDiscountAmount($discountAmount);
         $address->setBaseShippingDiscountAmount($baseDiscountAmount);
         $appliedRuleIds[$rule->getRuleId()] = $rule->getRuleId();
         $this->rulesApplier->maintainAddressCouponCode($address, $rule, $this->getCouponCode());
         $this->rulesApplier->addDiscountDescription($address, $rule);
         if ($rule->getStopRulesProcessing()) {
             break;
         }
     }
     $address->setAppliedRuleIds($this->validatorUtility->mergeIds($address->getAppliedRuleIds(), $appliedRuleIds));
     $quote->setAppliedRuleIds($this->validatorUtility->mergeIds($quote->getAppliedRuleIds(), $appliedRuleIds));
     return $this;
 }
Пример #4
0
 /**
  * Add shipping totals information to address object
  *
  * @param \Magento\Sales\Model\Quote\Address $address
  * @return $this
  */
 public function fetch(\Magento\Sales\Model\Quote\Address $address)
 {
     $amount = $address->getShippingAmount();
     $shippingDescription = $address->getShippingDescription();
     if ($amount != 0 || $shippingDescription) {
         $title = $shippingDescription ? __('Shipping & Handling (%1)', $shippingDescription) : __('Shipping & Handling');
         $address->addTotal(array('code' => $this->getCode(), 'title' => $title, 'value' => $amount));
     }
     return $this;
 }
Пример #5
0
 /**
  * @param Address $address
  * @param bool $useBaseCurrency
  * @return \Magento\Tax\Service\V1\Data\QuoteDetails\Item
  */
 public function getShippingDataObject(Address $address, $useBaseCurrency)
 {
     if ($address->getShippingTaxCalculationAmount() === null) {
         //Save the original shipping amount because shipping amount will be overridden
         //with shipping amount excluding tax
         $address->setShippingTaxCalculationAmount($address->getShippingAmount());
         $address->setBaseShippingTaxCalculationAmount($address->getBaseShippingAmount());
     }
     if ($address->getShippingTaxCalculationAmount() !== null) {
         $itemBuilder = $this->quoteDetailsBuilder->getItemBuilder();
         $itemBuilder->setType(self::ITEM_TYPE_SHIPPING);
         $itemBuilder->setCode(self::ITEM_CODE_SHIPPING);
         $itemBuilder->setQuantity(1);
         if ($useBaseCurrency) {
             $itemBuilder->setUnitPrice($address->getBaseShippingTaxCalculationAmount());
         } else {
             $itemBuilder->setUnitPrice($address->getShippingTaxCalculationAmount());
         }
         if ($address->getShippingDiscountAmount()) {
             if ($useBaseCurrency) {
                 $itemBuilder->setDiscountAmount($address->getBaseShippingDiscountAmount());
             } else {
                 $itemBuilder->setDiscountAmount($address->getShippingDiscountAmount());
             }
         }
         $itemBuilder->setTaxClassKey($itemBuilder->getTaxClassKeyBuilder()->setType(TaxClassKey::TYPE_ID)->setValue($this->_config->getShippingTaxClass($address->getQuote()->getStore()))->create());
         $itemBuilder->setTaxIncluded($this->_config->shippingPriceIncludesTax($address->getQuote()->getStore()));
         return $itemBuilder->create();
     }
     return null;
 }