Skip to content

Commit f2c72e2

Browse files
Derrick HeesbeenDerrick Heesbeen
authored andcommitted
First Version
0 parents  commit f2c72e2

16 files changed

Lines changed: 486 additions & 0 deletions

Block/Checkout/LayoutProcessor.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace Experius\ExtraCheckoutAddressFields\Block\Checkout;
4+
5+
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
6+
{
7+
protected $helper;
8+
9+
public function __construct(
10+
\Experius\ExtraCheckoutAddressFields\Helper\Data $helper
11+
)
12+
{
13+
$this->helper = $helper;
14+
}
15+
16+
public function process($result) {
17+
$result = $this->getShippingFormFields($result);
18+
$result = $this->getBillingFormFields($result);
19+
return $result;
20+
}
21+
22+
public function getAdditionalFields($addressType='shipping'){
23+
if($addressType=='shipping') {
24+
return $this->helper->getExtraCheckoutAddressFields('extra_checkout_shipping_address_fields');
25+
}
26+
return $this->helper->getExtraCheckoutAddressFields('extra_checkout_billing_address_fields');
27+
}
28+
29+
public function getShippingFormFields($result){
30+
if(isset($result['components']['checkout']['children']['steps']['children']
31+
['shipping-step']['children']['shippingAddress']['children']
32+
['shipping-address-fieldset'])
33+
){
34+
35+
$shippingPostcodeFields = $this->getFields('shippingAddress.custom_attributes','shipping');
36+
37+
$shippingFields = $result['components']['checkout']['children']['steps']['children']
38+
['shipping-step']['children']['shippingAddress']['children']
39+
['shipping-address-fieldset']['children'];
40+
41+
if(isset($shippingFields['street'])){
42+
unset($shippingFields['street']['children'][1]['validation']);
43+
unset($shippingFields['street']['children'][2]['validation']);
44+
}
45+
46+
$shippingFields = array_replace_recursive($shippingFields,$shippingPostcodeFields);
47+
48+
$result['components']['checkout']['children']['steps']['children']
49+
['shipping-step']['children']['shippingAddress']['children']
50+
['shipping-address-fieldset']['children'] = $shippingFields;
51+
52+
}
53+
54+
return $result;
55+
}
56+
57+
public function getBillingFormFields($result){
58+
if(isset($result['components']['checkout']['children']['steps']['children']
59+
['billing-step']['children']['payment']['children']
60+
['payments-list'])) {
61+
62+
$paymentForms = $result['components']['checkout']['children']['steps']['children']
63+
['billing-step']['children']['payment']['children']
64+
['payments-list']['children'];
65+
66+
foreach ($paymentForms as $paymentMethodForm => $paymentMethodValue) {
67+
68+
$paymentMethodCode = str_replace('-form', '', $paymentMethodForm);
69+
70+
if (!isset($result['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'][$paymentMethodCode . '-form'])) {
71+
continue;
72+
}
73+
74+
$billingFields = $result['components']['checkout']['children']['steps']['children']
75+
['billing-step']['children']['payment']['children']
76+
['payments-list']['children'][$paymentMethodCode . '-form']['children']['form-fields']['children'];
77+
78+
$billingPostcodeFields = $this->getFields('billingAddress' . $paymentMethodCode . '.custom_attributes','billing');
79+
80+
$billingFields = array_replace_recursive($billingFields, $billingPostcodeFields);
81+
82+
$result['components']['checkout']['children']['steps']['children']
83+
['billing-step']['children']['payment']['children']
84+
['payments-list']['children'][$paymentMethodCode . '-form']['children']['form-fields']['children'] = $billingFields;
85+
}
86+
}
87+
88+
return $result;
89+
}
90+
91+
public function getFields($scope,$addressType){
92+
$fields = [];
93+
foreach($this->getAdditionalFields($addressType) as $field){
94+
$fields[$field] = $this->getField($field,$scope);
95+
}
96+
return $fields;
97+
}
98+
99+
public function getField($attributeCode,$scope) {
100+
$field = [
101+
'config' => [
102+
'customScope' => $scope,
103+
],
104+
'dataScope' => $scope . '.'.$attributeCode,
105+
];
106+
107+
return $field;
108+
}
109+
110+
}

