skip to Main Content

This code is displaying only one product from each category, however I want it to display all products from each category in its own owl-carousel.

How can I fix this?

<?php
$categoryIds = array("3","5","6","12","7");
foreach($categoryIds as $categoryId){

    $carouselcategoryProducts = $block->getCategoryProductsById($categoryId);

foreach ($carouselcategoryProducts as $carouselproduct) {
    /*Get Thumbnail*/
    $carouselimageBlock = $block->getLayout()->createBlock('MagentoCatalogBlockProductListProduct');
    $carouselproductImage = $carouselimageBlock->getImage($carouselproduct, 'category_page_grid');
    ?>

    <div class="owl-carousel owl-theme">
        <h4 class="item">
            <a href="<?php echo $carouselproduct->getProductUrl(); ?>">
                <div class="product_row2">
                    <div class="product_column2">
                        <img class="product_img2" <?php echo $carouselproductImage->toHtml(); ?><i class="far fa-clone compare"></i>
                        <p class="product_title2"><?php echo $carouselproduct->getName(); ?></p>
                        <p class="product_price2">€ <?php echo $carouselproduct->getFinalPrice(); ?>,-</p>
                    </div>
                </div>
            </a>
        </h4>
    </div>
<?php
    }
}
?>

2

Answers


  1. Chosen as BEST ANSWER

    Fixed the issue.

    Correct code:

    <?php
    $categoryIds = array(3,5,6,12,7);
    
    foreach($categoryIds as $categoryId) :
        //echo "<p>category id: </p>$categoryId";
        $carouselcategoryProducts = $block->getCategoryProductsById($categoryId);
        echo "<div class="owl-carousel owl-theme">";
    
    foreach ($carouselcategoryProducts as $carouselproduct) :
        //Get Thumbnail
        $carouselimageBlock = $block->getLayout()->createBlock('MagentoCatalogBlockProductListProduct');
        $carouselproductImage = $carouselimageBlock->getImage($carouselproduct, 'category_page_grid');
        ?>
            <h4 class="item">
                <a href="<?php echo $carouselproduct->getProductUrl(); ?>">
                    <div class="product_row2">
                        <div class="product_column2">
                            <img class="product_img2" <?php echo $carouselproductImage->toHtml(); ?><i class="far fa-clone compare"></i>
                            <p class="product_title2"><?php echo $carouselproduct->getName(); ?></p>
                            <p class="product_price2">€ <?php echo $carouselproduct->getFinalPrice(); ?>,-</p>
                        </div>
                    </div>
                </a>
            </h4>
    
    <?php endforeach; ?>
    </div>
    <?php endforeach; ?>
    

  2. Correct Code:

    $categoryIds = array(3,5,6,12,7);

    foreach ($categoryIds as $cat) 
    { 
    
            $_category = $objectManager->create('MagentoCatalogModelCategory')->load($cat);
            echo "<div class="owl-carousel owl-theme">";
            $categoryProducts = $_category->getProductCollection()
                             ->addAttributeToSelect('*');
    
            foreach ($categoryProducts as $product) {?>
    
             <h4 class="item">
            <a href="<?php echo $product->getProductUrl(); ?>">
                <div class="product_row2">
                    <div class="product_column2">
                        <img class="product_img2" <?php echo $product->toHtml(); ?><i class="far fa-clone compare"></i>
                        <p class="product_title2"><?php echo $product->getName(); ?></p>
                        <p class="product_price2">€ <?php echo $product->getFinalPrice(); ?>,-</p>
                    </div>
                </div>
            </a>
        </h4>
    
        }
        echo "</div>";
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search