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+ }
0 commit comments