Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Flatrate.php
Go to the documentation of this file.
1 <?php
7 
13 
20 class Flatrate extends AbstractCarrier implements CarrierInterface
21 {
25  protected $_code = 'flatrate';
26 
30  protected $_isFixed = true;
31 
36 
41 
45  private $itemPriceCalculator;
46 
56  public function __construct(
57  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
58  \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
59  \Psr\Log\LoggerInterface $logger,
60  \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
61  \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
62  \Magento\OfflineShipping\Model\Carrier\Flatrate\ItemPriceCalculator $itemPriceCalculator,
63  array $data = []
64  ) {
65  $this->_rateResultFactory = $rateResultFactory;
66  $this->_rateMethodFactory = $rateMethodFactory;
67  $this->itemPriceCalculator = $itemPriceCalculator;
68  parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
69  }
70 
77  public function collectRates(RateRequest $request)
78  {
79  if (!$this->getConfigFlag('active')) {
80  return false;
81  }
82 
83  $freeBoxes = $this->getFreeBoxesCount($request);
84  $this->setFreeBoxes($freeBoxes);
85 
87  $result = $this->_rateResultFactory->create();
88 
89  $shippingPrice = $this->getShippingPrice($request, $freeBoxes);
90 
91  if ($shippingPrice !== false) {
92  $method = $this->createResultMethod($shippingPrice);
93  $result->append($method);
94  }
95 
96  return $result;
97  }
98 
103  private function getFreeBoxesCount(RateRequest $request)
104  {
105  $freeBoxes = 0;
106  if ($request->getAllItems()) {
107  foreach ($request->getAllItems() as $item) {
108  if ($item->getProduct()->isVirtual() || $item->getParentItem()) {
109  continue;
110  }
111 
112  if ($item->getHasChildren() && $item->isShipSeparately()) {
113  $freeBoxes += $this->getFreeBoxesCountFromChildren($item);
114  } elseif ($item->getFreeShipping()) {
115  $freeBoxes += $item->getQty();
116  }
117  }
118  }
119  return $freeBoxes;
120  }
121 
125  public function getAllowedMethods()
126  {
127  return ['flatrate' => $this->getConfigData('name')];
128  }
129 
135  private function getShippingPrice(RateRequest $request, $freeBoxes)
136  {
137  $shippingPrice = false;
138 
139  $configPrice = $this->getConfigData('price');
140  if ($this->getConfigData('type') === 'O') {
141  // per order
142  $shippingPrice = $this->itemPriceCalculator->getShippingPricePerOrder($request, $configPrice, $freeBoxes);
143  } elseif ($this->getConfigData('type') === 'I') {
144  // per item
145  $shippingPrice = $this->itemPriceCalculator->getShippingPricePerItem($request, $configPrice, $freeBoxes);
146  }
147 
149 
150  if ($shippingPrice !== false && $request->getPackageQty() == $freeBoxes) {
151  $shippingPrice = '0.00';
152  }
153  return $shippingPrice;
154  }
155 
160  private function createResultMethod($shippingPrice)
161  {
163  $method = $this->_rateMethodFactory->create();
164 
165  $method->setCarrier('flatrate');
166  $method->setCarrierTitle($this->getConfigData('title'));
167 
168  $method->setMethod('flatrate');
169  $method->setMethodTitle($this->getConfigData('name'));
170 
171  $method->setPrice($shippingPrice);
172  $method->setCost($shippingPrice);
173  return $method;
174  }
175 
180  private function getFreeBoxesCountFromChildren($item)
181  {
182  $freeBoxes = 0;
183  foreach ($item->getChildren() as $child) {
184  if ($child->getFreeShipping() && !$child->getProduct()->isVirtual()) {
185  $freeBoxes += $item->getQty() * $child->getQty();
186  }
187  }
188  return $freeBoxes;
189  }
190 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$shippingPrice
Definition: price.phtml:12
$logger
$method
Definition: info.phtml:13
__construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory, \Psr\Log\LoggerInterface $logger, \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory, \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory, \Magento\OfflineShipping\Model\Carrier\Flatrate\ItemPriceCalculator $itemPriceCalculator, array $data=[])
Definition: Flatrate.php:56