I want to filter the products, listed on a particular category page by an attribute valueProduct Image on category view not updated with new imageMagento 2 : Static blocks with products on category landing pageFilter product search by categoryIs it possible to filter products in the admin catalog by Category in Magento 2?Filter by attribute doesn't bring any productsMagento 2: How to show New Products in a particular Category?Resolved: How to exclude specific attributes's values products in category product listing page?Magento 2: Filter by custom product/category attribute returns 0 resultsMagento 2.2.5 get the product collection without Stock dataHow can I remove the category filter from category page that do not have further any categories?How to filter products by discount percentage on category page?

How important are the Author's mood and feelings for writing a story?

"This used to be my phone number"

Why should fork() have been designed to return a file descriptor?

Why are flying carpets banned while flying brooms are not?

Why teach C using scanf without talking about command line arguments?

Do higher dimensions have axes?

Counting multiples of 3 up to a given number

Locked-up DOS computer beeped on keypress. What mechanism caused that?

How should I interpret a promising preprint that was never published in a peer-reviewed journal?

Why would word of Princess Leia's capture generate sympathy for the Rebellion in the Senate?

How to get a type of "screech" on guitar

When a ball on a rope swings in a circle, is there both centripetal force and tension force?

What makes MOVEQ quicker than a normal MOVE in 68000 assembly?

Authorship dispute on a paper that came out of a final report of a course?

Could a US citizen born through "birth tourism" become President?

Get Chord Name From a Given Set of Notes

Inside Out and Back to Front

Are there any satellites in geosynchronous but not geostationary orbits?

Why don't humans perceive sound waves as twice the frequency they are?

You have no, but can try for yes

Is encryption still applied if you ignore the SSL certificate warning for self-signed certs?

Applying for jobs with an obvious scar

Simplest instruction set that has an c++/C compiler to write an emulator for?

Why is carrying a heavy object more taxing on the body than pushing the same object on wheels?



I want to filter the products, listed on a particular category page by an attribute value


Product Image on category view not updated with new imageMagento 2 : Static blocks with products on category landing pageFilter product search by categoryIs it possible to filter products in the admin catalog by Category in Magento 2?Filter by attribute doesn't bring any productsMagento 2: How to show New Products in a particular Category?Resolved: How to exclude specific attributes's values products in category product listing page?Magento 2: Filter by custom product/category attribute returns 0 resultsMagento 2.2.5 get the product collection without Stock dataHow can I remove the category filter from category page that do not have further any categories?How to filter products by discount percentage on category page?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















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










share|improve this question
























  • Update your code such that it will be easy to debug..

    – aravind
    Jul 11 at 14:01


















2















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










share|improve this question
























  • Update your code such that it will be easy to debug..

    – aravind
    Jul 11 at 14:01














2












2








2








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










share|improve this question
















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







magento2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 11 at 14:34







Sidharth Satheesh

















asked Jul 11 at 10:28









Sidharth SatheeshSidharth Satheesh

164 bronze badges




164 bronze badges












  • Update your code such that it will be easy to debug..

    – aravind
    Jul 11 at 14:01


















  • Update your code such that it will be easy to debug..

    – aravind
    Jul 11 at 14:01

















Update your code such that it will be easy to debug..

– aravind
Jul 11 at 14:01






Update your code such that it will be easy to debug..

– aravind
Jul 11 at 14:01











1 Answer
1






active

oldest

votes


















0














You can do this by following code,



<?php

namespace VendorModuleBlock;

class Test extends MagentoFrameworkViewElementTemplate

protected $_productCollectionFactory;
protected $_session;
protected $_customerFactory;

public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCustomerModelCustomerFactory $customerFactory,
MagentoCustomerModelSession $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="NamespaceModulenameBlockTest" 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







share|improve this answer

























  • Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge

    – Sidharth Satheesh
    Jul 11 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.

    – Sidharth Satheesh
    Jul 11 at 13:53











  • Please check screenshots

    – Sidharth Satheesh
    Jul 11 at 14:00











  • okay i will look into it

    – Mohit Rane
    Jul 11 at 14:01











  • Hi, any updates? @MohitRane

    – Sidharth Satheesh
    Jul 12 at 3:14













Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f281712%2fi-want-to-filter-the-products-listed-on-a-particular-category-page-by-an-attrib%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can do this by following code,



<?php

namespace VendorModuleBlock;

class Test extends MagentoFrameworkViewElementTemplate

