Skip to content
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

Error in vendor/magento/module-shipping/Model/Config/Source/Allmethods.php - public function toOptionArray #13136

Closed
Tantuss opened this issue Jan 11, 2018 · 15 comments
Assignees
Labels
Component: Shipping Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@Tantuss
Copy link

Tantuss commented Jan 11, 2018

Preconditions

Magento 2.2.2 but also 2.2.1

Steps to reproduce

This code is used by an external extension, but this should just work.
See also: magmodules/magento2-channable#28
But this is actually not the extensions bug, it is a bug in Magento 2.2 code.
See below my explanation.
I have no idea where else this is used in Magento, but if it is, there should be also an error visible.

Expected result

Dropdown box with all carriers, active and inactive.

Actual result

1 exception(s):
Exception #0 (Exception): Notice: Array to string conversion in vendor/magento/module-shipping/Model/Config/Source/Allmethods.php on line 61

Exception #0 (Exception): Notice: Array to string conversion in vendor/magento/module-shipping/Model/Config/Source/Allmethods.php on line 61
#0 vendor/magento/module-shipping/Model/Config/Source/Allmethods.php(61): Magento\Framework\App\ErrorHandler->handler(8, 'Array to string...', '/Users/jw/Proje...', 61, Array)
#1 vendor/magento/module-config/Model/Config/Structure/Element/Field.php(458): Magento\Shipping\Model\Config\Source\Allmethods->toOptionArray(false)
#2 vendor/magento/module-config/Model/Config/Structure/Element/Field.php(376): Magento\Config\Model\Config\Structure\Element\Field->_getOptionsFromSourceModel(Object(Magento\Shipping\Model\Config\Source\Allmethods))


Looking into this Magento code on this line:
'label' => '[' . $carrierCode . '] ' . $methodTitle,

At the $carrierCode = 'usps', I get an array returned.
This results in the error above.

So a simple quick fix is to change:

$methods[$carrierCode]['value'][] = [
'value' => $carrierCode . '_' . $methodCode,
'label' => '[' . $carrierCode . '] ' . $methodTitle,
];

into:
if(!is_array($methodTitle))
$methods[$carrierCode]['value'][] = [
'value' => $carrierCode . '_' . $methodCode,
'label' => '[' . $carrierCode . '] ' . $methodTitle,
];

But this is not the complete correct solution, but it makes my page working.

UPD from @joni-jones

To reproduce this issue you need to manually insert carries/usps/allowed_methods NULL into core_config_data table and call \Magento\Shipping\Model\Config\Source\Allmethods::toOptionArray method.

The issue not in the \Magento\Shipping\Model\Config\Source\Allmethods::toOptionArray but how \Magento\Usps\Model\Carrier::getAllowedMethods (and other similar methods) processes invalid configuration.

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Jan 11, 2018
@Tantuss
Copy link
Author

Tantuss commented Jan 11, 2018

To make it more complete..
If you look here:
vendor/magento/module-usps/Model/Carrier.php

Line 1148: public function getAllowedMethods()

You see that the return of this function is an array.

So the error is a logical result.

@YevSent
Copy link
Contributor

YevSent commented Jan 11, 2018

Hi, @Tantuss, did you get the same error on the fresh installation of Magento without extensions? I've checked described case and I didn't get any errors.

According to a described example with USPS, the $carrierMethods contains array in the following format:

array (
  '0_FCLE' => 
  Magento\Framework\Phrase::__set_state(array(
     'text' => 'First-Class Mail Large Envelope',
  )),
...
  1 => 
  Magento\Framework\Phrase::__set_state(array(
     'text' => 'Priority Mail',
  )),
...
  'INT_20' => 
  Magento\Framework\Phrase::__set_state(array(
     'text' => 'Priority Mail International Small Flat Rate Envelope',
  )),
)

So, $methodTitle is a string, not an array.

@Tantuss
Copy link
Author

Tantuss commented Jan 12, 2018

Of course I do not try it on a fresh installation without any configurations.
I assume Magento is not developed to be only installed default and that configuration should be done.

Set in the core_config_data table the path:
carriers/usps/allowed_methods
to a value of 'NULL'.

(so basically nothing selected)

Then you get back the array at the wrong variable.

So probably the mistake is in the USPS extension. So here:
vendor/magento/module-usps/Model/Carrier.php
Line 1148: public function getAllowedMethods()

@YevSent
Copy link
Contributor

YevSent commented Jan 12, 2018

Of course I do not try it on a fresh installation without any configurations.
I assume Magento is not developed to be only installed default.

It helps to understand if extensions could be a reason for this issue.

Set in the core_config_data table the path:
carriers/usps/allowed_methods
to a value of 'NULL'.
(so basically nothing selected)

This behavior doesn't seem correct because you don't have a possibility to don't select at least one allowed method, also, by default, the Use system value option is enabled for allowed shipping methods and the configuration will be read from XML because there are no any USPS configurations in core_config_data table.

So, the described case is artificial it might happen only if you manually set invalid data into the database. But agree, the code should validate such behavior.

