123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- /**
- * Free shipping model
- *
- * @author Magento Core Team <core@magentocommerce.com>
- */
- namespace Magento\OfflineShipping\Model\Carrier;
- use Magento\Quote\Model\Quote\Address\RateRequest;
- /**
- * Free shipping model
- *
- * @api
- * @since 100.0.2
- */
- class Freeshipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
- \Magento\Shipping\Model\Carrier\CarrierInterface
- {
- /**
- * @var string
- */
- protected $_code = 'freeshipping';
- /**
- * @var bool
- */
- protected $_isFixed = true;
- /**
- * @var \Magento\Shipping\Model\Rate\ResultFactory
- */
- protected $_rateResultFactory;
- /**
- * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
- */
- protected $_rateMethodFactory;
- /**
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
- * @param \Psr\Log\LoggerInterface $logger
- * @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
- * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
- * @param array $data
- */
- public function __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 = []
- ) {
- $this->_rateResultFactory = $rateResultFactory;
- $this->_rateMethodFactory = $rateMethodFactory;
- parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
- }
- /**
- * FreeShipping Rates Collector
- *
- * @param RateRequest $request
- * @return \Magento\Shipping\Model\Rate\Result|bool
- */
- public function collectRates(RateRequest $request)
- {
- if (!$this->getConfigFlag('active')) {
- return false;
- }
- /** @var \Magento\Shipping\Model\Rate\Result $result */
- $result = $this->_rateResultFactory->create();
- $this->_updateFreeMethodQuote($request);
- if ($request->getFreeShipping() || $request->getPackageValueWithDiscount() >= $this->getConfigData(
- 'free_shipping_subtotal'
- )
- ) {
- /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
- $method = $this->_rateMethodFactory->create();
- $method->setCarrier('freeshipping');
- $method->setCarrierTitle($this->getConfigData('title'));
- $method->setMethod('freeshipping');
- $method->setMethodTitle($this->getConfigData('name'));
- $method->setPrice('0.00');
- $method->setCost('0.00');
- $result->append($method);
- } elseif ($this->getConfigData('showmethod')) {
- $error = $this->_rateErrorFactory->create();
- $error->setCarrier($this->_code);
- $error->setCarrierTitle($this->getConfigData('title'));
- $errorMsg = $this->getConfigData('specificerrmsg');
- $error->setErrorMessage(
- $errorMsg ? $errorMsg : __(
- 'Sorry, but we can\'t deliver to the destination country with this shipping module.'
- )
- );
- return $error;
- }
- return $result;
- }
- /**
- * Allows free shipping when all product items have free shipping (promotions etc.)
- *
- * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
- * @return void
- */
- protected function _updateFreeMethodQuote($request)
- {
- $freeShipping = false;
- $items = $request->getAllItems();
- $c = count($items);
- for ($i = 0; $i < $c; $i++) {
- if ($items[$i]->getProduct() instanceof \Magento\Catalog\Model\Product) {
- if ($items[$i]->getFreeShipping()) {
- $freeShipping = true;
- } else {
- return;
- }
- }
- }
- if ($freeShipping) {
- $request->setFreeShipping(true);
- }
- }
- /**
- * Returns allowed shipping methods
- *
- * @return array
- */
- public function getAllowedMethods()
- {
- return ['freeshipping' => $this->getConfigData('name')];
- }
- }
|