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