在命令 Prestashop 中使用 getOrderTotal

问题描述 投票:0回答:1

我正在尝试在 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/Component/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}
 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();
    }
}
prestashop-1.7 prestashop-modules
1个回答
0
投票

您遇到的问题似乎是由于 PrestaShop 核心未在您的脚本中正确初始化,这可能导致您在 getPriceStatic() 方法中提到的错误。

要解决此问题,您需要确保 PrestaShop 的环境设置正确。

确保包含 init.php ,类似

require_once('/path/to/prestashop/config/config.inc.php');

还要确保 Context 和 Employee 已初始化

$context = Context::getContext();
$context->employee = new Employee((int)Configuration::get('PS_EMPLOYEE_ID'));

如果您不确定员工 ID,您可以在默认 PrestaShop 设置中使用 1 作为超级管理员帐户。

© www.soinside.com 2019 - 2024. All rights reserved.