postcode-nl / PostcodeNl_Api_Magento2

A Magento 2 plugin that implements the Postcode.nl international address API
https://developer.postcode.eu/documentation/
BSD 2-Clause "Simplified" License
10 stars 12 forks source link

TypeError: Checkout\AttributeMerger::merge() must be of the type array, null given #62

Open mischabraam opened 1 year ago

mischabraam commented 1 year ago

With the module version 3.1.7.1 on Magento 2.4.5 on PHP 7.4 I receive the following critical error in the log.

report.CRITICAL: TypeError: Argument 4 passed to Magento\Checkout\Block\Checkout\AttributeMerger::merge() must be of the type array, null given, called in /vendor/magento/module-customer-custom-attributes/Block/Checkout/LayoutProcessor.php on line 196 and defined in /vendor/magento/module-checkout/Block/Checkout/AttributeMerger.php:134
Stack trace:
#0 /vendor/magento/module-customer-custom-attributes/Block/Checkout/LayoutProcessor.php(196): Magento\Checkout\Block\Checkout\AttributeMerger->merge(Array, 'checkoutProvide...', 'billingAddresss...', NULL)
#1 /vendor/magento/module-customer-custom-attributes/Block/Checkout/LayoutProcessor.php(88): Magento\CustomerCustomAttributes\Block\Checkout\LayoutProcessor->mergeCustomAttributesOfBillingAddress(Array, Array)

The module is enabled when I get this error and the checkout is not usable for customers. If I disable the module using configuration in Magento's admin the checkout works correct.

mischabraam commented 1 year ago

This error also happens on PHP 8.1 with Magento 2.4.5-p1. I've traced it to https://github.com/postcode-nl/PostcodeNl_Api_Magento2/blob/c7423e39405c89be5f0ffbd0f64465e777ea9a61/Block/Onepage/LayoutProcessor.php#L85 where the exception is thrown.

JerrySmidt commented 1 year ago

Customer Attributes is a module that is only available in Adobe Commerce. The Postcode.eu is developed only on Magento Open Source so we can't test this issue.

mischabraam commented 1 year ago

Any luck on this? I can imagine you don't have a Commerce license. Though many retailers do. It would be a shame not to support that.

yuriichayka commented 1 week ago

In PHP 8, accessing an array key by reference that doesn't exist will create that key in the array with NULL value That's exactly what happens in the module

Here is the path for fixing it:

diff --git vendor/postcode-nl/api-magento2-module/Block/Onepage/LayoutProcessor.php vendor/postcode-nl/api-magento2-module/Block/Onepage/LayoutProcessor.php
--- vendor/postcode-nl/api-magento2-module/Block/Onepage/LayoutProcessor.php
+++ vendor/postcode-nl/api-magento2-module/Block/Onepage/LayoutProcessor.php    (date 1725623104896)
@@ -87,20 +87,32 @@
             }
         }

-        // Billing address on payment page
-        $billingFields = &$jsLayout['components']
-            ['checkout']['children']
-            ['steps']['children']
-            ['billing-step']['children']
-            ['payment']['children']
-            ['afterMethods']['children']
-            ['billing-address-form']['children']
-            ['form-fields']['children'];
+        if (isset(
+            $jsLayout['components']
+            ['checkout']['children']
+            ['steps']['children']
+            ['billing-step']['children']
+            ['payment']['children']
+            ['afterMethods']['children']
+            ['billing-address-form']['children']
+            ['form-fields']['children']
+        )
+        ) {
+            // Billing address on payment page
+            $billingFields = &$jsLayout['components']
+                              ['checkout']['children']
+                              ['steps']['children']
+                              ['billing-step']['children']
+                              ['payment']['children']
+                              ['afterMethods']['children']
+                              ['billing-address-form']['children']
+                              ['form-fields']['children'];

-        if (isset($billingFields)) {
-            $billingFields += $this->_updateCustomScope($autofillFields, 'billingAddressshared');
-            $billingFields = $this->_updateDataScope($billingFields, 'billingAddressshared');
-            $billingFields = $this->_changeAddressFieldsPosition($billingFields);
+            if (isset($billingFields)) {
+                $billingFields += $this->_updateCustomScope($autofillFields, 'billingAddressshared');
+                $billingFields = $this->_updateDataScope($billingFields, 'billingAddressshared');
+                $billingFields = $this->_changeAddressFieldsPosition($billingFields);
+            }
         }

         // Compatibility
JerrySmidt commented 1 week ago

Thank you yuriichayka. We'll add a fix for this side effect to the next release.