2

In my custom module I've created model/carrier.php to add custom shipping method, one of my custom shipping method will show up if magento free shipping method is not active, is there a way to do this?

Model/carrier.php

public function collectRates(RateRequest $request)
{
   $method = $this->_rateMethodFactory->create();
   if($free_shipping_method != 'Active'){ //???
      $method->setCarrier('test');
      $method->setCarrierTitle('test');
      /* Use method name */
      $method->setMethod('test');
      $method->setMethodTitle('England is My City');
      $method->setCost('30');
      $method->setPrice('30'); 
   }
}
0

You need to add the following code in your custom shipping module (carrier.php).

 /**
 * @var \Magento\OfflineShipping\Model\Carrier\Freeshipping
 */
protected $_freeshipping;

 public function __construct(
\Magento\OfflineShipping\Model\Carrier\Freeshipping $freeshipping
) {
        $this->_freeshipping = $freeshipping;
  }

public function collectRates(RateRequest $request)
    {
        if (!$this->getConfigFlag('active') || $this->_freeshipping->getConfigData('active')) {
            return false;
        }
 /** @var \Magento\Shipping\Model\Rate\Result $result */
        $result = $this->_rateResultFactory->create();

        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */
        $method = $this->_rateMethodFactory->create();

        $method->setCarrier($this->_code);
        $method->setCarrierTitle($this->getConfigData('title'));

        $method->setMethod($this->_code);
        $method->setMethodTitle($this->getConfigData('name'));

        $amount = $this->getShippingPrice();

        $method->setPrice($amount);
        $method->setCost($amount);

        $result->append($method);

        return $result;
}

Hope this answer will help you. let me know you have any further issue. Thanks

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.