1

my code is

Vender/Module/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 name="check_return_cron" instance="Vender\Module\Cron\CheckReturn" method="execute">

        <schedule>0 21 * * *</schedule>

    </job>

</group>

</config>

my php class is

Vender/Module/Cron/CheckReturn.php

<?php

namespace Mymodules\SMS\Cron;

use Magento\Framework\Api\FilterBuilder;
use Magento\Framework\Api\Search\FilterGroup;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Api\OrderManagementInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;

use \Psr\Log\LoggerInterface;

class CheckReturn {

    protected $logger;

  /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var Order                 //These two lines are added by Asad
     */
    private $order;

    /**
     * @var FilterBuilder
     */
    private $filterBuilder;

    /**
     * @var FilterGroup
     */
    private $filterGroup;

    /**
     * @var OrderManagementInterface
     */
    private $orderManagement;

  public function __construct( 
      LoggerInterface $logger,
      OrderRepositoryInterface $orderRepository,
      SearchCriteriaBuilder $searchCriteriaBuilder,
      FilterBuilder $filterBuilder,
      FilterGroup $filterGroup,
      OrderManagementInterface $orderManagement,
      Order $order                                 //This line is added by Asad 
    ) {
      $this->logger = $logger;
      $this->orderRepository       = $orderRepository;
      $this->searchCriteriaBuilder = $searchCriteriaBuilder;
      $this->filterBuilder         = $filterBuilder;
      $this->filterGroup           = $filterGroup;
      $this->orderManagement       = $orderManagement;
      $this->_changeStatus = $order;                         //This line is added by Asad
  }

  /**

    * Write to system.log

    *

    * @return void

  */

  public function execute() {

    // https://www.dckap.com/blog/how-to-set-and-configure-custom-cron-jobs-in-magento-2/

    // Do your Stuff

    $today          = date("Y-m-d h:i:s");
    $to             = strtotime('-15 day', strtotime($today));
    $to             = date('Y-m-d h:i:s', $to);

    $filterGroupDate      = $this->filterGroup;
    // $filterGroupStatus    = clone($filterGroupDate);

    $filterDate      = $this->filterBuilder
        ->setField('updated_at')
        ->setConditionType('from')
        ->setValue($to)
        ->create();
    // $filterStatus    = $this->filterBuilder
    //     ->setField('status')
    //     ->setConditionType('eq')
    //     ->setValue('pending_payment')
    //     ->create();

    $filterGroupDate->setFilters([$filterDate]);
    // $filterGroupStatus->setFilters([$filterStatus]);

    // $searchCriteria = $this->searchCriteriaBuilder->setFilterGroups(
    //     [$filterGroupDate, $filterGroupStatus]
    // );
    $searchCriteria = $this->searchCriteriaBuilder->setFilterGroups(
        [$filterGroupDate]
    );
    $searchResults  = $this->orderRepository->getList($searchCriteria->create());

    $this->logger->info('cron:potato:start');
    /** @var Order $order */
    foreach ($searchResults->getItems() as $order) {
        $this->logger->info('cron:potato:checking:'.$order->getId().":".$order->getIncrementId());
        $this->logger->info('cron:potato:track id:'.$order->getData('call_courier_tracker'));

        // $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/check.log');
        // $loggerr = new \Zend\Log\Logger();
        // $loggerr->addWriter($writer);
        // $loggerr->info('cron:potato:checking:'.$order->getId().":".$order->getIncrementId());

        if($order->getData('call_courier_tracker')){
           $tracking = $this->sendCurlRequest("http://cod.callcourier.com.pk/api/CallCourier/GetTackingHistory?cn=".$order->getData('call_courier_tracker'));
            //  $tracking = $this->sendCurlRequest("http://cod.callcourier.com.pk/api/CallCourier/GetTackingHistory?cn=02856-01-010616300");       //customer Name Maniha Arshad From Islamabad.
            if($tracking){
            $json = json_decode($tracking, true);
            $counts = count($json);
              if($counts && $json[$counts-1]['ProcessDescForPortal'] == 'RETURN SUBMITTED'){
                  $this->orderManagement->cancel($order->getId());
                }
            }
        }
    }
    $this->logger->info('cron:potato:start');

  }

  public function sendCurlRequest($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    return $response ? $response : $error;
   }

}

second question I think my cron is not running then how to check that my cron is properly running or not? I'm working on server not on localhost

  • just check your crontab.xml file is in correct place or not else it should work. – Ronak Chauhan Oct 1 at 10:24
  • it is not even creating the log file to check the error – Asad Khan Oct 1 at 10:28
  • change use \Psr\Log\LoggerInterface to use Psr\Log\LoggerInterface – Amit Bera Oct 1 at 10:30
  • @AmitBera Sir but the Cron is not even added in the cron_schadule table – Asad Khan Oct 1 at 10:38
  • @RonakChauhan Sir my cron is running only once... what is the problem can you please explain it to me?? while in my crontab.xml the <schedule>*/1 * * * *</schedule> – Asad Khan Oct 1 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.