I'm trying to get product collection by Multiple Category Ids with Multiple SKUs. The result has been coming wrong. I can't understand where I'm wrong. The code is given below
<?php
namespace CustomVendor\CustomModule\Block\Specific;
class Collection extends \Magento\Framework\View\Element\Template{
protected $_productCollectionFactory;
protected $helperFactory;
protected $listProductBlock;
private $request;
protected $_categoryFactory;
protected $_attributeFactory;
protected $_productListHelper;
/**
* @var \Magento\Framework\Registry
*/
protected $_coreRegistry;
/**
* @param \Magento\Framework\Registry
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Block\Product\ListProduct $listProductBlock,
\Magento\Catalog\Helper\ImageFactory $helperFactory,
\Magento\Framework\App\Request\Http $request,
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
\Magento\Catalog\Model\ResourceModel\Eav\Attribute $attributeFactory,
\Magento\Framework\Registry $registry,
array $data = []
)
{
$this->_attributeFactory = $attributeFactory;
$this->_categoryFactory = $categoryFactory;
$this->categoryCollectionFactory = $categoryCollectionFactory;
$this->_productCollectionFactory = $productCollectionFactory;
$this->listProductBlock = $listProductBlock;
$this->helperFactory = $helperFactory;
$this->request =$request;
$this->_coreRegistry = $registry;
parent::__construct($context, $data);
}
public function getProductCollection()
{
$price=$this->getRequest()->getParam('price');
$cat=$this->getRequest()->getParam('cat');
$sortby=$this->getRequest()->getParam('product_list_order');
if(empty($sortby)){
$sortby='position';
}
$listdesc=$this->getRequest()->getParam('product_list_dir');
$page=($this->getRequest()->getParam('p'))? $this->getRequest()->getParam('p') : 1;
//get values of current limit
//$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 3;
$pageSize=($this->getRequest()->getParam('limit'))? $this->getRequest()->getParam('limit') : 12;
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addCategoriesFilter(['in' => array(3,4)]);
$collection->addAttributeToFilter('sku', ['in' => array('1709529','880998', '555555','111111','222222')]);
$collection->setOrder('title','ASC');
$collection->setPageSize($pageSize);
$collection->setCurPage($page);
$collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
$collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
//echo "<pre>"; print_r($collection->getData()); exit();
return $collection;
}
public function getImage($product)
{
// for image url get
return $this->helperFactory->create()->init($product, 'product_thumbnail_image')->resize(150)->getUrl();
}
protected function _prepareLayout()
{
parent::_prepareLayout();
//$this->pageConfig->getTitle()->set(__('products'));
//$post = $this->_coreRegistry->registry('current_post');
//echo "collection Product"; echo "<pre>"; print_r($post->getData());
if ($this->getProductCollection()) {
$toolbar = $this->getLayout()
->createBlock(
'Magento\Catalog\Block\Product\ProductList\Toolbar',
'product_list_toolbar'
)
->setTemplate('CustomVendor_CustomModule::product/list/toolbar.phtml')
->setCollection($this->getProductCollection());
// $pager = $this->getLayout()->createBlock(
// 'Magento\Theme\Block\Html\Pager',
// 'CustomModule.product.pager'
// )->setAvailableLimit(array(3=>3,6=>6,9=>9))->setShowPerPage(true)->setCollection(
// $this->getProductCollection()
// );
$pager = $this->getLayout()->createBlock(
'Magento\Theme\Block\Html\Pager',
'CustomModule.product.pager'
)->setAvailableLimit(array(12=>12,24=>24,36=>36))->setShowPerPage(true)->setCollection(
$this->getProductCollection()
);
$this->setChild('pager', $pager);
$this->setChild('toolbar', $toolbar);
$this->getProductCollection()->load();
}
return $this;
}
public function getPagerHtml()
{
return $this->getChildHtml('pager');
}
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
public function getMode()
{
return $this->getChildBlock('toolbar')->getCurrentMode();
}
public function getProductImageUrl($product)
{
// return $this->_image->init($product, 'category_page_grid')->constrainOnly(FALSE)
// ->keepAspectRatio(TRUE)
// ->keepFrame(FALSE)
// ->getUrl();
return $this->helperFactory->create()->init($product, 'category_page_grid')->constrainOnly(FALSE)
->keepAspectRatio(TRUE)
->keepFrame(FALSE)
->getUrl();
}
public function getAddToCartPostParams($product)
{
return $this->listProductBlock->getAddToCartPostParams($product);
}
}
I have tried to show the proper result of Product collection by googling a lot of times. But there is no proper solution that I haven't got in google. It will be helpful and grateful if this type of task has been figured out. Thank You.
e
.sku
IN('880981', '880998', '1692971', '1709529', '1718933', '1733375', '878771', '878802', '889607', '555555', '111111', '222222')) ... like that. I can't understand how to all products from cat 3,4,5,...with multiple SKUs. Thank U for quick reply.