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

[3.x]: Object of class <shipping method> could not be converted to int when calling getAvailableShippingMethodOptions #3093

Closed
OscarBarrett opened this issue Feb 17, 2023 · 9 comments
Assignees
Labels
bug commerce3 Issues related to Commerce v3

Comments

@OscarBarrett
Copy link

What happened?

Description

When calling $order->getAvailableShippingMethodOptions() on a beforeSave event, we're getting an exception here.

$option->matchesOrder = ArrayHelper::isIn($method->handle, $availableMethods);

In our case we're running into this with an object of class verbb\postie\models\ShippingMethod.

Related to #2986 and 6bf9ebd
Related Stack Exchange post: https://craftcms.stackexchange.com/questions/40448/object-of-class-craft-commerce-models-shippingmethod-could-not-be-converted-to-i

Stack trace:

yii\base\ErrorException: Object of class verbb\postie\models\ShippingMethod could not be converted to int
#25 /app/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php(853): yii\base\ErrorHandler::handleError
#24 /app/vendor/craftcms/cms/src/web/ErrorHandler.php(87): craft\web\ErrorHandler::handleError
#23 /app/vendor/sentry/sentry/src/ErrorHandler.php(305): Sentry\ErrorHandler::handleError
#22 [internal](0): in_array
#21 /app/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php(853): yii\helpers\BaseArrayHelper::isIn
#20 /app/vendor/craftcms/commerce/src/elements/Order.php(1899): craft\commerce\elements\Order::getAvailableShippingMethodOptions
#19 /app/modules/cart-helpers/services/OrderService.php(25): cartHelpers\services\OrderService::autoSetShippingMethod
#18 /app/modules/cart-helpers/Module.php(120): cartHelpers\Module::cartHelpers\{closure}
#17 [internal](0): call_user_func
#16 /app/vendor/yiisoft/yii2/base/Event.php(312): yii\base\Event::trigger
#15 /app/vendor/yiisoft/yii2/base/Component.php(642): yii\base\Component::trigger
#14 /app/vendor/craftcms/cms/src/base/Element.php(4138): craft\base\Element::beforeSave
#13 /app/vendor/craftcms/cms/src/services/Elements.php(2527): craft\services\Elements::_saveElementInternal
#12 /app/vendor/craftcms/cms/src/services/Elements.php(785): craft\services\Elements::saveElement
#11 /app/vendor/craftcms/commerce/src/services/Carts.php(122): craft\commerce\services\Carts::getCart
#10 /app/vendor/craftcms/commerce/src/controllers/CartController.php(487): craft\commerce\controllers\CartController::_getCart
#9 /app/vendor/craftcms/commerce/src/controllers/CartController.php(69): craft\commerce\controllers\CartController::actionGetCart
#8 [internal](0): call_user_func_array
#7 /app/vendor/yiisoft/yii2/base/InlineAction.php(57): yii\base\InlineAction::runWithParams
#6 /app/vendor/yiisoft/yii2/base/Controller.php(178): yii\base\Controller::runAction
#5 /app/vendor/yiisoft/yii2/base/Module.php(552): yii\base\Module::runAction
#4 /app/vendor/craftcms/cms/src/web/Application.php(293): craft\web\Application::runAction
#3 /app/vendor/yiisoft/yii2/web/Application.php(103): yii\web\Application::handleRequest
#2 /app/vendor/craftcms/cms/src/web/Application.php(278): craft\web\Application::handleRequest
#1 /app/vendor/yiisoft/yii2/base/Application.php(384): yii\base\Application::run
#0 /index.php(21): null

Further context:
image

Steps to reproduce

  1. Set up a custom shipping method
  2. Set up a beforeSave event, and call $order->getAvailableShippingMethodOptions()

Craft CMS version

3.7.63.1

Craft Commerce version

3.4.19

PHP version

7.4.33

Operating system and version

Alpine, latest

Database type and version

MySQL 5.7

Image driver and version

N/A

Installed plugins and versions

  • verbb/postie 2.4.21
@OscarBarrett OscarBarrett added commerce3 Issues related to Commerce v3 bug labels Feb 17, 2023
@pdaleramirez pdaleramirez added 🔎 status: investigating Trying to reproduce ℹ️ status: need more info When waiting for user to supply database or more information. and removed 🔎 status: investigating Trying to reproduce labels Feb 21, 2023
@pdaleramirez
Copy link
Contributor

@OscarBarrett, I can’t seem to replicate the issue. It appears to be an issue on how the custom shipping got registered. Have you contacted the third party developer about the issue?

@zollf
Copy link

zollf commented Feb 21, 2023

Hey, @pdaleramirez I'm a colleague of @OscarBarrett just for context.

verbb\postie\models\ShippingMethod uses properties from its provider. So in this case the shipping provider handle is 76, which somehow turns into an int.

Doing

$option->matchesOrder = ArrayHelper::isIn($method->getHandle(), $availableMethods);

ends up working as within getHandle() as it casts the handle to string, so it seems to be specific to ints

Take a look at this

use craft\commerce\models\ShippingMethod;

$shippingMethod = new ShippingMethod();
$shippingMethod->handle = 76;
$shippingMethod->name = "Test";
$shippingMethod->enabled = true;