protected $_productCollectionFactory;
protected $_session;
protected $_customerFactory;

public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCustomerModelCustomerFactory $customerFactory,
MagentoCustomerModelSession $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="NamespaceModulenameBlockTest" 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







share|improve this answer

























  • Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge

    – Sidharth Satheesh
    Jul 11 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.

    – Sidharth Satheesh
    Jul 11 at 13:53











  • Please check screenshots

    – Sidharth Satheesh
    Jul 11 at 14:00











  • okay i will look into it

    – Mohit Rane
    Jul 11 at 14:01











  • Hi, any updates? @MohitRane

    – Sidharth Satheesh
    Jul 12 at 3:14















0














You can do this by following code,



<?php

namespace VendorModuleBlock;

class Test extends MagentoFrameworkViewElementTemplate

protected $_productCollectionFactory;
protected $_session;
protected $_customerFactory;

public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCustomerModelCustomerFactory $customerFactory,
MagentoCustomerModelSession $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="NamespaceModulenameBlockTest" 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







share|improve this answer

























  • Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge

    – Sidharth Satheesh
    Jul 11 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.

    – Sidharth Satheesh
    Jul 11 at 13:53











  • Please check screenshots

    – Sidharth Satheesh
    Jul 11 at 14:00











  • okay i will look into it

    – Mohit Rane
    Jul 11 at 14:01











  • Hi, any updates? @MohitRane

    – Sidharth Satheesh
    Jul 12 at 3:14













0












0








0







You can do this by following code,



<?php

namespace VendorModuleBlock;

class Test extends MagentoFrameworkViewElementTemplate

protected $_productCollectionFactory;
protected $_session;
protected $_customerFactory;

public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCustomerModelCustomerFactory $customerFactory,
MagentoCustomerModelSession $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="NamespaceModulenameBlockTest" 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







share|improve this answer















You can do this by following code,



<?php

namespace VendorModuleBlock;

class Test extends MagentoFrameworkViewElementTemplate

protected $_productCollectionFactory;
protected $_session;
protected $_customerFactory;

public function __construct(
MagentoBackendBlockTemplateContext $context,
MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,
MagentoCustomerModelCustomerFactory $customerFactory,
MagentoCustomerModelSession $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="NamespaceModulenameBlockTest" 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








share|improve this answer














share|improve this answer



share|improve this answer








edited Jul 11 at 13:12

























answered Jul 11 at 12:22









Mohit RaneMohit Rane

76816 bronze badges




76816 bronze badges












  • Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge

    – Sidharth Satheesh
    Jul 11 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.

    – Sidharth Satheesh
    Jul 11 at 13:53











  • Please check screenshots

    – Sidharth Satheesh
    Jul 11 at 14:00











  • okay i will look into it

    – Mohit Rane
    Jul 11 at 14:01











  • Hi, any updates? @MohitRane

    – Sidharth Satheesh
    Jul 12 at 3:14

















  • Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge

    – Sidharth Satheesh
    Jul 11 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.

    – Sidharth Satheesh
    Jul 11 at 13:53











  • Please check screenshots

    – Sidharth Satheesh
    Jul 11 at 14:00











  • okay i will look into it

    – Mohit Rane
    Jul 11 at 14:01











  • Hi, any updates? @MohitRane

    – Sidharth Satheesh
    Jul 12 at 3:14
















Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge

– Sidharth Satheesh
Jul 11 at 12:47






Where should I be calling getProductCollection(); ? I need to list these products in a page Being new to Magento, excuse my level of knowledge

– Sidharth Satheesh
Jul 11 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.

– Sidharth Satheesh
Jul 11 at 13:53





I did the above things in my module, but the list is not loading from my module. My module name is Faes_CCEngine.

– Sidharth Satheesh
Jul 11 at 13:53













Please check screenshots

– Sidharth Satheesh
Jul 11 at 14:00





Please check screenshots

– Sidharth Satheesh
Jul 11 at 14:00













okay i will look into it

– Mohit Rane
Jul 11 at 14:01





okay i will look into it

– Mohit Rane
Jul 11 at 14:01













Hi, any updates? @MohitRane

– Sidharth Satheesh
Jul 12 at 3:14





Hi, any updates? @MohitRane

– Sidharth Satheesh
Jul 12 at 3:14

















draft saved

draft discarded
















































Thanks for contributing an answer to Magento Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f281712%2fi-want-to-filter-the-products-listed-on-a-particular-category-page-by-an-attrib%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form