Пример #1
0
 /**
  * Collect rates by address
  *
  * @param \Magento\Framework\DataObject $address
  * @param null|bool|array $limitCarrier
  * @return $this
  */
 public function collectRatesByAddress(\Magento\Framework\DataObject $address, $limitCarrier = null)
 {
     /** @var $request \Magento\Quote\Model\Quote\Address\RateRequest */
     $request = $this->_shipmentRequestFactory->create();
     $request->setAllItems($address->getAllItems());
     $request->setDestCountryId($address->getCountryId());
     $request->setDestRegionId($address->getRegionId());
     $request->setDestPostcode($address->getPostcode());
     $request->setPackageValue($address->getBaseSubtotal());
     $request->setPackageValueWithDiscount($address->getBaseSubtotalWithDiscount());
     $request->setPackageWeight($address->getWeight());
     $request->setFreeMethodWeight($address->getFreeMethodWeight());
     $request->setPackageQty($address->getItemQty());
     $request->setStoreId($this->_storeManager->getStore()->getId());
     $request->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
     $request->setBaseCurrency($this->_storeManager->getStore()->getBaseCurrency());
     $request->setPackageCurrency($this->_storeManager->getStore()->getCurrentCurrency());
     $request->setLimitCarrier($limitCarrier);
     $request->setBaseSubtotalInclTax($address->getBaseSubtotalInclTax());
     return $this->collectRates($request);
 }
Пример #2
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\Quote\Model\Quote\Item\AbstractItem $item
  * @return bool
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 public function requestShippingRates(\Magento\Quote\Model\Quote\Item\AbstractItem $item = null)
 {
     /** @var $request \Magento\Quote\Model\Quote\Address\RateRequest */
     $request = $this->_rateRequestFactory->create();
     $request->setAllItems($item ? [$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\Quote\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;
 }