$allShippingMethods = [];
$allShippingMethods[$shippingMethod->handle] = $shippingMethod;

print(in_array('76', $allShippingMethods)); // doesn't throw error
print(in_array(76, $allShippingMethods)); // throws error: could not be converted to int

You can see this in the Sentry stack trace:
Annotation on 2023-02-21 at 16-34-00

It seems to be similar to the SO case linked in the description, where they somehow got an int handle.

#2 /var/www/sslptest.com/vendor/yiisoft/yii2/helpers/BaseArrayHelper.php(853): in_array(349, Array, false)

Additional we could make an issue to verbb/postie, however it wouldn't solve the problem of creating a shippingMethod with int as a handle.

@pdaleramirez
Copy link
Contributor

@zollf, The shipping method handle, should not be an integer, but I’ve added a fix for this issue. It forces the shipping handle to return as a string when the handle is numeric.

@jacobrussell
Copy link

@pdaleramirez I can confirm that we've run into the same issue. Do you know when you'll have a release with that fix?

@pdaleramirez
Copy link
Contributor

To get the fix early, change your craftcms/commerce requirement in composer.json to:

"require": {
  "craftcms/commerce": "v3.x-dev#c9cdda166dcbaecaaee6e0ac13e9a0aa7f0f8b6c",
  "...": "..."
}

Then run composer update.

@joshparylak
Copy link

joshparylak commented Mar 1, 2023

@pdaleramirez I'm the poster from the related SO post. Just tried this fix using this version, ran into an error that resolves when we move back to the published version:

ParseError: syntax error, unexpected ')', expecting '|' or variable (T_VARIABLE) in /var/www/domain.com/vendor/craftcms/commerce/src/elements/Product.php:319
Stack trace:
#0 /var/www/domain.com/vendor/composer/ClassLoader.php(346): Composer\Autoload\includeFile('/var/www/sslpte...')
#1 [internal function]: Composer\Autoload\ClassLoader->loadClass('craft\\commerce\\...')
#2 /var/www/domain.com/modules/appmodule/Module.php(107): spl_autoload_call('craft\\commerce\\...')
#3 /var/www/domain.com/modules/appmodule/Module.php(42): modules\appmodule\Module->registerEvents()
#4 /var/www/domain.com/vendor/yiisoft/yii2/base/BaseObject.php(109): modules\appmodule\Module->init()
#5 /var/www/domain.com/vendor/yiisoft/yii2/base/Module.php(161): yii\base\BaseObject->__construct(Array)
#6 [internal function]: yii\base\Module->__construct('appmodule', Object(craft\web\Application), Array)
#7 /var/www/domain.com/vendor/yiisoft/yii2/di/Container.php(411): ReflectionClass->newInstanceArgs(Array)
#8 /var/www/domain.com/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build('modules\\appmodu...', Array, Array)
#9 /var/www/domain.com/vendor/yiisoft/yii2/BaseYii.php(345): yii\di\Container->get('modules\\appmodu...', Array)
#10 /var/www/domain.com/vendor/yiisoft/yii2/base/Module.php(445): yii\BaseYii::createObject('modules\\appmodu...', Array)
#11 /var/www/domain.com/vendor/yiisoft/yii2/base/Application.php(313): yii\base\Module->getModule('appmodule')
#12 /var/www/domain.com/vendor/craftcms/cms/src/web/Application.php(116): yii\base\Application->bootstrap()
#13 /var/www/domain.com/vendor/yiisoft/yii2/base/Application.php(271): craft\web\Application->bootstrap()
#14 /var/www/domain.com/vendor/craftcms/cms/src/web/Application.php(93): yii\base\Application->init()
#15 /var/www/domain.com/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init()
#16 /var/www/domain.com/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct(Array)
#17 [internal function]: yii\base\Application->__construct(Array)
#18 /var/www/domain.com/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs(Array)
#19 /var/www/domain.com/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build('craft\\web\\Appli...', Array, Array)
#20 /var/www/domain.com/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get('craft\\web\\Appli...', Array, Array)
#21 /var/www/domain.com/vendor/craftcms/cms/src/Craft.php(61): yii\BaseYii::createObject(Array, Array)
#22 /var/www/domain.com/vendor/craftcms/cms/bootstrap/bootstrap.php(247): Craft::createObject(Array)
#23 /var/www/domain.com/vendor/craftcms/cms/bootstrap/web.php(51): require('/var/www/sslpte...')
#24 /var/www/domain.com/public/index.php(21): require('/var/www/sslpte...')
#25 {main}

@pdaleramirez
Copy link
Contributor

@joshparylak Your issue seems unrelated; it comes from a custom module.

@joshparylak
Copy link

@pdaleramirez Looks like we actually had to scale up to php 8 to clear this error, which would be a no-go at the moment for production for us.

Clearing that error has run us into a number of other errors, and we've been yet unable to get the site running stably on this branch.

What's the timeline for an official release version of this potential fix look like?

@lukeholder
Copy link
Member

This PHP error has been fixed and is the 3.4.20.1 release. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug commerce3 Issues related to Commerce v3
Projects
None yet
Development

No branches or pull requests

7 participants