0

I am trying to load a custom phtml on my homepage that would show products from a specific category within an owl carousel. My issue is I am notable to get the product listing to work.

Code here

app/code/Firstimage/ProductSlider/Block/productslider.php

 namespace Firstimage\ProductSlider\Block;
 class ProductSlider extends \Magento\Framework\View\Element\Template
 {    
     protected $_categoryFactory;

     public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        array $data = []
     ) {    
       $this->_categoryFactory = $categoryFactory;
       parent::__construct($context, $data);
   }

   public function getCategory($categoryId) 
   {
       $category = $this->_categoryFactory->create();
       $category->load($categoryId);
       return $category;
       return $category->getData();
   }

   public function getCategoryProducts($categoryId) 
   {
     $products = $this->getCategory($categoryId)->getProductCollection();
     $products->addAttributeToSelect('*');
     return $products;
     return $products->getData();
   }
}

Then my frontend phtml files which is located

app/design/frontend/firstimagedesign/rjtheme/Magento_Theme/templates/html/favoritestyles.phtml

<?php
$categoryId = 81; // this is the category holding your products  
$categoryProducts = $block->getCategoryProducts($categoryId);
foreach ($categoryProducts as $product) 
{
    //print_r($product->getData());
    // printing category name and url
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

My issue seems to be now that in my phtml file the $categoryProducts has a value of NULL and not sure why since category ID 81 has products and is enabled.

13
  • hello you are the magento 1 code Mage but not support magento 2 Mage.you are this line mage $products = Mage::getSingleton('catalog/category')->load($categoryId) check please > prnt.sc/s0pj5b Apr 16, 2020 at 18:07
  • How would I change that?
    – Jayreis
    Apr 16, 2020 at 18:15
  • i think this type :--- $products = $this->_categoryFactory->create()->load($categoryId); Apr 16, 2020 at 18:23
  • I changed my approach using blog.chapagain.com.np/magento-2-get-all-products-of-a-category
    – Jayreis
    Apr 16, 2020 at 18:35
  • products are showing or not in category is 81?? Apr 16, 2020 at 18:46

1 Answer 1

0

I've checked your block class ProductSlider code and found an issue with the injected class \Magento\Backend\Block\Template\Context.

This class is used for the admin block class. You should use Magento\Framework\View\Element\Template\Context for frontend block class to make it work.

I've corrected your ProductSlider class code and add it below.

<?php

namespace Firstimage\ProductSlider\Block;

use Magento\Catalog\Model\CategoryFactory;
use Magento\Framework\View\Element\Template;

class ProductSlider extends Template
{
    /**
     * @var CategoryFactory
     */
    protected $_categoryFactory;

    /**
     * CategoryProducts constructor.
     * @param Template\Context $context
     * @param CategoryFactory $categoryFactory
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        CategoryFactory $categoryFactory,
        array $data = []
    ) {
        $this->_categoryFactory = $categoryFactory;
        parent::__construct($context, $data);
    }

    /**
     * @param $categoryId
     * @return \Magento\Catalog\Model\Category
     */
    public function getCategory($categoryId)
    {
        $category = $this->_categoryFactory->create();
        $category->load($categoryId);
        return $category;
    }

    /**
     * @param $categoryId
     * @return array
     */
    public function getCategoryProducts($categoryId)
    {
        $products = $this->getCategory($categoryId)->getProductCollection();
        $products->addAttributeToSelect('*');
        return $products;
    }
}

Please cross-check the name of the block class file, it should be ProductSlider.php not productslider.php.

Also, add the template file under the module instead in the theme on the below path (optional step).

Firstimage/ProductSlider/view/frontend/templates/favoritestyles.phtml

Call your block class with the below code on the home page.

{{block class="Firstimage\ProductSlider\Block\ProductSlider" template="Firstimage_ProductSlider::favoritestyles.phtml"}}

Hope it helps!!!

0

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.