Excuse my poor presentation of a question I am trying to list products of the same category on any product page of the named category.

Here is the code I have, which works perfectly if done for each category of product. But I wish to have a category id fetch code to insert in this here, so I can load it to all my products pages.

Here is the code I have with category id "842"

<div class="cdz-product-wrap base-slider product-list-style-30  bottom-hovered-style mb-stage-padding-5 text-center">
<div class="cdz-block-title">
<p class="b-title h2"><span class="inner">ALL COLORS IN THIS CATEGORY:</span></p>
<p class="title-desc">Recommended for You</p>
<div class="cdz-block-content">
<div class="cdz-product">{{widget type="Codazon\ProductFilter\Block\Product\ProductsList" display_type="all_products" products_count="22" order_by="entity_id DESC" show="thumb,name,review,price,addtocart,wishlist,compare,quickshop,label" thumb_width="250" thumb_height="250" filter_template="custom" custom_template="grid-style-30.phtml" conditions_encoded="^['1':^['type':'Magento||CatalogWidget||Model||Rule||Condition||Combine','aggregator':'all','value':'1','new_child':''^],'1--1':^['type':'Magento||CatalogWidget||Model||Rule||Condition||Product','attribute':'category_ids','operator':'==','value':'842'^]^]" total_rows="1" total_cols="4" ajax_load="0" show_slider="1" slider_dots="0" slider_nav="1" slider_autoplay="0" slider_autoplay_timeout="5000" slider_margin="20" items_1900="4" items_1600="4" items_1420="4" items_1280="4" items_980="4" items_768="3" items_480="3" items_320="2" items_0="1"}}</div>

Is there any way to add a code in here to fetch each product's category id so I do not have to implement it manually to each group

Thanks for your kind attention

1 Answer 1

namespace Vendor\AllCategory\Model;

use Exception;
use Magento\Catalog\Api\CategoryListInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Catalog\Api\Data\CategorySearchResultsInterface;

class AllCategory
     * @var SearchCriteriaBuilder
    private $searchCriteriaBuilder;

     * @var CategoryListInterface
    private $categoryList;

    public function __construct(
        CategoryListInterface $categoryList,
        SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->categoryList = $categoryList;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;

     * Fetch all Category list
     * @return CategorySearchResultsInterface
    public function getAllSystemCategory()
        $categoryList = [];
        try {
            $searchCriteria = $this->searchCriteriaBuilder->create();
            $categoryList = $this->categoryList->getList($searchCriteria);
        } catch (Exception $exception) {
            throw new Exception($exception->getMessage());

        return $categoryList;

Fetch Category by iterate over a loop from the result given by the method and first check total Count of the category condition.

$categoryList = $this->getAllSystemCategory();
if ($categoryList->getTotalCount()) {
    foreach ($categoryList->getItems() as $category){

After that add the phtml code and call the foreach there.

  • Thank you very much for your answer Mr. Shahbaz. I am not the smartest person, and trying to get this done with little knowledge of locations and files that need to be changed and updated for this to work. Would you be kind enough to state a step by step of what to do with the codes you provided? Much apology and many thanks in advance Reza
    – RJavan
    Commented Mar 28, 2022 at 17:58
  • The above html code is written in .phtml file right..? or any other Commented Mar 29, 2022 at 6:13

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.