Example #1
0
 /**
  * Request shipping rates for entire address or specified address item
  * Returns true if current selected shipping method code corresponds to one of the found rates
  *
  * @param \Magento\Sales\Model\Quote\Item\AbstractItem $item
  * @return bool
  */
 public function requestShippingRates(\Magento\Sales\Model\Quote\Item\AbstractItem $item = null)
 {
     /** @var $request \Magento\Sales\Model\Quote\Address\RateRequest */
     $request = $this->_rateRequestFactory->create();
     $request->setAllItems($item ? array($item) : $this->getAllItems());
     $request->setDestCountryId($this->getCountryId());
     $request->setDestRegionId($this->getRegionId());
     $request->setDestRegionCode($this->getRegionCode());
     $request->setDestStreet($this->getStreetFull());
     $request->setDestCity($this->getCity());
     $request->setDestPostcode($this->getPostcode());
     $request->setPackageValue($item ? $item->getBaseRowTotal() : $this->getBaseSubtotal());
     $packageWithDiscount = $item ? $item->getBaseRowTotal() - $item->getBaseDiscountAmount() : $this->getBaseSubtotalWithDiscount();
     $request->setPackageValueWithDiscount($packageWithDiscount);
     $request->setPackageWeight($item ? $item->getRowWeight() : $this->getWeight());
     $request->setPackageQty($item ? $item->getQty() : $this->getItemQty());
     /**
      * Need for shipping methods that use insurance based on price of physical products
      */
     $packagePhysicalValue = $item ? $item->getBaseRowTotal() : $this->getBaseSubtotal() - $this->getBaseVirtualAmount();
     $request->setPackagePhysicalValue($packagePhysicalValue);
     $request->setFreeMethodWeight($item ? 0 : $this->getFreeMethodWeight());
     /**
      * Store and website identifiers need specify from quote
      */
     /*$request->setStoreId($this->_storeManager->getStore()->getId());
       $request->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());*/
     $request->setStoreId($this->getQuote()->getStore()->getId());
     $request->setWebsiteId($this->getQuote()->getStore()->getWebsiteId());
     $request->setFreeShipping($this->getFreeShipping());
     /**
      * Currencies need to convert in free shipping
      */
     $request->setBaseCurrency($this->getQuote()->getStore()->getBaseCurrency());
     $request->setPackageCurrency($this->getQuote()->getStore()->getCurrentCurrency());
     $request->setLimitCarrier($this->getLimitCarrier());
     $request->setBaseSubtotalInclTax($this->getBaseSubtotalInclTax());
     $result = $this->_rateCollector->create()->collectRates($request)->getResult();
     $found = false;
     if ($result) {
         $shippingRates = $result->getAllRates();
         foreach ($shippingRates as $shippingRate) {
             $rate = $this->_addressRateFactory->create()->importShippingRate($shippingRate);
             if (!$item) {
                 $this->addShippingRate($rate);
             }
             if ($this->getShippingMethod() == $rate->getCode()) {
                 if ($item) {
                     $item->setBaseShippingAmount($rate->getPrice());
                 } else {
                     /**
                      * possible bug: this should be setBaseShippingAmount(),
                      * see \Magento\Sales\Model\Quote\Address\Total\Shipping::collect()
                      * where this value is set again from the current specified rate price
                      * (looks like a workaround for this bug)
                      */
                     $this->setShippingAmount($rate->getPrice());
                 }
                 $found = true;
             }
         }
     }
     return $found;
 }
Example #2
0
 /**
  * @param AbstractItem $item
  * @return float
  */
 public function getBaseSubtotalInclTax($item)
 {
     $tax = $item->getBaseTaxAmount() + $item->getBaseDiscountTaxCompensation();
     return $item->getBaseRowTotal() + $tax;
 }
Example #3
0
 /**
  * Calculate base total amount for the item
  *
  * @param QuoteItem|Item|InvoiceItem|CreditmemoItem $item
  * @return mixed
  */
 public function getBaseTotalAmount($item)
 {
     $baseTotalAmount = $item->getBaseRowTotal() - $item->getBaseDiscountAmount();
     return $baseTotalAmount;
 }