0

I have a cron job in magento 2, since I have added dependencies it has stopped running. I am getting the error

Fatal error: Uncaught Error: Cannot access parent:: when current class scope has no parent

and this is when I add this to the constructor :

parent::__construct($context,$data);

If I remove this line then the cron fails because it can't see the dependencies. This is my cron, I want to log the customer ids for that store:

use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Framework\Registry;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\ProductRepository;
use Magento\Customer\Api\CustomerRepositoryInterface;

class reinitpricing {

  private $resultJsonFactory;
    protected $resourceConnection;
    protected $authSession;
    private $productCollectionFactory;
    protected $helperData;
    protected $coreRegistry;
    private $currentStore;
    private $storeManager;
    private $storeId;
    protected $productRepository;
    protected $_productAttributeRepository;
    private $storeResolver;
    protected $_customer;
    protected $_customerFactory;

    public function __construct(\Magento\Framework\App\Action\Context $context, 
    JsonFactory $resultJsonFactory, 
    \Magento\Framework\App\ResourceConnection $resourceConnection, 
    \Magento\Backend\Model\Auth\Session $authSession, ProductCollectionFactory $productCollectionFactory,
     \FlavourWarehouse\Pricing\Helper\Data $helperData, 
     Registry $coreRegistry,
      \Magento\Catalog\Model\Product\Attribute\Repository $productAttributeRepository,
       ProductRepository $productRepository, 
       \Magento\Store\Model\StoreResolver $storeResolver, 
       \Magento\Framework\App\RequestInterface $request, 
       CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        array $data = [])
    {
        parent::__construct($context,$data);
        $this->resultJsonFactory = $resultJsonFactory;
        $this->resourceConnection = $resourceConnection;
        $this->authSession = $authSession;
        $this->productCollectionFactory = $productCollectionFactory;
        $this->helperData = $helperData;
        $this->storeManager = $storeManager;
        $this->currentStore = 1;
        $this->storeId = 1;
        $this->coreRegistry = $coreRegistry;
        $this->productRepository = $productRepository;
        $this->_productAttributeRepository = $productAttributeRepository;
        $this->storeResolver = $storeResolver;
        $this->_request = $request;
        $this->customerRepository = $customerRepository;
        $this->_customer = $customer;
        $this->_customerFactory = $customerFactory;
        

    }

  public function execute() {


    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logger.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("the cron job is running");

        $collection = $this->_customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addStoreFilter(1)
            ->load();

            foreach ($collection AS $customer) {
                $logger->info($customer->getId());
            }
}
0

This is because given class reinitpricing { have not extends SomeClassName because constructor tries to construct with $context and $data something from parent class.


Alternatively, based on execute method code I can suggest following.

File: Vendor/Module/Cron/Reinitpricing.php

namespace Vendor\Module\Cron;
class Reinitpricing {

    protected $_customerFactory;

    public function __construct( 
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory,
    )
    {  
        $this->_customerFactory = $customerFactory;
    }

    public function execute() {

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/logger-reinitpricing.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("the cron job is running");

        $collection = $this->_customerFactory->create()->getCollection()
            ->addAttributeToSelect("*")
            ->addStoreFilter(1)
            ->load();

            foreach ($collection AS $customer) {
                $logger->info($customer->getId());
    }
}

File: Vendor/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="vendor_module_price_reinit" instance="Vendor\Module\Cron\Reinitpricing" method="execute">
            <schedule>0 */6 * * *</schedule>
        </job>
    </group>
</config>
3
  • I had already tried this first and it was failing as if it didnt know what customerFactory was – Gaz Smith Jul 23 at 13:47
  • Check now I have updated answer – sandip Jul 23 at 15:49
  • Nothings changed? – Gaz Smith Jul 26 at 8:11

Your Answer

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

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