0

I've added a field for my customer (called "Group").

I want to show the product from the catalog based on this group (I have added a "group" field in my product).

What do I need to override to do this ?

EDIT : based on answer I found a start for the solution, I'm overriding ListProduct.php but when I change the collection sort by adding addAttributeToFilter it shows the same result. See :

    protected function _getProductCollection()
{
    $group = $this->customerSession->getCustomer()->getData("groupe"); //NOT CUSTOMER GROUP, BUT A CUSTOM FIELD CALLED GROUP

    if ($this->_productCollection === null) {
        $this->_productCollection = $this->initializeProductCollection();
        $this->_productCollection->addFieldToSelect("*");
        try {
            $this->_productCollection->addAttributeToFilter("groupe_product", array('eq' => $group));
        } catch (LocalizedException $e) {
            echo $e;
        }

    }

    return $this->_productCollection;
}

Any thoughts ?

1

you can try this to filter the product based on customer groupId but remember you have to add customer group id to product

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class yourclass{

protected $_customerSession;

protected $_productCollection;

public function __construct(

    \Magento\Customer\Model\Session $customerSession,
    CollectionFactory $collectionFactory,


)
{
    $this->_customerSession = $customerSession;
    $this->_productCollection = $collectionFactory;

}

public function getGroupId() { if ($this->_customerSession->isLoggedIn()) {

        return $this->_customerSession->getCustomer()->getGroupId();
    }
}

public function yourfunction{

    $groupId = $this->getGroupId();

   $collection = $this->_productCollection->create()
        ->addFieldToSelect("*")
        ->addFieldToFilter('group_id', $groupId);

// do your rest of work

}

  • What file should I override ? – Morgan Tartreau Mar 26 at 15:40
  • first add groupId to the productcollection and override the listproduct.php and let me if any issues – mohith Mar 27 at 4:27
  • Ended up to do something like that. Thanks ! – Morgan Tartreau Apr 1 at 15:39
0

You need to use product collection and filter products based on that customer group you created. Please do this in app/code .create a new module and can you please specify in which page you need to display that products.

  • On basic listing page and on search results. – Morgan Tartreau Mar 26 at 15:43
  • override list.phtml file in your theme and modify the collection in block, list.phtml is for listing page and search page . Please let me know if you have any questions. – Prasanth Mar 29 at 4:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.