How to get data from core_config_data table in Magento 2?
-
14-04-2021 - |
سؤال
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
المحلول
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();
}
}
نصائح أخرى
<?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
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.