1

I have the following in my cron folder and also I have the following the Controller folder which is exact same code and it get the price when I load in the url but it refuse to get price in cron folder.

I can get the name just fine but no price

enter image description here

<?php
namespace Vendor\Module\Cron;

class GenerateXml
{

    protected $_productCollectionFactory;
    protected $_stockItemRepository;
    protected $_fileSystem;
    protected $_storeManager;
    protected $_imageHelper;
    protected $_stockRegistry;

    public function __construct(
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository,
        \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Helper\Image $imageHelper
    )
    {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_stockItemRepository = $stockItemRepository;
        $this->_fileSystem = $filesystem;
        $this->_storeManager = $storeManager;
        $this->_imageHelper = $imageHelper;
        $this->_stockRegistry = $stockRegistry;
    }

    public function execute()
    {
        $mediaProductUrl = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/product';
        $placeholderImageUrl = $this->_imageHelper->getDefaultPlaceholderUrl('image');
        $content = '';
        $fileName = 'test';
        $productCollection = $this->getProductCollection();
        foreach ($productCollection as $product) {
            if ($product->getTypeId() == 'configurable') {
                $productCstock = $this->getStockItem($product->getId());
                $prices = $product->getPriceInfo()->getPrice('final_price')->getAmount()->getValue();
                $content .= '<item>' . PHP_EOL;
                $content .= '<price>' . $prices . '</price>' . PHP_EOL;
                $content .= '<title>' . $product->getName() . '</title>' . PHP_EOL;
                $content .= '</item>' . PHP_EOL;
                $configChild = $product->getTypeInstance()->getUsedProducts($product);
                foreach ($configChild as $child) {
                    $productStock = $this->getStockItem($product->getId());
                    $content .= '<item>' . PHP_EOL;
                    $content .= '<price>' . $prices . '</price>' . PHP_EOL;
                    $content .= '<title>' . $product->getName() . '</title>' . PHP_EOL;
                    $content .= '</item>' . PHP_EOL;
                }
            }
        }
        $feed = $this->createHeader() . $content . $this->createFooter();
        $media = $this->_fileSystem->getDirectoryWrite(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA);
        $media->writeFile("customFeed/" . $fileName . ".xml", $feed);

        }
        return $this;
    }

    public function getProductCollection()
    {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        return $collection;
    }

    public function getStockItem($productId)
    {
        return $this->_stockRegistry->getStockStatus($productId);
    }

    public function createHeader()
    {
        $header = '<?xml version="1.0"?>' . PHP_EOL;
        $header .= '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' . PHP_EOL;
        $header .= '<xs:element name="item">' . PHP_EOL;
        return $header;
    }

    public function createFooter()
    {
        $footer = '</xs:element>' . PHP_EOL;
        $footer .= '</xs:schema>' . PHP_EOL;
        return $footer;
    }

}

I've also tried different method such as getFinalPrice() but no luck in cron. It only works in controller.

Anyone know what I could be missing in cron get price?

1

Try to run cron functionality with store emulation.

protected $emulation;

public function __construct(
    \Magento\Store\Model\App\Emulation $emulation
)
{
    $this->emulation = $emulation;
}

public function execute()
{
    $_store = 1;
    $this->emulation->startEnvironmentEmulation($_store, \Magento\Framework\App\Area::AREA_FRONTEND, true);

    // your logic here

    $this->emulation->stopEnvironmentEmulation();
}

As cronjob does not have store settled, so you need to run your code by above way.

  • you'r star That works as treat. Just need to correct my answer actually it was just the configurable product that I couldn't get final price and also the product url was returning the non-friendly url now with emulation in place everything is returning correctly. – Juliano Vargas Apr 1 at 9:01
  • Thats great, have a good day !!!! – Yash Shah Apr 1 at 12:50

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.