3

I want to filter the product collection on a category page by the user_id of the current user. I have created an attribute 'user_id' which is linked to the product when it is created with the current id of the user(here products are created by the user). I have created a category 'User Products' also when directed to userproducts.html all the products are shown, I want to filter it by user_id of the current user.

Edit 1. I tried overriding catalog_category_view in my module as suggested by @MohitRane but it is not loading from my module. This is the screenshot enter image description here

1
  • Update your code such that it will be easy to debug..
    – aravind
    Commented Jul 11, 2019 at 14:01

1 Answer 1

0

You can do this by following code,

<?php

namespace Vendor\Module\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    protected $_productCollectionFactory;
    protected $_session;
    protected $_customerFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\Session $session,
        array $data = []
    ) {
        $this->_productCollectionFactory = $productCollectionFactory;
        $this->_customerFactory = $customerFactory;
        $this->_session = $session;
        parent::__construct($context, $data);
    }

    public function getCustomerId()
    {
        if ($this->_session->isLoggedIn()) {
            return $this->_session->getCustomer()->getId();
        }
    }
    public function getProductCollection()
    {
        $userId = $this->getCustomerId();
        if (!empty($userId)) {
            /*$customerCollection = $this->_customerFactory->create()
                                        ->getCollection()
                                        ->addAttributeToSelect("*")
                                        ->load(); */

            $productCollection = $this->_productCollectionFactory->create();
            $productCollection->addAttributeToSelect('*')
                                ->addAttributeToFilter(
                                    'user_id',
                                    array('eq' => $userId)
                                ); // user_id is your attribute name
            return $productCollection;
        }
    }
}

Add view/frontend/layout/catalog_category_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="category.products.list">
            <block class="Namespace\Modulename\Block\Test" name="my.block.name" template="Namespace_Modulename::product/list.phtml" />
        </referenceContainer>
    </body>
</page>

copy this list.phtml file from

vendor/magento/module-catalog/view/frontend/templates/product/list.phtml

to

view/frontend/templates/product/list.phtml

5
  • Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge Commented Jul 11, 2019 at 12:47
  • I did the above things in my module, but the list is not loading from my module. My module name is Faes_CCEngine. Commented Jul 11, 2019 at 13:53
  • Please check screenshots Commented Jul 11, 2019 at 14:00
  • okay i will look into it
    – Mohit Rane
    Commented Jul 11, 2019 at 14:01
  • Hi, any updates? @MohitRane Commented Jul 12, 2019 at 3:14

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.