I have created a custom shipping method which allows delivery to a local office of the carrier. I have created a select when the shipping method is selected, so the user picks the office. Now I need to store the office ID to quote_address
when it is provided.
I have managed to add that field to extension_attributes, so when the checkout form is submitted, this is passed to the back-end (@ http://website/rest/default/V1/guest-carts/e01f063c3b16e082b3546697e321e643/shipping-information
:
carrier_office
in extension_attributes
is what I need to save.
I already created an install script, which creates a column in quote_address
and its there in the database. I just need to save the value to it.
What I've done is first, I set up an extension field in in extension_attributes.xml
:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\AddressInterface">
<attribute code="carrier_office" type="string"/>
</extension_attributes>
</config>
Then in di.xml I set up a plugin:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Vendor_Module\Api\OfficeManagementInterface" type="Vendor_Module\Model\OfficeManagement" />
<preference for="Vendor_Module\Api\Data\OfficeInterface" type="Vendor_Module\Model\Office" />
<type name="Magento\Quote\Model\ShippingAddressManagement">
<plugin disabled="false" name="Creatizmo_Speedy_Plugin_Magento_Quote_Model_ShippingAddressManagement" sortOrder="10" type="Vendor_Module\Plugin\Magento\Quote\Model\ShippingAddressManagement"/>
</type>
</config>
Where of course Vendor_Module is replaced with my module's name.
And then in Plugin/Magento/Quote/Model/ShippingAddressManagement
:
namespace Vendor_Module\Plugin\Magento\Quote\Model;
class ShippingAddressManagement
{
protected $logger;
public function __construct(
\Psr\Log\LoggerInterface $logger
) {
$this->logger = $logger;
}
public function beforeAssign(
\Magento\Quote\Model\ShippingAddressManagement $subject,
$cartId,
\Magento\Quote\Api\Data\AddressInterface $address
) {
$extAttributes = $address->getExtensionAttributes();
if (!empty($extAttributes)) {
try {
$address->setCarrierOffice($address->getCarrierOffice());
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}
}
}
}
In the last file I can see the extension attribute with the correct ID (if I dump it)
, but it's not saved. It seems $address->setCarrierOffice
is not doing its thing or maybe it's not the right place for a hook.. I don't know
Any suggestions on what I'm doing wrong or what I should be doing are highly appreciated.