0

Here I am trying to get custom attribute value from bestseller products collections magento 2??

<?php
namespace Vendor\Module\Block;
class BestSeller extends \Magento\Framework\View\Element\Template
{ 
   protected $_collectionFactory;
   protected $_storeManager;   
   protected $_productloader;  
   protected $categoryFactory; 
   public function __construct(
       \Magento\Backend\Block\Template\Context $context,
       \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
       \Magento\Store\Model\StoreManagerInterface $storeManager,
       \Magento\Catalog\Model\ProductFactory $_productloader,
       \Magento\Catalog\Model\CategoryFactory $categoryFactory,
       array $data = []
   ) { 
       $this->_collectionFactory = $collectionFactory;
       $this->_storeManager = $storeManager;
       $this->_productloader = $_productloader;
       $this->categoryFactory = $categoryFactory;
       parent::__construct($context, $data);
   }

    public function getBestSellerData(){

        $bestSellerProdcutCollection = $this->_collectionFactory->create()
                    ->setModel('Magento\Catalog\Model\Product')
                    ->setPeriod('month') //you can add period daily,yearly
                    ->setPageSize(8);            

        return $bestSellerProdcutCollection;

    }


}

In phtml file i am trying like this

$bestCollection = $block->getBestSellerData();
foreach($bestCollection as $bproduct){
$ProductType = $bproduct->getData('type_product');
}

1 Answer 1

1

Block file

namespace Vendor\Module\Block;
class BestSeller extends \Magento\Framework\View\Element\Template
{ 
   protected $_collectionFactory;
   protected $_storeManager;   
   protected $_productloader;  
   protected $categoryFactory; 
   public function __construct(
       \Magento\Backend\Block\Template\Context $context,
       \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
       \Magento\Store\Model\StoreManagerInterface $storeManager,
       \Magento\Catalog\Model\ProductFactory $_productloader,
       \Magento\Catalog\Model\CategoryFactory $categoryFactory,
       array $data = []
   ) { 
       $this->_collectionFactory = $collectionFactory;
       $this->_storeManager = $storeManager;
       $this->_productloader = $_productloader;
       $this->categoryFactory = $categoryFactory;
       parent::__construct($context, $data);
   }

    public function getBestSellerData(){

        $bestSellerProdcutCollection = $this->_collectionFactory->create()
                    ->setModel('Magento\Catalog\Model\Product')
                    ->setPeriod('month') //you can add period daily,yearly
                    ->setPageSize(8);            

        return $bestSellerProdcutCollection;

    }
    public function getLoadProduct($id)
    {
        return $this->_productloader->create()->load($id);
    }

}

phtml file

$bestCollection = $block->getBestSellerData();
foreach($bestCollection as $bproduct){
$p = $block->getLoadProduct($bproduct->getId());
$ProductType = $p->getData('type_product');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.