Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
3 edited title
| link

Magento 2 Cron job Can'tcan't get finalPrice for configurable product

2 added 110 characters in body
source | link

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?

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

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
source | link

Magento 2 Cron job Can't get finalPrice

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;
    }

}

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