123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Shipping\Model\Rate;
- /**
- * Class Result
- * Container for Rates
- *
- * @api
- * @since 100.0.2
- */
- class Result
- {
- /**
- * Shipping method rates
- *
- * @var array
- */
- protected $_rates = [];
- /**
- * Shipping errors
- *
- * @var null|bool
- */
- protected $_error = null;
- /**
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- */
- public function __construct(\Magento\Store\Model\StoreManagerInterface $storeManager)
- {
- $this->_storeManager = $storeManager;
- }
- /**
- * Reset result
- *
- * @return $this
- */
- public function reset()
- {
- $this->_rates = [];
- return $this;
- }
- /**
- * Set Error
- *
- * @param bool $error
- * @return void
- */
- public function setError($error)
- {
- $this->_error = $error;
- }
- /**
- * Get Error
- *
- * @return null|bool
- */
- public function getError()
- {
- return $this->_error;
- }
- /**
- * Add a rate to the result
- *
- * @param \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult|\Magento\Shipping\Model\Rate\Result $result
- * @return $this
- */
- public function append($result)
- {
- if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\Error) {
- $this->setError(true);
- }
- if ($result instanceof \Magento\Quote\Model\Quote\Address\RateResult\AbstractResult) {
- $this->_rates[] = $result;
- } elseif ($result instanceof \Magento\Shipping\Model\Rate\Result) {
- $rates = $result->getAllRates();
- foreach ($rates as $rate) {
- $this->append($rate);
- }
- }
- return $this;
- }
- /**
- * Return all quotes in the result
- *
- * @return \Magento\Quote\Model\Quote\Address\RateResult\Method[]
- */
- public function getAllRates()
- {
- return $this->_rates;
- }
- /**
- * Return rate by id in array
- *
- * @param int $id
- * @return \Magento\Quote\Model\Quote\Address\RateResult\Method|null
- */
- public function getRateById($id)
- {
- return isset($this->_rates[$id]) ? $this->_rates[$id] : null;
- }
- /**
- * Return quotes for specified type
- *
- * @param string $carrier
- * @return array
- */
- public function getRatesByCarrier($carrier)
- {
- $result = [];
- foreach ($this->_rates as $rate) {
- if ($rate->getCarrier() === $carrier) {
- $result[] = $rate;
- }
- }
- return $result;
- }
- /**
- * Converts object to array
- *
- * @return array
- */
- public function asArray()
- {
- if ($this->_storeManager->getStore()->getBaseCurrency()
- && $this->_storeManager->getStore()->getCurrentCurrency()
- ) {
- $currencyFilter = $this->_storeManager->getStore()->getCurrentCurrency()->getFilter();
- $currencyFilter->setRate(
- $this->_storeManager->getStore()->getBaseCurrency()->getRate(
- $this->_storeManager->getStore()->getCurrentCurrency()
- )
- );
- } elseif ($this->_storeManager->getStore()->getDefaultCurrency()) {
- $currencyFilter = $this->_storeManager->getStore()->getDefaultCurrency()->getFilter();
- } else {
- $currencyFilter = new \Magento\Framework\Filter\Sprintf('%s', 2);
- }
- $rates = [];
- $allRates = $this->getAllRates();
- foreach ($allRates as $rate) {
- $rates[$rate->getCarrier()]['title'] = $rate->getCarrierTitle();
- $rates[$rate->getCarrier()]['methods'][$rate->getMethod()] = [
- 'title' => $rate->getMethodTitle(),
- 'price' => $rate->getPrice(),
- 'price_formatted' => $currencyFilter->filter($rate->getPrice()),
- ];
- }
- return $rates;
- }
- /**
- * Get cheapest rate
- *
- * @return null|\Magento\Quote\Model\Quote\Address\RateResult\Method
- */
- public function getCheapestRate()
- {
- $cheapest = null;
- $minPrice = 100000;
- foreach ($this->getAllRates() as $rate) {
- if (is_numeric($rate->getPrice()) && $rate->getPrice() < $minPrice) {
- $cheapest = $rate;
- $minPrice = $rate->getPrice();
- }
- }
- return $cheapest;
- }
- /**
- * Sort rates by price from min to max
- *
- * @return $this
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
- */
- public function sortRatesByPrice()
- {
- if (!is_array($this->_rates) || !count($this->_rates)) {
- return $this;
- }
- /* @var $rate \Magento\Quote\Model\Quote\Address\RateResult\Method */
- foreach ($this->_rates as $i => $rate) {
- $tmp[$i] = $rate->getPrice();
- }
- natsort($tmp);
- foreach ($tmp as $i => $price) {
- $result[] = $this->_rates[$i];
- }
- $this->reset();
- $this->_rates = $result;
- return $this;
- }
- /**
- * Set price for each rate according to count of packages
- *
- * @param int $packageCount
- * @return $this
- */
- public function updateRatePrice($packageCount)
- {
- if ($packageCount > 1) {
- foreach ($this->_rates as $rate) {
- $rate->setPrice($rate->getPrice() * $packageCount);
- }
- }
- return $this;
- }
- }
|