vasims_zignutsvasims_zignuts MemberComments: 23 Received thanks: 0 Member since: September 27 edited October 4

I would like run cron job every 30 minutes to check items in the warehouse. We will update stock based on that. When I check by CLI tool

sw:cron:run ExampleStockManagerCron

. This is working as expected but the interval is not working. Is there any issue with any settings I need.

A plugin Root file:


<?php

namespace SwagExample;

use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;

class SwagExample extends Plugin
{
 
    /**
     * Install Plugin
     * 
     * Create a  table when plugin is activate
     */
    public function install(InstallContext $context): void
    {
        
         $this->addCron();
    }

    /**
     *  Uninstall Plugin
     * 
     *  Drop a table when plugin uninstall
     */
    public function uninstall(UninstallContext $context): void
    {
       $this->removeCron();
    }


    public function addCron()
    {
        $connection = $this->container->get('dbal_connection');
        $connection->insert(
            's_crontab',
            [
                'name'             => 'ExampleStockManagerCron',
                'action'           => 'ExampleStockManagerCron',
                'next'             => new \DateTime(),
                'start'            => null,
                '`interval`'       => '100',
                'active'           => 1,
                'end'              => new \DateTime(),
                'pluginID'         => null
            ],
            [
                'next' => 'datetime',
                'end'  => 'datetime',
            ]
        );
    }
    public function removeCron()
    {
        $this->container->get('dbal_connection')->executeQuery('DELETE FROM s_crontab WHERE `name` = ?', [
            'ExampleStockManagerCron'
        ]);
return;
    }

   
}

?>

An XML for Cron Register:

<?xml version="1.0" encoding="utf-8"?>
<cronjobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
          xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/shopware/5.3/engine/Shopware/Components/Plugin/schema/cronjob.xsd">;

    <cronjob>
        <name>StockManagerCron</name>
        <action>Shopware_CronJob_ExampleStockManagerCron</action>
        <active>true</active>
        <interval>300</interval>
        <disableOnError>true</disableOnError>
    </cronjob>
</cronjobs>

A Subscriber Class

 

<?php
namespace SwagExample\Subscribers;
use SwagExample\ExampleAPI;
use Enlight\Event\SubscriberInterface;

class ExampleSubscriber extends ExampleHelper implements SubscriberInterface
{


   public static function getSubscribedEvents()
    {
        return [
    
            'Shopware_CronJob_ExampleStockManagerCron' => 'ExampleStockManagerCron'
           
        ];

    } 


    /**
     *   APIStockManagerCron() this will update stock based on API
     */
    public function ExampleStockManagerCron(\Shopware_Components_Cron_CronJob $job)
    {
               
       $responseMsg = 'Product id '.$value['ordernumber'].' updated with QTY '.$goodsQTY;
       $sql = "INSERT INTO `s_plugin_SwagAPI_log` (`id`, `response_desc`) VALUES (NULL, '$responseMsg')";            
       Shopware()->Db()->query($sql);       
        
    }
    
  
}

 

Answers

Sign In or Register to comment.