1

I already know how to create a single shipping custom method, with files system.xml, config.xml and Carrier.php, however I'm working with a REST API of a Carrier provider, and when I get the response, I get like 6 different shipping methods, from different Couriers, for example: Fedex, UPS and more.

How can I show this quotes and Couriers in Checkout Shipping side? I was checking USPS module and it has this code under vendor/magento/module-usps/etc/config.xml :

<allowed_methods>0_FCLE,0_FCL,0_FCP,1,2,3,4,6,7,13,16,17,22,23,25,27,28,33,34,35,36,37,42,43,53,55,56,57,61,INT_1,INT_2,INT_4,INT_6,INT_7,INT_8,INT_9,INT_10,INT_11,INT_12,INT_13,INT_14,INT_15,INT_16,INT_20,INT_26</allowed_methods>

But what I don't get, is how USPS module creates different shipping methods, also I will need to have different Courier codes, in collectRates method, for example, the default shipping method has just one courier code:

/** @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->setCarrierTitle("Pakkkee");

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

        $shippingCost = (float)$this->getConfigData('shipping_cost');

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

        $result->append($method);

I will need something like this:

for($i = 0; $i < _code.length; i++){
    /** @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[$i]);
        $method->setCarrierTitle($this->getConfigData('title'));


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

        $shippingCost = (float)$this->getConfigData('shipping_cost');

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

        $result->append($method);

}

Same for getAllowedMethods();

public function getAllowedMethods()
    {
       for($i = 0; $i < _code.length; i++){
        return [$this->_code[$i] => $this->getConfigData('name')];
       }
    }

The number of Couriers is dynamic, so creating many methods in config.xml will not be useful.

Greetings and thank you!

1

see your own "I will need something like this" section.

Basically if you were to do this, it would return the last result in the array only.

This creates your quote result set.

$result = $this->rateResultFactory->create();

then everything assigning to method is creating your actual result until finally this adds your shipping method to your result set.

$result->append($method);

so you're effectively overwriting your result set each time.

|improve this answer|||||
  • hi, you are right, it would only return the last index of the array, but then how can I create many shipping methods on checkout from this method? is it in the function getAllowedMethods? – Oscar Vazquez Dec 11 '19 at 18:41
  • Sorry, hoping you manged to sort this? i lost my username. Does just dropping the $result->append($method); our of the for each not return what youre looking for? – R.Dukane Jan 8 at 9:12
  • Yes don't worry, I solved this issue – Oscar Vazquez Jan 9 at 21:26
0

I solved my issue by doing this in Model/Carrier.php

    use Magento\Quote\Model\Quote\Address\RateRequest;
use Magento\Shipping\Model\Carrier\AbstractCarrier;
use Magento\Shipping\Model\Carrier\CarrierInterface;

/**
 * Custom shipping model
 */
class Carrier extends AbstractCarrier implements CarrierInterface
{
    /**
     * @var string
     */


    protected $_code = "Pakke";



    /**
     * @var bool
     */
    protected $_isFixed = true;

    /**
     * @var \Magento\Shipping\Model\Rate\ResultFactory
     */
    private $rateResultFactory;

    /**
     * @var \Magento\Quote\Model\Quote\Address\RateResult\MethodFactory
     */
    private $rateMethodFactory;

    public $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
    protected $scopeConfig;
    /**
     * @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 = []
    ) {
        parent::__construct($scopeConfig, $rateErrorFactory, $logger, $data);

        $this->rateResultFactory = $rateResultFactory;
        $this->rateMethodFactory = $rateMethodFactory;
        $this->scopeConfig = $scopeConfig;

    }

    /**
     * Custom Shipping Rates Collector
     *
     * @param RateRequest $request
     * @return \Magento\Shipping\Model\Rate\Result|bool
     */
    public function collectRates(RateRequest $request)
    {

        if (!$this->getConfigFlag('active')) {
            return false;
        }


        $result = $this->rateResultFactory->create();

        /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */

        $allowedMethods = $this->getAllowedMethods();
        foreach ($allowedMethods as $key) {

            $method = $this->rateMethodFactory->create();

            $method->setCarrier($this->_code);
            $method->setCarrierTitle($key["title"]);

            $method->setMethod($key["code"]);
            $method->setMethodTitle($key["method"]);

            $shippingCost = (float)$key["cost"];
            $method->setPrice($shippingCost);
            $method->setCost($shippingCost);


            $result->append($method);
        }

        return $result;

    }

    /**
     * @return array
     */
    public function getAllowedMethods()
    {

       //HERE IF YOU HAVE AN ARRAY, YOU CAN JUST RETURN IT 
    $methods = array(
        0 => array(
            'code' => 'code1',
            'title' => 'Carrier 1 Title',
            'method' => 'Carrier 1 method'
            'cost' => 11.2
        ),
        1 => array(
            'code' => 'code2',
            'title' => 'Carrier 2 Title'
            'method' => 'Carrier 2 method'
            'cost' => 32.48
        ),
    );
    return $methods
}

    }
|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.