Helper/Data.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Experius\ExtraCheckoutAddressFields\Helper;
4+
5+
use Magento\Framework\App\Helper\Context;
6+
use Magento\Framework\App\Helper\AbstractHelper;
7+
8+
class Data extends AbstractHelper{
9+
10+
protected $fieldsetConfig;
11+
12+
protected $logger;
13+
14+
public function __construct(
15+
\Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
16+
\Psr\Log\LoggerInterface $logger
17+
) {
18+
$this->fieldsetConfig = $fieldsetConfig;
19+
$this->logger = $logger;
20+
}
21+
22+
public function getExtraCheckoutAddressFields($fieldset='extra_checkout_billing_address_fields',$root='global'){
23+
24+
$fields = $this->fieldsetConfig->getFieldset($fieldset, $root);
25+
26+
$extraCheckoutFields = [];
27+
28+
foreach($fields as $field=>$fieldInfo){
29+
$extraCheckoutFields[] = $field;
30+
}
31+
32+
return $extraCheckoutFields;
33+
34+
}
35+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
4+
namespace Experius\ExtraCheckoutAddressFields\Observer\Sales;
5+
6+
class ModelServiceQuoteSubmitBefore implements \Magento\Framework\Event\ObserverInterface
7+
{
8+
9+
protected $helper;
10+
11+
protected $logger;
12+
13+
protected $quoteRepository;
14+
15+
public function __construct(
16+
\Magento\Quote\Model\QuoteRepository $quoteRepository,
17+
\Psr\Log\LoggerInterface $logger,
18+
\Experius\ExtraCheckoutAddressFields\Helper\Data $helper
19+
)
20+
{
21+
$this->quoteRepository = $quoteRepository;
22+
$this->logger = $logger;
23+
$this->helper = $helper;
24+
}
25+
26+
/**
27+
* Execute observer
28+
*
29+
* @param \Magento\Framework\Event\Observer $observer
30+
* @return void
31+
*/
32+
public function execute(
33+
\Magento\Framework\Event\Observer $observer
34+
) {
35+
36+
/** @var \Magento\Sales\Model\Order $order */
37+
$order = $observer->getOrder();
38+
39+
$quote = $this->quoteRepository->get($order->getQuoteId());
40+
41+
foreach($this->helper->getExtraCheckoutAddressFields('extra_checkout_billing_address_fields') as $extraField) {
42+
$set = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
43+
$get = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
44+
45+
try {
46+
$order->getBillingAddress()->$set($quote->getBillingAddress()->$get())->save();
47+
} catch (\Exception $e) {
48+
$this->logger->critical($e->getMessage());
49+
}
50+
}
51+
52+
foreach($this->helper->getExtraCheckoutAddressFields('extra_checkout_shipping_address_fields') as $extraField) {
53+
$set = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
54+
$get = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
55+
56+
try {
57+
$order->getShippingAddress()->$set($quote->getShippingAddress()->$get())->save();
58+
} catch (\Exception $e) {
59+
$this->logger->critical($e->getMessage());
60+
}
61+
}
62+
63+
}
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
4+
namespace Experius\ExtraCheckoutAddressFields\Plugin\Magento\Quote\Model;
5+
6+
class BillingAddressManagement
7+
{
8+
9+
protected $helper;
10+
11+
protected $logger;
12+
13+
public function __construct(
14+
\Psr\Log\LoggerInterface $logger,
15+
\Experius\ExtraCheckoutAddressFields\Helper\Data $helper
16+
) {
17+
$this->logger = $logger;
18+
$this->helper = $helper;
19+
}
20+
21+
public function beforeAssign(
22+
\Magento\Quote\Model\BillingAddressManagement $subject,
23+
$cartId,
24+
\Magento\Quote\Api\Data\AddressInterface $address,
25+
$useForShipping = false
26+
) {
27+
28+
$extAttributes = $address->getExtensionAttributes();
29+
if (!empty($extAttributes)) {
30+
31+
foreach($this->helper->getExtraCheckoutAddressFields('extra_checkout_billing_address_fields') as $extraField) {
32+
33+
$set = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
34+
$get = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
35+
36+
$value = $extAttributes->$get();
37+
try {
38+
$address->$set($value);
39+
} catch (\Exception $e) {
40+
$this->logger->critical($e->getMessage());
41+
}
42+
}
43+
}
44+
45+
}
46+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
4+
namespace Experius\ExtraCheckoutAddressFields\Plugin\Magento\Quote\Model;
5+
6+
class ShippingAddressManagement
7+
{
8+
9+
protected $helper;
10+
11+
protected $logger;
12+
13+
public function __construct(
14+
\Psr\Log\LoggerInterface $logger,
15+
\Experius\ExtraCheckoutAddressFields\Helper\Data $helper
16+
) {
17+
$this->logger = $logger;
18+
$this->helper = $helper;
19+
}
20+
21+
public function beforeAssign(
22+
\Magento\Quote\Model\ShippingAddressManagement $subject,
23+
$cartId,
24+
\Magento\Quote\Api\Data\AddressInterface $address
25+
) {
26+
27+
$extAttributes = $address->getExtensionAttributes();
28+
29+
if (!empty($extAttributes)) {
30+
31+
foreach($this->helper->getExtraCheckoutAddressFields('extra_checkout_shipping_address_fields') as $extraField) {
32+
33+
$set = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
34+
$get = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
35+
36+
$value = $extAttributes->$get();
37+
try {
38+
$address->$set($value);
39+
} catch (\Exception $e) {
40+
$this->logger->critical($e->getMessage());
41+
}
42+
}
43+
}
44+
45+
}
46+
}

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Magento 2 Add Extra Address Fields to Checkout
2+
3+
- It renames extra address attribute fieldname, datascope to customAttributes.{attribute_code} LayoutProcessor
4+
- With a javascript mixin it transports them from customAttributes to extension_attributes
5+
- Plugin
6+
- In a event is transport the values from object to object. Example Quote Address to Order Address
7+
8+
Usage:
9+
10+
- Add a fieldset.xml
11+
- Add a extensions_attributes.xml
12+
- Add a customer_address attribute setup
13+
- Add a quote_address field setup
14+
- Add a order_address field setup
15+
16+
Example: See the module ExtraCheckoutAddressFieldsTest

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "experius/module-extracheckoutaddressfields",
3+
"description": "",
4+
"license": "proprietary",
5+
"authors": [
6+
{
7+
"email": "info@mage2gen.com",
8+
"name": "Mage2Gen"
9+
}
10+
],
11+
"minimum-stability": "dev",
12+
"require": {},
13+
"autoload": {
14+
"files": [
15+
"registration.php"
16+
],
17+
"psr-4": {
18+
"Experius\\ExtraCheckoutAddressFields\\": ""
19+
}
20+
}
21+
}

etc/di.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" ?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
3+
<type name="Magento\Quote\Model\BillingAddressManagement">
4+
<plugin disabled="false" name="Experius_ExtraCheckoutAddressFields_Plugin_Magento_Quote_Model_BillingAddressManagement" sortOrder="10" type="Experius\ExtraCheckoutAddressFields\Plugin\Magento\Quote\Model\BillingAddressManagement"/>
5+
</type>
6+
<type name="Magento\Quote\Model\ShippingAddressManagement">
7+
<plugin disabled="false" name="Experius_ExtraCheckoutAddressFields_Plugin_Magento_Quote_Model_ShippingAddressManagement" sortOrder="10" type="Experius\ExtraCheckoutAddressFields\Plugin\Magento\Quote\Model\ShippingAddressManagement"/>
8+
</type>
9+
</config>

etc/events.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
3+
<event name="sales_model_service_quote_submit_before">
4+
<observer instance="Experius\ExtraCheckoutAddressFields\Observer\Sales\ModelServiceQuoteSubmitBefore" name="experius_extracheckoutaddressfields_observer_sales_modelservicequotesubmitbefore_sales_model_service_quote_submit_before"/>
5+
</event>
6+
</config>

etc/frontend/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
3+
<type name="Magento\Checkout\Block\Onepage">
4+
<arguments>
5+
<argument name="layoutProcessors" xsi:type="array">
6+
<item name="experius_extra_checkout_address_fields_layoutprocessor" xsi:type="object">Experius\ExtraCheckoutAddressFields\Block\Checkout\LayoutProcessor</item>
7+
</argument>
8+
</arguments>
9+
</type>
10+
</config>

0 commit comments

Comments
 (0)