Skip to content

Add billing address autocomplete functionality to module #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Model/AutocompleteConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function getConfig()
'active' => $this->helper->getConfigValue('shipping/shipper_autocomplete/active'),
'api_key' => $this->helper->getConfigValue('shipping/shipper_autocomplete/google_api_key'),
'use_geolocation' => $this->helper->getConfigValue('shipping/shipper_autocomplete/use_geolocation'),
'use_long_postcode' => $this->helper->getConfigValue('shipping/shipper_autocomplete/use_long_postcode')
'use_long_postcode' => $this->helper->getConfigValue('shipping/shipper_autocomplete/use_long_postcode'),
'billing_autocomplete' => $this->helper->getConfigValue('shipping/shipper_autocomplete/autocomplete_billing_address')
];
return $config;
}
Expand Down
82 changes: 82 additions & 0 deletions src/Plugin/AddCheckoutBillingAddressAutocomplete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace ShipperHQ\AddressAutocomplete\Plugin;

use Magento\Checkout\Block\Checkout\LayoutProcessor;
use Magento\Checkout\Helper\Data as CheckoutHelper;

class AddCheckoutBillingAddressAutocomplete
{
/**
* @var CheckoutHelper
*/
private $checkoutHelper;

/**
* @param CheckoutHelper $checkoutHelper
*/
public function __construct(
CheckoutHelper $checkoutHelper
) {
$this->checkoutHelper = $checkoutHelper;
}

/**
* Adds the billing address autocomplete component to the billing address forms in checkout
*
* @see LayoutProcessor::process()
* @param LayoutProcessor $subject
* @param array $jsLayout
* @return array
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterProcess(LayoutProcessor $subject, $jsLayout)
{
if (!isset(
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']['children']
['payment']['children']
)) {
return $jsLayout;
}

if ($this->checkoutHelper->isDisplayBillingOnPaymentMethodAvailable()) {
$configuration = &$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['payments-list']['children'];

foreach ($configuration as $paymentFormCode => $formConfiguration) {
if (strpos($paymentFormCode, '-form') !== false) {
$paymentCode = str_replace('-form', '', $paymentFormCode);
$configuration[$paymentFormCode]['children']
[$paymentCode . '-billing-address-autocomplete'] = $this->getBillingAddressAutocompleteComponent(
$paymentCode
);
}
}
} else {
$jsLayout['components']['checkout']['children']['steps']['children']['billing-step']
['children']['payment']['children']['afterMethods']['children']['billing-address-form']['children']
['shared-billing-address-autocomplete'] = $this->getBillingAddressAutocompleteComponent(
'shared'
);
}

return $jsLayout;
}

/**
* Returns that component configuration to be used in the checkout
*
* @param string $paymentCode
* @return array
*/
private function getBillingAddressAutocompleteComponent($paymentCode)
{
return [
'component' => 'ShipperHQ_AddressAutocomplete/js/billing-address-autocomplete',
'config' => [
'paymentCode' => $paymentCode
]
];
}
}
7 changes: 7 additions & 0 deletions src/etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
]]>
</comment>
</field>
<field id="autocomplete_billing_address" type="select" translate="label" sortOrder="140" showInDefault="1" showInWebsite="1" showInStore="1">
<label>Autocomplete Billing Address</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>
<![CDATA[Enabling this will allow autocomplete on the billing address field.]]>
</comment>
</field>
</group>
</section>
</system>
Expand Down
1 change: 1 addition & 0 deletions src/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<shipper_autocomplete>
<use_geolocation>1</use_geolocation>
<use_long_postcode>0</use_long_postcode>
<autocomplete_billing_address>0</autocomplete_billing_address>
</shipper_autocomplete>
</shipping>
</default>
Expand Down
4 changes: 4 additions & 0 deletions src/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
</argument>
</arguments>
</type>

<type name="Magento\Checkout\Block\Checkout\LayoutProcessor">
<plugin name="addCheckoutBillingAddressAutocomplete" type="ShipperHQ\AddressAutocomplete\Plugin\AddCheckoutBillingAddressAutocomplete" />
</type>
</config>
Loading