Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Freeshipping.php
Go to the documentation of this file.
1 <?php
13 
15 
24 {
28  protected $_code = 'freeshipping';
29 
33  protected $_isFixed = true;
34 
39 
44 
53  public function __construct(
54  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
55  \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
56  \Psr\Log\LoggerInterface $logger,
57  \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
58  \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
59  array $data = []
60  ) {
61  $this->_rateResultFactory = $rateResultFactory;
62  $this->_rateMethodFactory = $rateMethodFactory;
63  parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
64  }
65 
72  public function collectRates(RateRequest $request)
73  {
74  if (!$this->getConfigFlag('active')) {
75  return false;
76  }
77 
79  $result = $this->_rateResultFactory->create();
80 
81  $this->_updateFreeMethodQuote($request);
82 
83  if ($request->getFreeShipping() || $request->getBaseSubtotalInclTax() >= $this->getConfigData(
84  'free_shipping_subtotal'
85  )
86  ) {
88  $method = $this->_rateMethodFactory->create();
89 
90  $method->setCarrier('freeshipping');
91  $method->setCarrierTitle($this->getConfigData('title'));
92 
93  $method->setMethod('freeshipping');
94  $method->setMethodTitle($this->getConfigData('name'));
95 
96  $method->setPrice('0.00');
97  $method->setCost('0.00');
98 
99  $result->append($method);
100  }
101 
102  return $result;
103  }
104 
111  protected function _updateFreeMethodQuote($request)
112  {
113  $freeShipping = false;
114  $items = $request->getAllItems();
115  $c = count($items);
116  for ($i = 0; $i < $c; $i++) {
117  if ($items[$i]->getProduct() instanceof \Magento\Catalog\Model\Product) {
118  if ($items[$i]->getFreeShipping()) {
119  $freeShipping = true;
120  } else {
121  return;
122  }
123  }
124  }
125  if ($freeShipping) {
126  $request->setFreeShipping(true);
127  }
128  }
129 
133  public function getAllowedMethods()
134  {
135  return ['freeshipping' => $this->getConfigData('name')];
136  }
137 }
__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, array $data=[])
$logger
$method
Definition: info.phtml:13
$i
Definition: gallery.phtml:31
$items