0

I have a cms page that I am using to display products via CatalogWidget template. I would like to modify the template to have the title of the category link to the category page, but I have been unsuccessful so far. Hopefully someone can point me in the right direction for this to work.

My CMS page has multiple product list widgets on the page, using the following widget code (with different category ids assigned).

    {{widget type="Magento\CatalogWidget\Block\Product\ProductsList" title="" show_pager="0" products_count="10" template="Magento_CatalogWidget::product/widget/content/chemlist.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`:`1923`^]^]"}}

The chemlist.phtml file (referenced above) contains the following code at the top of the file ( I have omitted the foreach code here, since that code is working and I only need help getting the parent category url of the list).

    <?php

use Magento\Framework\App\Action\Action;

/** @var \Magento\CatalogWidget\Block\Product\ProductsList $block */
?>
<?php if ($exist = ($block->getProductCollection() && $block->getProductCollection()->getSize())) : ?>



    <?php
    $type = 'widget-product-grid';
    $mode = 'list';
    $items = $block->getProductCollection()->getItems();
    $showCart = true;
    $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW;
    $description = false;
    ?>


<li class="list-group-item">
<hr style="background-color: #333; height: 2px; border: 0;" />
        <div class="row 2col chemList">
            
<div class="col-md-4">
    <h4 class="" style="color:#333;">
        <?php if ($block->getTitle()) : ?>
            <div class="block-title">
                <!-- THE FOLLOWING HREF IS WHERE I WOULD LIKE TO LINK TO THE PARENT CATEGORY OF THE LIST -->
                <a href="<?= $block->getCategoryUrl() ?>"><strong><?= $block->escapeHtml(__($block->getTitle())) ?></strong></a>
            </div>
        <?php endif ?>
    </h4>
</div>

Thanks for any suggestions on how to make this work. Let me know if anything is unclear, or if more information is needed.

1 Answer 1

0

Inject the necessary dependencies in your template file. You will need the Magento\Catalog\Model\CategoryFactory and Magento\Catalog\Helper\Category classes.

 <?php
namespace Vendor\Module\Block\Widget;

use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Helper\Category;

class MyWidget extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
    protected $_template = 'widget/mywidget.phtml';

    protected $_categoryFactory;
    protected $_categoryHelper;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        CategoryFactory $categoryFactory,
        Category $categoryHelper,
        array $data = []
    ) {
        $this->_categoryFactory = $categoryFactory;
        $this->_categoryHelper = $categoryHelper;
        parent::__construct($context, $data);
    }
}

Get the current category ID in your template file. This can be done using the getCurrentCategory() method from the category helper class.

$currentCategoryId = $this->_categoryHelper->getCurrentCategory()->getId();

Get the parent category ID using the getParentId() method from the category factory class.

$parentCategoryId = $this->_categoryFactory->create()->load($currentCategoryId)->getParentId();

Get the parent category URL using the getCategoryUrl() method from the category helper class.

$parentCategoryUrl = $this->_categoryHelper->getCategoryUrl($parentCategoryId);

You can now use the $parentCategoryUrl variable to display the parent category URL in your template file.

1
  • Thank you for your suggestion @brentwpeterson. I've tried adding the above code to my chemlist.phtml file, but I get the following error: Error filtering template: Notice: Undefined property: Magento\CatalogWidget\Block\Product\ProductsList\Interceptor::$_categoryHelper in /var/www/html/vendor/magento/framework/View/TemplateEngine/Php.php on line 122 I must be doing something wrong, but I am not sure what. Do I need to create a custom module to acheive what I am looking to do?
    – Rich D
    May 11, 2023 at 15:56

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.