Magento – Magento 2 – hacking FedEx rate request

fedexmagento-2.1magento2shipping-addressshipping-methods

We are running CE 2.1.2

Magento 2 sets the residential flag in the rates to request based on the configuration data. This means that all quotes are either residential or commercial, depending on what you set in the shipping method.

I want to set the flag based on if the Destination Address has a Company set or not.

What I did is go into /vendor/magento/module-fedex/Model/carrier.php and change line 430 from

'Residential' => (bool)$this->getConfigData('residence_delivery'),

to

'Residential' => !empty(trim($r->getRecipientContactCompanyName())),

The is not working. Residential is false always. So empty must be returning true always.

Is the getRecipientContactCompanyName() not the right way to get the Company name in this module? Any hints as to what I am doing wrong would be appreciated.

Best Answer

The problem is that $r is not an instance of RateRequest, it's a plain DataObject that is created in the setRequest method. You need to add

if ($request->getRecipientContactCompanyName()) {
    $r->setRecipientContactCompanyName($request->getRecipientContactCompanyName());
}

to the setRequest method before $this->setRawRequest($r);.

You should not alter the core files, instead you should override the class in a module, otherwise your changes may be lost when updating Magento.

Related Topic