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'); 
   }
}

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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