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.