cancel
Showing results for 
Search instead for 
Did you mean: 

Get category_id variable for content block

Get category_id variable for content block

Hello, Magento 2.2.1

 

I am wanting to dynamically show featured items specific to whatever category page the user is viewing.

 

So, I have featured items showing in the left side column using a block. It has this code:

 {{block class="Smartwave\Filterproducts\Block\Home\FeaturedList" name="featured_product" product_count="9" aspect_ratio="1" image_width="150" template="side_list.phtml"}}

I would like to instead do something like this:

 {{block class="Smartwave\Filterproducts\Block\Home\FeaturedList" name="featured_product" product_count="9" aspect_ratio="1" image_width="150"  category_id="<category_id_variable>" template="side_list.phtml"}}

The difference being passing a 'category_id=' variable that is the id of the current category page.

 

Maybe I am not quite understanding the best way to do that.. but open to thoughts!

 

Thanks.

Donovan

5 REPLIES 5

Re: Get category_id variable for content block

Bump... any ideas on this? I'm at a loss.

Re: Get category_id variable for content block

@dbrooke1007
As far i understand you want to get category id dynamically when user viewing category page.
So here you ca do. get current category registry and use that

 

protected $registry;

    public function __construct(  
        \Magento\Framework\Registry $registry,

    )
    {      
        $this->registry = $registry;

    }


    public function getCurrentCategory()
    {      
        $id =  $this->registry->registry('current_category')->geId();
    }

check complete reference

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: Get category_id variable for content block

Thanks.  I have added your code to the themes FeatureList.php file.

class FeaturedList extends \Magento\Catalog\Block\Product\ListProduct {

    protected $_collection;

    protected $categoryRepository;

    protected $_resource;

    // ddb added
    protected $registry;


    public function __construct(
            \Magento\Catalog\Block\Product\Context $context,
            \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
            \Magento\Catalog\Model\Layer\Resolver $layerResolver,
            CategoryRepositoryInterface $categoryRepository,
            \Magento\Framework\Url\Helper\Data $urlHelper,
            \Magento\Catalog\Model\ResourceModel\Product\Collection $collection,
            \Magento\Framework\App\ResourceConnection $resource,
            \Magento\Framework\Registry $registry,//ddb added
            array $data = []
    ) {
        $this->categoryRepository = $categoryRepository;
        $this->_collection = $collection;
        $this->_resource = $resource;
        $this->registry = $registry; // ddb added

        parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $registry, $data);
    }

    // ddb added
    public function getCurrentCategory()
    {
        $id =  $this->registry->registry('current_category')->geId();
        return $id;
    }

I then call it from another function:

$immediateCatID =  getCurrentCategory();

I am getting this error:

PHP Fatal error:  Uncaught TypeError: Argument 8 passed to Smartwave\Filterproducts\Block\Home\FeaturedList::__construct() must be an instance of Magento\Framework\Registry, array given, called in /home/jpadmin/public_html/store/generated/code/Smartwave/Filterproducts/Block/Home/FeaturedList/Interceptor.php on line 14 and defined in /home/jpadmin/public_html/store/app/code/Smartwave/Filterproducts/Block/Home/FeaturedList.php:21

 

Re: Get category_id variable for content block

@dbrooke1007    remove the var/cache var/generation folder will solved this issue.

Find helpful ? Consider Giving Kudos to this post.
Problem solved? Click Accept as Solution!"
Qaisar Satti

Re: Get category_id variable for content block

Hi, though the solution was a bit different than yours (maybe because of the theme), this got me there. Thanks.