0

i have created

  • role as vendor with product and sales grid access
  • custom product attributes as vendor_id and assign a particular vendor to products using this
  • custom order attributes as vendor_id (i have spit the order based on vendor and assign vendor_id using custom order attributes)

when any user login as vendor i want to show only associated products and order in grids, is thare any conman class or anything from where i can filter for this.

0
<?php 

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\User\Api\Data;

class yourclass{

  protected $_user;
  protected $_customerSession;
  protected $_productCollection;

  public function __construct(

    Data $user,
    \Magento\Customer\Model\Session $customerSession,
    CollectionFactory $collectionFactory
  )
  {
    $this->_customerSession = $customerSession;
    $this->_user = $user;
    $this->_productCollection = $collectionFactory;
  }

  public function getVendorId() { 

      if ($this->_customerSession->isLoggedIn()) {
        return $this->_user->getId();
      }  
  }

  public function yourfunction{

      $vendorId = $this->getVendorId();

      if($vendorId){

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

      }
      // you will get your product collection
  }

//for order collection

namespace 'YOUR_CUSTOM_NAME_SPACE';

class YOURCALSS extends \Magento\Framework\App\Action\Action
{

    protected $_orderCollectionFactory;

    protected $_user;

    public function __construct(
        Magento\Framework\App\Action\Context $context,
    Data $user,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory 
    ) {

        $this->_user = $user;
        $this->_orderCollectionFactory = $orderCollectionFactory;
        parent::__construct($context);

    }

    public function getVendorId() { 

        return $this->_user->getId();

    }

    public function YOURFUNCTION()
    {
      $vendorId = $this->getVendorId();

      if($vendorId){
         $collection = $this->_orderCollectionFactory->create()->addAttributeToSelect('*');
         // You Can filter collection as 
         $this->orderCollectionFactory->addFieldToFilter('vendor_id',  $vendorId);

      }
    }      
}
1
  • let me know if any issues
    – mohith
    Apr 3 '19 at 5:55

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.