How can I override the shipping block and show my custom shipping addresses there with the module?
I have one custom table which has addresses with website id. I need to display that address in the shipping address section website-wise. It will show the address to the user which are related to that website on the checkout page.
i.e. if i have 2 address in my custom table for website dev. then when I come to the checkout page of the dev website. I need to show that 2 addresses there for the user to select anyone and do the checkout process.
I have multiple addresses that need to be shown in front, so, user can select any one and go through.
I have one idea that I want to override the shipping address block and show there drop down of all addresses which are saved, then on the selection of address filled the shipping address form in hidden and fire save shipping address event. will it be useful?
1 Answer 1
In that case you need a plugin for this.
Create di.xml
Vendor/Module/etc/frontend/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="vendor_assign_default_value" type="Vendor\Module\Plugin\Checkout\Model\Checkout\LayoutProcessor" sortOrder="100"/>
</type>
</config>
Create LayoutProcessor.php
Vendor/Module/Plugin/Checkout/Model/Checkout/LayoutProcessor.php
namespace Vendor\Module\Plugin\Checkout\Model\Checkout;
class LayoutProcessor
{
/**
* @param \Magento\Checkout\Block\Checkout\LayoutProcessor $subject
* @param array $jsLayout
* @return array
*/
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['firstname']['value'] = 'FirstName';
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']['children']
['shippingAddress']['children']['shipping-address-fieldset']['children']['company']['value'] = 'Demo Company';
return $jsLayout;
}
}
If its not work then you will need to move your di.xml file from :
app/code/Vendor/Module/etc/frontend/di.xml
To
app/code/Vendor/Module/etc/di.xml
With code.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="vendor_assign_default_value" type="Vendor\Module\Plugin\Checkout\Model\Checkout\LayoutProcessor" sortOrder="100"/>
</type>
</config>
Thanks To: https://magento.stackexchange.com/a/141544/62614 https://magento.stackexchange.com/a/202727/62614
-
Thanks for your help. but this is for single address. but i have multiple addresses in database. that i want to show in front end and user need to select anyone address. and go further.Utsav Gupta– Utsav Gupta2018年12月10日 09:08:11 +00:00Commented Dec 10, 2018 at 9:08
-
You will need to add all the address in dropdown and need to write dropdown change event.Ashish Viradiya– Ashish Viradiya2018年12月10日 09:35:17 +00:00Commented Dec 10, 2018 at 9:35
Explore related questions
See similar questions with these tags.