Freeshipping.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Free shipping model
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\OfflineShipping\Model\Carrier;
  12. use Magento\Quote\Model\Quote\Address\RateRequest;
  13. /**
  14. * Free shipping model
  15. *
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class Freeshipping extends \Magento\Shipping\Model\Carrier\AbstractCarrier implements
  20. \Magento\Shipping\Model\Carrier\CarrierInterface
  21. {
  22. /**
  23. * @var string
  24. */
  25. protected $_code = 'freeshipping';
  26. /**
  27. * @var bool
  28. */
  29. protected $_isFixed = true;
  30. /**
  31. * @var \Magento\Shipping\Model\Rate\ResultFactory
  32. */
  33. protected $_rateResultFactory;
  34. /**
  35. * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
  36. */
  37. protected $_rateMethodFactory;
  38. /**
  39. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  40. * @param \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory
  41. * @param \Psr\Log\LoggerInterface $logger
  42. * @param \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory
  43. * @param \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory
  44. * @param array $data
  45. */
  46. public function __construct(
  47. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  48. \Magento\Quote\Model\Quote\Address\RateResult\ErrorFactory $rateErrorFactory,
  49. \Psr\Log\LoggerInterface $logger,
  50. \Magento\Shipping\Model\Rate\ResultFactory $rateResultFactory,
  51. \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory $rateMethodFactory,
  52. array $data = []
  53. ) {
  54. $this->_rateResultFactory = $rateResultFactory;
  55. $this->_rateMethodFactory = $rateMethodFactory;
  56. parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);
  57. }
  58. /**
  59. * FreeShipping Rates Collector
  60. *
  61. * @param RateRequest $request
  62. * @return \Magento\Shipping\Model\Rate\Result|bool
  63. */
  64. public function collectRates(RateRequest $request)
  65. {
  66. if (!$this->getConfigFlag('active')) {
  67. return false;
  68. }
  69. /** @var \Magento\Shipping\Model\Rate\Result $result */
  70. $result = $this->_rateResultFactory->create();
  71. $this->_updateFreeMethodQuote($request);
  72. if ($request->getFreeShipping() || $request->getPackageValueWithDiscount() >= $this->getConfigData(
  73. 'free_shipping_subtotal'
  74. )
  75. ) {
  76. /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
  77. $method = $this->_rateMethodFactory->create();
  78. $method->setCarrier('freeshipping');
  79. $method->setCarrierTitle($this->getConfigData('title'));
  80. $method->setMethod('freeshipping');
  81. $method->setMethodTitle($this->getConfigData('name'));
  82. $method->setPrice('0.00');
  83. $method->setCost('0.00');
  84. $result->append($method);
  85. } elseif ($this->getConfigData('showmethod')) {
  86. $error = $this->_rateErrorFactory->create();
  87. $error->setCarrier($this->_code);
  88. $error->setCarrierTitle($this->getConfigData('title'));
  89. $errorMsg = $this->getConfigData('specificerrmsg');
  90. $error->setErrorMessage(
  91. $errorMsg ? $errorMsg : __(
  92. 'Sorry, but we can\'t deliver to the destination country with this shipping module.'
  93. )
  94. );
  95. return $error;
  96. }
  97. return $result;
  98. }
  99. /**
  100. * Allows free shipping when all product items have free shipping (promotions etc.)
  101. *
  102. * @param \Magento\Quote\Model\Quote\Address\RateRequest $request
  103. * @return void
  104. */
  105. protected function _updateFreeMethodQuote($request)
  106. {
  107. $freeShipping = false;
  108. $items = $request->getAllItems();
  109. $c = count($items);
  110. for ($i = 0; $i < $c; $i++) {
  111. if ($items[$i]->getProduct() instanceof \Magento\Catalog\Model\Product) {
  112. if ($items[$i]->getFreeShipping()) {
  113. $freeShipping = true;
  114. } else {
  115. return;
  116. }
  117. }
  118. }
  119. if ($freeShipping) {
  120. $request->setFreeShipping(true);
  121. }
  122. }
  123. /**
  124. * Returns allowed shipping methods
  125. *
  126. * @return array
  127. */
  128. public function getAllowedMethods()
  129. {
  130. return ['freeshipping' => $this->getConfigData('name')];
  131. }
  132. }