Filter Products by Category in Magento Catalog Widget (2.4.6-p3)

76 Views Asked by At

I'm trying to display the Magento Catalog Widget at the top of every category page showing only products which belong to it. I have been able to create and place the widget using app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_category_view.xml with the following code:

<block class="Magento\CatalogWidget\Block\Product\ProductsList" name="magento_catalog_widget_arrivals">
  <arguments>
    <argument name="show_pager" xsi:type="number">0</argument>
    <argument name="products_count" xsi:type="number">5</argument>
    <argument name="template" xsi:type="string">Magento_CatalogWidget::product/widget/content/grid.phtml</argument>
    <argument name="conditions_encoded" xsi:type="string">^[`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`:`45`^]^]</argument>
</arguments>
</block>

I'm also able to retrieve the category name and id using app/code/Vendor/Module/Block/CategoryInfo.php with the following code:

<?php

namespace Vendor\Module\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Block\ArgumentInterface;
use Magento\Framework\Registry;

class CategoryInfo extends Template implements ArgumentInterface
{
    /**
     * @var Registry
     */
    protected $registry;

    /**
     * @param Template\Context $context
     * @param Registry $registry
     * @param array $data
     */
    public function __construct(
        Template\Context $context,
        Registry $registry,
        array $data = []
    )
    {
        $this->registry = $registry;
        parent::__construct($context, $data);
    }

    /**
     * Get current category ID
     *
     * @return int|null
     */
    public function getCurrentCategoryId()
    {
        $currentCategory = $this->registry->registry('current_category');
        return $currentCategory ? $currentCategory->getId() : null;
    }

    /**
     * Get current category name
     *
     * @return string|null
     */
    public function getCurrentCategoryName()
    {
        $currentCategory = $this->registry->registry('current_category');
        return $currentCategory ? $currentCategory->getName() : null;
    }
}

?>

Again, in the .xml, using something like this:

<block class="Vendor\Module\Block\CategoryInfo" name="category-products-arrivals-block" template="ThemeName_Theme::category-products-arrivals.phtml"/>

And app/design/frontend/Vendor/theme/ThemeName_Theme/templates/category-products-arrivals.phtml with the following code:

<?php
/** @var \Vendor\Module\Block\CategoryInfo $block */
$categoryId = $block->getCurrentCategoryId();
$categoryName = $block->getCurrentCategoryName();
?>

<h2 class="block-title"><?php echo __('New Arrivals on %1', $categoryName) ?></h2>

I can display name and id in the Category Page. However, and here comes the problem, I have no idea of how to pass the CurrentCategoryId to the encoded conditions of the widget or how to modify the class it controls it so if filters by it. Right now, the only thing I can do is hardcoding the category id into the conditions_encoded parameter in the .xml block that creates the widget. Here:

`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`45`^]

I need to replace that 45 with the current category id, just that.

One would think this is a pretty straightforward task or that, at least, someone would have done it or asked in the forums, but nothing. Have been banging my head against the wall for days and no way.

This is my first question here and I'm really new to Magento. So, if there is any more information needed feel free to ask. In any case, thanks in advance.

I have tried many things. Replacing the class that controls the widget, asking chatGPT... Nothing worked.

0

There are 0 best solutions below