magento2: Array to String conversion error on checkout page when changin country - how to debug

Preconditions

  1. Magento 2.2.1
  2. php and mysql updated from ubuntu 16.04 repositories

Steps to reproduce

  1. I guess it is difficult to reproduce and configuration dependent. I’d be happy for hints how to debug this. The payment methods are set available for all allowed countries. There are shipping tablerates for the respictive countries too.
  2. Add one or more product(s) to cart
  3. Go to (onepage) checkout page
  4. Change the country

Expected result

  1. Shipping method and tax values and payment providers should change according to country selection.

Actual result

  1. Sometimes client sees a short red error message above the checkout page about Array to string conversion. In that case following is seen in var/log/exception.log: [2017-12-08 03:48:23] main.CRITICAL: Report ID: webapi-5a2a0b8746dca; Message: Notice: Array to string conversion in /var/www/XXX/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2999 {"exception":"[object] (Exception(code: 0): Report ID: webapi-5a2a0b8746dca; Message: Notice: Array to string conversion in /var/www/XXX/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2999 at /var/www/XXX/vendor/magento/framework/Webapi/ErrorProcessor.php:205, Exception(code: 0): Notice: Array to string conversion in /var/www/XXX/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2999 at /var/www/XXX/vendor/magento/framework/App/ErrorHandler.php:61)"} [] The corresponding (shipping, tax, payment) values do not always change. Sometimes the error is not shown in frontend, then the log message differs slightly. In between configuration changes or separete tests I flush caches and clear the browser session (FF, Ctrl+F5).

[2017-12-07 23:57:04] main.CRITICAL: Notice: Array to string conversion in /var/www/XXX/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2999 {"exception":"[object] (Exception(code: 0): Notice: Array to string conversion in /var/www/XXX/vendor/magento/framework/DB/Adapter/Pdo/Mysql.php on line 2999 at /var/www/XXX/vendor/magento/framework/App/ErrorHandler.php:61)"} [] I am sorry not to be able to be more precise here but any help on getting a more detailed stacktrace or more information on where the error actually comes from would be greatly appreciated. I hacked the corresponding Mysql.php file and printed the “array” contents into a file. It showed up an empty list (“()” afair).

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 21 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Here are the exact steps to reproduce, I tested it multiple times to make sure I didn’t make any mistakes

Preconditions

  1. Clean Magento instance, code from 2.1-develop branch, current commit: c1b771005bd
  2. PHP 7.0.27

Steps to reproduce

  1. Go to the backend of Magento
  2. Create a category and product, so you have something to put in the cart
  3. Go to Stores => Configuration => Customers => Customer Configuration => Create New Account Options
  4. Change these settings:
    • Enable Automatic Assignment to Customer Group => Yes
    • Tax Calculation Based On => Shipping Address
    • Validate on Each Transaction => Yes
    • Show VAT Number on Storefront => Yes
    • The rest can stay as is, defaults are fine
  5. Flush the cache
  6. In the frontend, create a new account
  7. Then while you are logged in, in the my account section, go to Address Book and add a new address with a VAT Number (doesn’t have to be valid, just use ‘0123’ or something), pick Belgium as country (any EU country should work, but I tested with Belgium)
  8. Create yet another address, use a different name, but the same address (again Belgium) and the same VAT Number, check ‘Use as my default billing address’
  9. Verify that you now have a default shipping and default billing address which have the same address, but only differ in their name
  10. Add the test product to your cart
  11. Go to the checkout

Expected result

  1. Should see no error and a shipping method available to pick from

Actual result

  1. Getting the error:
Notice: Array to string conversion in lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php on line 2929
  1. Seeing no shipping methods to pick from

Fix which hasn’t been tested very well

I wrote a small module with a preference for the Magento\Quote\Observer\Frontend\Quote\Address\VatValidator and copied the validate method and added some extra lines of code:

@@ -63,6 +34,20 @@
                 $merchantVatNumber
             );

+            if (is_array($quoteAddress->getRegion()))
+            {
+                $regionData = $quoteAddress->getRegion();
+                if (array_key_exists('region_code', $regionData))
+                {
+                    $quoteAddress->setRegionCode($regionData['region_code']);
+                }
+                if (array_key_exists('region_id', $regionData))
+                {
+                    $quoteAddress->setRegionId($regionData['region_id']);
+                }
+                $quoteAddress->setRegion(null);
+            }
+
             // Store validation results in corresponding quote address
             $quoteAddress->setVatIsValid((int)$validationResult->getIsValid());
             $quoteAddress->setVatRequestId($validationResult->getRequestIdentifier());

This is most likely not the correct solution, but it seems to work…

@magento-engcom-team: can you double check and verify and then add the G3 Passed label once verified? Thanks!

I also ran into this issue. From what I am seeing this happens only to users who get their customer group emulated (and vat number validated) during checkout. (customers with a valid tax id shipping to another country within the EU that is)

When Magento fetches an address object from db it seems to turn the region data into an array (Magento\Customer\Model\Address\AbstractAddress::getDataModel, Line 533).

If later on vat validation is performed (Magento\Quote\Observer\Frontend\Quote\Address\VatValidator, Line 43) there is a (\Magento\Quote\Model\Quote\Address) Model being saved that still has the region split as array.

As the data is getting persisted into quote_address where region is a varchar obviously Magento is trying to convert the array into a string which produces the error.

This does not look like an error caused by extensions or migrated data.

@hostep , sorry for mistake, we rechecked this issue and added all correct labeles

@hostep Since the region data was of no importance in our case, we fixed this by simply unsetting the region data before vat validation was performed in a Plugin.
(We also targeted the Magento\Quote\Observer\Frontend\Quote\Address\VatValidator::validate Method)

I’m also unsure if this method is the correct place to add the region splitting, this is something @magento-engcom-team needs to decide. Hopefully they now acknowledge the existence of this issue.