1

I was working on the shipping part, but I found that some functions' declaration can not be found in the code repository. For example, in the 1000th line of

app/code/Magento/Quote/Model/Quote/Address.php

$shippingRates = $result->getAllRates();

https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/Quote/Model/Quote/Address.php#L1000

I searched the whole mangento 2 repository, but I could not find something like

public function getAllRates(){ //....}

Is there any mechanism in Magento 2 that will auto generate functions?

0

The getAllRates method is not a direct method as you assumed, understand that it does not process anything it is a simple getter.

What happens in the app/code/Magento/Quote/Model/Quote/Address.php is that first, this line is called:

$result = $this->_rateCollector->create()->collectRates($request)->getResult();

The collectRates method is called, this method is implemented by every carrier model that implements the RateCollectorInterface interface.

This collectRates method returns a Magento\Shipping\Model\Rate\Result object which is retrieved by the getResult() method.

Then the getAllRates() method is called from Magento\Shipping\Model\Rate\Result:

public function getAllRates()
{
    return $this->_rates;
}

The _rates variable is set via the Magento\Quote\Model\Quote\Address\RateResult\MethodFactory directly in the carrier model, most of the time with code that looks like this:

$rate = $this->_rateMethodFactory->create();
$rate->setCarrier($this->_code);
$rate->setCarrierTitle($this->getConfigData('title'));
$rate->setMethod($method);
$rate->setMethodTitle($this->getCode('method', $method));
$rate->setCost($costArr[$method]);
$rate->setPrice($price);
$result->append($rate);
0

Check in Magento\Shipping\Model\Rate\Result around line 90:

    /**
     * Return all quotes in the result
     *
     * @return array
     */
    public function getAllRates()
    {
        return $this->_rates;
    }

This is what you are looking for.

Anyway, in Magento2 there are some automatically created classes and methods, but not for this kind of functions. Mainly for plugins and factories.

Hope it helps.

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.