Question

I'm building a widget which displays a given number of products based on the Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory collection factory and I try to get the correct prices for every product inside the factory but somehow this results in all products showing the same price as the first product. All products have catalog rules applied to them and every product is a simple product.

Everything except the product price related values are returning correct values.

I'm using the following code:

namespace ActiveAnts\Bestsellers\Block\Widget;

use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template;
use Magento\Reports\Model\ResourceModel\Report\Collection\Factory;
use Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory;
use Magento\Widget\Block\BlockInterface;

class Bestsellers extends Template implements BlockInterface
{
protected $_template = 'widget/bestsellers.phtml';

protected $_productsCount;
protected $httpContext;
protected $_resourceFactory;
protected $_productCollectionFactory;
protected $_imageHelper;
protected $_cartHelper;
private $_productRepository;
private $_collectionFactory;

/**
 * Bestsellers constructor.
 * @param Context $context
 * @param Factory $resourceFactory
 * @param CollectionFactory $collectionFactory
 * @param ProductRepository $productRepository
 * @param array $data
 */
public function __construct(
    Context $context,
    Factory $resourceFactory,
    CollectionFactory $collectionFactory,
    ProductRepository $productRepository,
    array $data = []
) {
    $this->_resourceFactory = $resourceFactory;
    $this->_imageHelper = $context->getImageHelper();
    $this->_cartHelper = $context->getCartHelper();
    $this->_collectionFactory = $collectionFactory;
    $this->_productRepository = $productRepository;
    parent::__construct($context, $data);
}

/**
 * get featured product collection
 */
public function getBestsellerProduct()
{
    $objectManager = ObjectManager::getInstance();

    $collection = $this->_collectionFactory->create()->setModel(
        'Magento\Catalog\Model\Product'
    );

    $collection->setPageSize(15);

    foreach ($collection as $item) {
        $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId());

        $_productRepo = $this->_productRepository->getById($item->getProductId());

        $bestsellers[] = [
            'id' => $item->getProductId(),
            'store id' => $item->getStoreId(),
            'product_url' => $_productRepo->getUrlModel()->getUrl($_productRepo),
            'addtocart_url' => $this->_cartHelper->getAddUrl($_product),
//                'price' => $objectManager->get('Magento\Catalog\Model\Product')->load($item->getProductId())->getPriceInfo()->getPrice('final_price')->getAmount(),
            'price' => $_product->getPriceInfo()->getPrice('final_price')->getAmount(),
            'price_html' => $this->getLayout()->getBlock('product.price.render.default')->render(
                'final_price',
                $_product
            )
        ];
    }
    return $collection;
}
}

I've been trying to fix this for 3 days now without any luck, so any answers that give me a push in the right direction would be awesome, thanks!

Was it helpful?

Solution

Try with the

    $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top