1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php /* * Company Name: Cozmot Inc * Website: https://cozmot.com * Writer Name: Zaheer Ahmed * Contact: zaheercena@gmail.com * Number: +92-301-1000201 */ $_category = $block->getCategory(); //or any ID like 2 or 3 if you already have ?> <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categorysFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); ?> <?php foreach($_category as $category): ?> <?php $cat = $category->getID(); ?> <div class="category-name-<?= $cat;?>"> <a href="<?php echo $category->getUrl(); ?>"> <span><?php if($block->getData('title')){echo $block->getData('title');}else{echo $category->getName();} ?></span> </a> </div> <div class="category-more-<?= $cat;?>"> <a href="<?php echo $category->getUrl(); ?>"> <span>See More<?php echo $category->getID(); ?></span> </a> </div> <?php $categoryId = $cat; $cate = $categorysFactory->create()->load($categoryId); if($block->getData('filter-selection') == '0') { $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('created_at','desc');; } elseif($block->getData('filter-selection') == '1') { $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('special_price', ['neq' => '']); } elseif($block->getData('filter-selection') == '2') { $categoryProducts = $cate->getProductCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_featured', '1'); } $block->getProductCollectionFromCategory($cat);?> <div id="homepage-wiget-slider" class="owl-side-arrow" style="margin: 0 -10px"> <div class="owl-carouel owl-thme" style="display: flex;"> <?php $counter = 5; foreach ($categoryProducts as $product) { if($counter > 0){ $counter--; $imagewidth=400; $imageheight=400; $imageHelper = $objectManager->get('\Magento\Catalog\Helper\Image'); $image_url = $imageHelper->init($product, 'product_page_image_small')->setImageFile($product->getFile())->resize($imagewidth, $imageheight)->getUrl(); ?> <div class="iem homeage-widget-slider-content-wrapper"> <a href="<?= $product->getProductUrl();?>"> <img src="<?php echo $image_url; ?>" alt="" /> <h4 class="price"><?php echo $product->getFormattedPrice();?></h4> <h3 class="title"><?php echo $product->getName();?></h3></a> </div> <?php }else{ break; } } ?> </div> </div> <?php endforeach; ?> |
Another solution if you only wanna fetch Products
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $categoryFactory = $objectManager->get('\Magento\Catalog\Model\CategoryFactory'); $categoryId = 4; // could be any of yours $category = $categoryFactory->create()->load($categoryId); $categoryProducts = $category->getProductCollection() ->addAttributeToSelect('*'); foreach ($categoryProducts as $product) { // geting Product object print_r($product->getData()); echo $product->getName(); echo $product->getProductUrl(); } |
Object Manager Usage Good and Bad:
]]>