0

I want to create cms page and will show products list as per given ids in admin editor.

3
  • have u tried to use wiget at cms page
    – Amit Bera
    Sep 6 '18 at 10:20
  • Please specify what you have do so far?
    – Amit Bera
    Sep 6 '18 at 10:21
  • I want only to show products by ids like product_ids="1,5,8,52" Sep 6 '18 at 10:22
1

If you want to some specific product to a cms page.

Then go to admin>Content>CMS page,Select your cms page,then open that cms page.

Then go to the Content tab, click open widget icon.

enter image description here

Insert Widget select Widget type Catalog Product Listing, after that at Conditions select option SKU and set your SKUs at here.

Then click on insert widget button for insert your widget and then click on Save Page for save your page,then cache flush and check your page.

You donot need any code.

enter image description here

2
  • Thanks @Amit Bera, yours answer 100% right but I want to show by ids only. Sep 6 '18 at 10:54
  • Why you don't want to use SKU?. ID AND SKU BOTH are like a unique key for a product.
    – Amit Bera
    Sep 6 '18 at 11:02
0
  1. Put the code bellow in your cms page

    {{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::html/products-list.phtml"}} 
    
  2. Put your product collection in :

    app/design/frontend/{Vendor}/{theme}/Magento_Theme/templates/html/products-list.phtml
    

Update:

For testing, you can put the code bellow with the objectManager but it is not the good way, ones again just for testing:

<?php
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$collection = $productCollection->addAttributeToSelect('*')
                                ->setPageSize(5) // load just 5 products
                                ->load();
foreach ($collection as $product)
\Zend_Debug::dump($product->getData()); // echo $product->getId(); for Ids
?>

The best way to get them is to use a a block, create some function in that block then you retrieve that function in phtml like this : $block->getMyCustomProducts()

Content block:

public function __construct(
    \Magento\Backend\Block\Template\Context $context,        
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,        
    array $data = []
)
{    
    $this->_productCollectionFactory = $productCollectionFactory;    
    parent::__construct($context, $data);
}

 public function getMyCustomProducts()
{
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->setPageSize(5); // fetching only 5 products
    return $collection;
}

Note: don't forget to update the block class and template path (since you use a block, you should put that files in your custom module) so:

{{block class="{Vendor}\{Module}\Block\{Blockname}" template="{Module}_{Name}::html/products-list.phtml"}} 

app/code/{Vendor}/{Module}/view/frontend/templates/html/products-list.phtml
6
  • Can you share me code - how to get products list by ids in products-list.phtml Sep 6 '18 at 10:17
  • @Pukhraj Suthar Look my update please
    – Aamir
    Sep 6 '18 at 10:32
  • @PЯINCƏ. Can u update the code of template file pls, Jul 15 '19 at 6:46
  • @jafarpinjar what do you mean by update the template file and why ?
    – Aamir
    Jul 15 '19 at 17:27
  • @PЯINCƏ, you have the collection and how u display it in template file? i need tthat code Jul 16 '19 at 6:04

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.