0

Everything was work fine. But now from cron doesn't load productFactory right! Can somebody please help me?

public function __construct(
    Context $context,
    \Magento\Checkout\Model\Session $session,
    \Magento\Quote\Model\Quote $quote,
    \Magento\Quote\Model\QuoteManagement $quoteManagement,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\View\Asset\Repository $assetRepo,
    \Magento\Framework\App\RequestInterface $request,
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Helper\ImageFactory $imageHelperFactory,
    \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
    \Magento\Framework\App\Filesystem\DirectoryList $_directorylist
) {
    $this->session = $session;
    $this->quote = $quote;
    $this->quoteManagement = $quoteManagement;
    $this->storeManager = $storeManager;
    $this->assetRepo = $assetRepo;
    $this->request = $request;
    $this->fileFactory = $fileFactory;
    $this->collectionFactory = $collectionFactory;
    $this->productFactory = $productFactory;
    $this->imageHelperFactory = $imageHelperFactory;
    $this->_stockItemRepository = $stockItemRepository;
    $this->_directorylist=$_directorylist;
    parent::__construct($context);
}


public function exportToCsv($store_id, $store_name)
{
    if(is_null($store_id) || $store_id == '')
    {
        $store_id = 0;
    }
    if(is_null($store_name) || $store_name == '')
    {
        $store_name = 'default';
    }

    $products = $this->productFactory->create()
            ->setStoreId($store_id)
            ->getCollection()
            ->addAttributeToFilter('status', 1)
            ->addAttributeToFilter('visibility', '4');
foreach($products as $product)
    {
if ($product->getTypeId() == 'configurable') {
            $regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getValue();
            $specialPrice = $product->getPriceInfo()->getPrice('special_price')->getValue();
        print_r($regularPrice); // this is null
        } }

Manually work fine!

class Run {
protected $categoryFactory;
protected $_storeManager;
protected $helper;

public function __construct(
    \Magento\Catalog\Model\CategoryFactory $categoryFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
    \Magelan\FacebookAds\Helper\Data $helper
) {
    $this->categoryFactory = $categoryFactory;
    $this->_storeManager = $storeManager;
    $this->fileFactory = $fileFactory;
    $this->helper = $helper;
}

public function execute()
{
    $storeManagerDataList = $this->_storeManager->getStores();
    foreach ($storeManagerDataList as $key => $value) {
            $this->helper->exportToCsv($key, $value['code']);
    }
}

}

2
  • what error you getting?
    – Devidas
    Jun 28 '21 at 13:50
  • Nothing, is just empty(null), I load exportToCsv(0, "default"), but 3days ago was working and nothing change in my code, you have some ideas? Jun 28 '21 at 13:53
1

We can debug the cron issue like below.

Download magerun2 by running the below commands in the Magento root folder.

wget https://files.magerun.net/n98-magerun2.phar
chmod +x ./n98-magerun2.phar

Then run the below command, it will list all cron jobs.

php n98-magerun2.phar sys:cron:list

Then run the needed specific cron by job code.

php n98-magerun2.phar sys:cron:run [cron code]

It will show the detailed error if the cron class having any error. Using these details, we can fix the code issue.

3
  • Thank you, for your help, I discovered that is problem if ($product->getTypeId() == 'configurable') { $regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getValue(); $specialPrice = $product->getPriceInfo()->getPrice('special_price')->getValue(); } so regularPrice is empty, you have some suggestions, how can I fix that? Jun 29 '21 at 13:38
  • So something changes on the server or deploy magento and stops working... Jun 29 '21 at 13:40
  • show me the error screenshot? Jun 29 '21 at 15:53
0

try below code. use product collection factory and check.

protected $_productsCollectionFactory;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productsCollectionFactory
    ) {
        $this->_productsCollectionFactory = $productsCollectionFactory;
    }

 ------------------
    $store_id = 0;
    $collection = $this->_productsCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->->setStoreId($store_id);
1
  • I change it, but the same mistake, other ideas? Jun 28 '21 at 14:28
0

Try changing your code from this

if ($product->getTypeId() == 'configurable'){
    $regularPrice = $product->getPriceInfo()->getPrice('regular_price')->getValue();
    $specialPrice = $product->getPriceInfo()->getPrice('special_price')->getValue();
}

to this

if ($product->getTypeId() == 'configurable') {
      $basePrice = $product->getPriceInfo()->getPrice('regular_price');

      $regularPrice = $basePrice->getMinRegularAmount()->getValue();
      $specialPrice = $product->getFinalPrice();
}

Why because you are using a simple product code for a configurable product that doesn't hold its own price all you can get a minimum price (start from)

For more follow this link

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.