Prestashop 1.7模块命令调用$cart->getOrderTotal触发致命错误求助
PrestaShop 1.7 模块调用
$cart->getOrderTotal触发致命错误的解决方案 问题场景
在PrestaShop 1.7模块中创建控制台命令,尝试从指定购物车生成订单时,调用$cart->getOrderTotal始终抛出致命错误。
相关代码
protected function execute(InputInterface $input, OutputInterface $output){ try{ $cart = new Cart(2433); $total = (float)$cart->getOrderTotal(true, Cart::BOTH); $output->writeln('total '. $total); }catch (Exception $e){ $output->writeln('error'. $e->getMessage()); die(); } }
错误栈信息
Fatal error - #0 /project/classes/Product.php(3623): ToolsCore::displayError() #1 /project/classes/Product.php(5632): ProductCore::getPriceStatic(20, false, 0, 6, NULL, false, true, 1) #2 /project/classes/Cart.php(833): ProductCore::getProductProperties(2, Array) #3 /project/classes/Cart.php(2144): CartCore->getProducts(false, false, NULL, true, false) #4 /project/modules/order_install/src/Command/CheckOrderCreationCommand.php(105): CartCore->getOrderTotal(true, 4) #5 /project/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php(255): order_install\Command\CheckOrderCreationCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #6 /project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(1010): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #7 /project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(86): Symfony\Component\Console\Application->doRunCommand(Object(order_install\Command\CheckOrderCreationCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #8 /project/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(255): Symfony\Bundle\FrameworkBundle\Console\Application->doRunCommand(Object(order_install\Command\CheckOrderCreationCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #9 /project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(74): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #10 /project/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(148): Symfony\Bundle\FrameworkBundle\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput)) #11 /project/bin/console(27): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput)) #12 {main}
问题分析与修复
从错误栈可以定位到,问题根源是ProductCore::getPriceStatic调用时触发了ToolsCore::displayError。控制台环境下PrestaShop默认缺少完整的上下文(店铺、语言、客户信息),导致计算订单总额时无法正确获取产品属性与价格。
修复步骤:
- 在实例化
Cart前,初始化核心上下文,确保店铺、语言、客户信息正确加载:
protected function execute(InputInterface $input, OutputInterface $output){ try{ // 初始化店铺上下文(替换为你的实际店铺ID) Context::getContext()->shop = new Shop(1); // 初始化语言上下文(替换为你的实际语言ID) Context::getContext()->language = new Language(1); // 关联购物车所属客户到上下文 $cart = new Cart(2433); if ($cart->id_customer) { Context::getContext()->customer = new Customer($cart->id_customer); } $total = (float)$cart->getOrderTotal(true, Cart::BOTH); $output->writeln('total '. $total); }catch (Exception $e){ $output->writeln('error: '. $e->getMessage()); die(); } }
- 验证购物车ID
2433的有效性,确保该购物车存在,且关联的产品、客户数据完整。 - 若需要更详细的错误信息,可在命令开头开启调试模式:
define('_PS_MODE_DEV_', true);
内容的提问来源于stack exchange,提问作者Malek Ben El Ouafi
相关产品推荐
相关产品推荐

