0

I have a problem when trying to modify an order through a cronjob this code works perfectly, but when executing it inside the cronjob it indicates that error "Request does not match any route"

<?php
    namespace Vendor\Module\Cron;
    use Magento\Sales\Model\Order;
    class cronTest
    {
        public function __construct(
        ) {
            
        }
        public function execute()
        {
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $orderId = 8;
            $order = $objectManager->get('\Magento\Sales\Model\Order')->load($orderId);
            $orderState = Order::STATE_PROCESSING;
            $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
            $order->save();
        }
    }

if I use this same code in an observer for example, it works without any problem

My crontab.xml is

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
    <group id="default">
        <job instance="Vendor\Module\Cron\cronTest" method="execute" name="testCronJob">
            <schedule>* * * * *</schedule> 
        </job>
    </group>
</config>

I have found a temporary solution, create an api and consume it from the cronjob to get the desired behavior

4
  • please share your crontab.xml file code Aug 23, 2020 at 16:34
  • hello friend there share the crontab.xml, thank you very much for any help you can give me Aug 23, 2020 at 16:54
  • what you want to change in order please let me know. Aug 23, 2020 at 17:06
  • my end goal is to change the price and quantity of the products, but suppose I need to change the order status. Aug 23, 2020 at 17:16

1 Answer 1

0

Please find the working code below

app\code\Vendor\CronTest\Cron\CronTest.php

<?php
declare(strict_types=1);
namespace Vendor\CronTest\Cron;
use Magento\Sales\Model\Order;
class CronTest
{

protected $logger;

/**
 * Constructor
 *
 * @param \Psr\Log\LoggerInterface $logger
 */
public function __construct(\Psr\Log\LoggerInterface $logger)
{
    $this->logger = $logger;
}

/**
 * Execute the cron
 *
 * @return void
 */
public function execute()
{
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/crontest.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);
    $this->logger->addInfo("Cronjob CronTest is executed.");
    try {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $orderId = 25;
        $order = $objectManager->get('\Magento\Sales\Model\Order')->load($orderId);
        $orderState = Order::STATE_PROCESSING;
        $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
        $order->save();
    } catch (\Exception $e) {
        $this->logger->addInfo($e->getMessage());
    }
}
}

app/code/Vendor/CronTest/etc/crontab.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
    <job instance="Vendor\CronTest\Cron\CronTest" method="execute" name="vendor_crontest_crontest">
        <schedule>* * * * *</schedule>
    </job>
</group>

Use below cmd to execute cron job

sudo php bin/magento cron:run --group="default"

Please check below screen shot of order status Before Cron job enter image description here

**

After Cron job

**

enter image description here

2
  • I tried to run the code, but the same problem keeps happening [2020-08-23 18:10:03] main.INFO: Cronjob CronTest is executed. [] [] [2020-08-23 18:10:03] main.INFO: Request does not match any route. [] [] Aug 23, 2020 at 18:10
  • main.CRITICAL: Request does not match any route. {"exception":"[object] (Magento\\Framework\\Webapi\\Exception(code: 0): Request does not match any route. at /var/www/html/vendor/magento/module-webapi/Controller/Rest/Router.php:57)"} [] Aug 23, 2020 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.