2

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!

  • this is returning the correct values $item->getProductId()? – Qaisar Satti Dec 5 '16 at 11:19
  • Yes, everything else is returning the correct values – Roy van Os Dec 5 '16 at 11:46
4

Try with the

    $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
  • This did the trick! Can't believe it was this simple. – Roy van Os Dec 5 '16 at 11:53
  • Glad to hear that :) Enjoy Magento @RoyvanOs – Keyur Shah Dec 5 '16 at 11:54
  • 2
    Actually $objectManager->get is same like singleton in Magento1 and $objectManager->create is same like getModel in Magento1 it always creates a new object every time @RoyvanOs – Keyur Shah Dec 5 '16 at 12:00

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.