@Tantuss
Copy link
Author

Tantuss commented Jan 12, 2018

It might be possible in Magento 1 and it might be there because of migration from Magento 1 to 2.

@YevSent YevSent added Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Component: Shipping Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release and removed Progress: needs update labels Jan 12, 2018
@Tantuss
Copy link
Author

Tantuss commented Jan 12, 2018

This behavior doesn't seem correct because you don't have a possibility to don't select at least one allowed method, also, by default, the Use system value option is enabled for allowed shipping methods and the configuration will be read from XML because there are no any USPS configurations in core_config_data table.

It might seem not correct, but it is definitely possible to do it from the user interface. Just set 1 as active, saved, checked db and it was there. Then unselected that 1 and there was again NULL in the database.

So it is not only related to migration, it is also just easy possible from the UI.

@Waldemar-Denke
Copy link

Waldemar-Denke commented Feb 6, 2018

I have same error. Magento 2.2.2, IWD_Opc Extension, Migration from M1 to M2.
On the configuration page of IWD_Opc in magento backend i get the error.
Figured out, that DHL, UPS and Fedex are objects. USPS is an array.

To figure out the problem i've changed code of toOptionArray():

{
        $methods = [['value' => '', 'label' => '']];
        $carriers = $this->_shippingConfig->getAllCarriers();
        foreach ($carriers as $carrierCode => $carrierModel) {
            if (!$carrierModel->isActive() && (bool)$isActiveOnlyFlag === true) {
                continue;
            }
            $carrierMethods = $carrierModel->getAllowedMethods();
            if (!$carrierMethods) {
                continue;
            }
            $carrierTitle = $this->_scopeConfig->getValue(
                'carriers/' . $carrierCode . '/title',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
            );
            $methods[$carrierCode] = ['label' => $carrierTitle, 'value' => []];
            foreach ($carrierMethods as $methodCode => $methodTitle) {
                if(is_string($methodTitle)){
                    $methods[$carrierCode]['value'][] = [
                        'value' => $carrierCode . '_' . $methodCode,
                        'label' => '[' . $carrierCode . '] ' . $methodTitle,
                    ];
                } else if (is_object($methodTitle) ){
                    $methods[$carrierCode]['value'][] = [
                        'value' => $carrierCode . '_' . $methodCode,
                        'label' => '[' . $carrierCode . '] ' . $methodTitle->getText(),
                    ];
                } else if(is_array($methodTitle)) {
                    foreach($methodTitle as $key => $value){
                        $methods[$carrierCode]['value'][] = [
                            'value' => $carrierCode . '_' . $key,
                            'label' => '[' . $carrierCode . '] ' . $value->getText(),
                        ];
                    }
                }
            }
        }
        return $methods;
    }

possible fix is to check if the method is active or not, directly in the module.

@magento-engcom-team magento-engcom-team added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed labels Feb 27, 2018
@magento-engcom-team
Copy link
Contributor

@Tantuss, thank you for your report.
We've acknowledged the issue and added to our backlog.

@diazwatson diazwatson self-assigned this Nov 15, 2018
@magento-engcom-team
Copy link
Contributor

Hi @diazwatson. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if your want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • 4. If the issue is not relevant or is not reproducible any more, feel free to close it.

@orozcodiaz orozcodiaz self-assigned this May 12, 2019
@m2-assistant
Copy link

m2-assistant bot commented May 12, 2019

Hi @oroskodias. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento-engcom-team give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. Verify that the issue is reproducible on 2.2-develop branch.

    Details- Add the comment @magento-engcom-team give me 2.2-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.2-develop branch, please add the label Reproduced on 2.2.x

  • 4. If the issue is not relevant or is not reproducible any more, feel free to close it.


@orozcodiaz
Copy link
Contributor

@magento-engcom-team give me 2.3-develop instance

@magento-engcom-team
Copy link
Contributor

Hi @oroskodias. Thank you for your request. I'm working on Magento 2.3-develop instance for you

@magento-engcom-team
Copy link
Contributor

Hi @oroskodias, here is your Magento instance.
Admin access: https://i-13136-2-3-develop.instances.magento-community.engineering/admin
Login: admin Password: 123123q
Instance will be terminated in up to 3 hours.

@ghost ghost unassigned orozcodiaz Sep 27, 2019
@mrodespin mrodespin self-assigned this Oct 27, 2019
@m2-assistant
Copy link

m2-assistant bot commented Oct 27, 2019

Hi @mrodespin. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


@VladimirZaets
Copy link
Contributor

Hi @Tantuss. Thank you for your report.
The issue has been fixed in #25315 by @mrodespin in 2.4-develop branch
Related commit(s):

The fix will be available with the upcoming 2.4.0 release.

@VladimirZaets VladimirZaets added the Fixed in 2.4.x The issue has been fixed in 2.4-develop branch label Dec 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Shipping Fixed in 2.4.x The issue has been fixed in 2.4-develop branch Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.1.x The issue has been reproduced on latest 2.1 release Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

8 participants