1

I have created a magento 2 module in which I have created a cron job and set cron for every 5 mins. But in cron_schedule table it is showing success message but cron code is not executed. If I hit that code manually using URL then its working fine. here is the code

<?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="Hemag\SaleCategory\Helper\Data" method="saleCategory" name="hemag_saleCategory_cron">
        <schedule>*/5 * * * *</schedule>
    </job>
</group>

<?php

namespace Hemag\SaleCategory\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Catalog\Api\CategoryLinkManagementInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;

class Data extends AbstractHelper
{


protected $scopeConfig;

protected $collectionFactory;

protected $LinkManagement;

protected $ProductRepository;

protected $_objectManager;


public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    CollectionFactory $collectionFactory,
    CategoryLinkManagementInterface $LinkManagement,
    \Magento\Framework\ObjectManagerInterface $objectmanager
)
{
    parent::__construct($context);
    $this->scopeConfig = $context->getScopeConfig();
    $this->collectionFactory = $collectionFactory;
    $this->LinkManagement = $LinkManagement;
    $this->_objectManager = $objectmanager;
}

public function saleCategory()
{
    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
    $isActive = $this->scopeConfig->getValue('saleproducts/general/enable', $storeScope);

    if($isActive){
        $allStores = array('4'=>'1607','5'=>'1609','87'=>'1610');
        foreach($allStores as $sid => $catid){
            $collection = $this->collectionFactory->create();
            $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
            $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
                               ->addFinalPrice()
                               ->getSelect()
                               ->where('price_index.final_price < price_index.price');
            $collection->addStoreFilter($sid);
            $this->updateData($collection,$sid,$catid);
        }
    } 
}

public function updateData($collection,$sid,$catid)
{ 
    foreach($collection as $product){
        $_product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($sid)->load($product->getEntityId());
        $oldIds = $_product->getCategoryIds();
        $category_ids = array($catid); 
        $newIds = array_merge($oldIds,$category_ids);
        $CategoryLinkRepository = $this->_objectManager->get('\Magento\Catalog\Api\CategoryLinkManagementInterface');
        $CategoryLinkRepository->assignProductToCategories($_product->getSku(), $newIds);
     }
}
}

Please let me know what could be the issue.

Thanks

  • Check your server whether cron is running or not.....If not try php bin/magento cron:run – Ranganathan May 13 '19 at 11:44
  • Yes, I have checked. cron is working fine – Mithi Techie May 13 '19 at 12:26
  • 1
    pls check this saleproducts/general/enable config value in default view....Enabled or not...? – Ranganathan May 13 '19 at 12:30
  • Yes, it is enabled. – Mithi Techie May 13 '19 at 12:58

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.