0

I have created a cron and in that i want to get some values from core_config_data table in my Cron File.

My Cron file is :

<?php

 namespace Mageplaza\HelloWorld\Cron;

 use Psr\Log\LoggerInterface;
 use Magento\Catalog\Model\Product\Attribute\Source\Status;
 use Magento\Catalog\Model\ResourceModel\Product\Action;

 class Customcron
 {  
private $productAction;
protected $_logger;
protected $_productCollectionFactory;
private $_objectManager;

public function __construct(
    Action $productAction,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\ObjectManagerInterface $objectmanager
) {
    $this->productAction = $productAction;
    $this->_logger = $logger;
    $this->_objectManager = $objectmanager;
}

public function execute(){

    $config = $this->_objectManager->get('Magento\Framework\App\Config\ScopeConfigInterface')->getValue('section/group/field');
    $values = (array)json_decode($config, true);

    $attributes = [
        'status' => 2,
        'price' => '10.70'
    ];
    $productIds = ['1'];
    $storeId = 0;
    $this->productAction->updateAttributes($productIds, $attributes, $storeId);
}
}

My controller index file is :

<?php
namespace Mageplaza\HelloWorld\Controller\Index;

  class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
private $scopeConfig;
protected $_productCollectionFactory;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\View\Result\PageFactory $pageFactory,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    array $data = []
){
    $this->_pageFactory = $pageFactory;
    $this->scopeConfig = $scopeConfig;
    return parent::__construct($context, $data);
}

public function execute()
{   
    $product_ids = [];
    $configValue = $this->scopeConfig->getValue('am_shipping/prices/add_item');
    $productCollection = $this->_productCollectionFactory->create();
    $productCollection->setFlag('has_stock_status_filter', false);
    foreach ($productCollection as $key => $value) {
        $product_ids[] = $value->getId();
    }
    print_r($product_ids);
    print_r($configValue);die('aaaa');
    //return $this->_pageFactory->create();
}
}

It shows me the error Type Error occurred when creating object: Mageplaza\HelloWorld\Controller\Index\Index\Interceptor

3
  • what your question? Question has it answer. Please ask a clear question
    – Amit Bera
    Commented Aug 20, 2020 at 16:38
  • I update answer please check. Commented Aug 21, 2020 at 7:15
  • I update new answer again, please check. Commented Aug 21, 2020 at 8:54

3 Answers 3

1

You need to do code like this :

<?php
namespace Mageplaza\HelloWorld\Controller\Index;

class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
private $scopeConfig;
protected $_productCollectionFactory;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\View\Result\PageFactory $pageFactory,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    array $data = []
){
    $this->_pageFactory = $pageFactory;
    $this->scopeConfig = $scopeConfig;
    $this->_productCollectionFactory = $productCollectionFactory;
    return parent::__construct($context);
}

public function execute()
{   
    $product_ids = [];
    $configValue = $this->scopeConfig->getValue('am_shipping/prices/add_item');
    $productCollection = $this->_productCollectionFactory->create();
    $productCollection->setFlag('has_stock_status_filter', false);
    foreach ($productCollection as $key => $value) {
        $product_ids[] = $value->getId();
    }
    print_r($product_ids);
    print_r($configValue);die('aaaa');
    //return $this->_pageFactory->create();
}
}
7
  • I have updated my controller index file which gives me this error Type Error occurred when creating object: Mageplaza\HelloWorld\Controller\Index\Index\Interceptor will you please help @Dhiren
    – Prits
    Commented Aug 21, 2020 at 6:38
  • I update answer please check. Commented Aug 21, 2020 at 7:15
  • Updated your file on di:compile command it shows Errors during compilation: Mageplaza\HelloWorld\Controller\Index\Index Extra parameters passed to parent construct: $data. File: /var/www/html/reg-dealers/app/code/Mageplaza/HelloWorld/Controller/Index/Index.php Total Errors Count: 1 this error @Dhiren
    – Prits
    Commented Aug 21, 2020 at 7:25
  • I update code, please check that one. Commented Aug 21, 2020 at 8:06
  • Thanks Yes it works with removing the data parameter thanks for your response. but there is an issue rearding i have applied a condition to show only disabled product ids with this $productCollection->setFlag('has_stock_status_filter', false); but it shows all product's id's will you please help.
    – Prits
    Commented Aug 21, 2020 at 9:04
1
<?php

namespace VendorMagento\ModuleName\Cron;

Class Example
{
    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
         $this->scopeConfig = $scopeConfig;
    }

    public function execute()
    {
        $configValue = $this->scopeConfig->getValue('config/to/path');
    }
}

Reference file: vendor/magento/module-catalog/Cron/DeleteOutdatedPriceValues.php

It shows me the error Type Error occurred when creating object: Mageplaza\HelloWorld\Controller\Index\Index\Interceptor

To resolve above error please run the following command:

php bin/magento setup:di:compile
php bin/magento cache:clean
6
  • I have updated my controller index file which gives me this error Type Error occurred when creating object: Mageplaza\HelloWorld\Controller\Index\Index\Interceptor will you please help @Pratik
    – Prits
    Commented Aug 21, 2020 at 6:38
  • @Prits please cheki Update ans Commented Aug 21, 2020 at 7:07
  • on di:compile it shows Errors during compilation: Mageplaza\HelloWorld\Controller\Index\Index Extra parameters passed to parent construct: $data. File: /var/www/html/reg-dealers/app/code/Mageplaza/HelloWorld/Controller/Index/Index.php Total Errors Count: 1 this error.
    – Prits
    Commented Aug 21, 2020 at 7:26
  • 1
    Please remove $data from this code: return parent::__construct($context, $data); and try again.
    – Pratik Oza
    Commented Aug 21, 2020 at 7:28
  • Thanks it works i have applied a condition to show only disabled product ids with this $productCollection->setFlag('has_stock_status_filter', false); but it shows all product will you please help @PratikOza
    – Prits
    Commented Aug 21, 2020 at 7:42
0

You just need to inject this below class in your construct :

protected $scopeInterface;

public function __construct(
     ...
     \Magento\Framework\App\Config\ScopeConfigInterface $scopeInterface,
     ...
)
{
    ...
    $this->scopeInterface = $scopeInterface;
    ...
}

And then, use this below line to get value from core config table :

$configValue = $this->scopeInterface->getValue('section/group/field',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);

Now, Remove generated folder and clean cache.

Reference

1

Your Answer

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

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