Last active
June 27, 2022 02:52
-
-
Save Alexander-Pop/8271f61e62e1bed99e1f2849bfbc5dde to your computer and use it in GitHub Desktop.
Magento 2 - Filter product / customer using search criteria #magento2 #product #search #customer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Namespace\MyModule\Block | |
use Magento\Customer\Api\CustomerRepositoryInterface; | |
use Magento\Customer\Api\Data\CustomerInterface; | |
use Magento\Framework\Api\SearchCriteriaBuilder; | |
use Magento\Framework\View\Element\Template; | |
class useCriteria extends Template | |
{ | |
/** | |
* useCriteria constructor. | |
* @param CustomerRepositoryInterface $customerRepository | |
* @param SearchCriteriaBuilder $searchCriteriaBuilder | |
*/ | |
public function __construct( | |
CustomerRepositoryInterface $customerRepository, | |
SearchCriteriaBuilder $searchCriteriaBuilder | |
) { | |
$this->customerRepository = $customerRepository | |
$this->searchCriteriaBuilder = $searchCriteriaBuilder | |
} | |
/** | |
* Return CustomerInterface | null | |
*/ | |
public function getSpecificCustomer() { | |
$email = 'foocustomer@gmail.com'; | |
$websiteId = 2; | |
$searchCriteria = $this->searchCriteriaBuilder | |
->addFilter('email', $email, 'eq') | |
->addFilter('website_id', $websiteId, 'eq') | |
->create(); | |
$customerCollection = $this->customerRepository->getList($searchCriteria); | |
$customer = $customerCollection->getFirstItem(); | |
return !!$customer->getId() !== false ? $customer : null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyNamespace\MyModule\Model; | |
use Magento\Framework\Api\SearchCriteriaBuilder; | |
use Magento\Catalog\Api\ProductRepositoryInterface; | |
class ProductFilter | |
{ | |
/** @var ProductRepositoryInterface */ | |
protected $productRepository; | |
/** @var SearchCriteriaBuilder */ | |
protected $searchCriteriaBuilder; | |
/** | |
* Initialize dependencies. | |
* | |
* @param ProductRepositoryInterface $productRepository | |
* @param SearchCriteriaBuilder $searchCriteriaBuilder | |
*/ | |
public function __construct( | |
ProductRepositoryInterface $productRepository, | |
SearchCriteriaBuilder $searchCriteriaBuilder | |
) { | |
$this->productRepository = $productRepository; | |
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | |
} | |
/** | |
* Get products with filter. | |
* | |
* @param string $fieldName | |
* @param string $fieldValue | |
* @param string $filterType | |
* @return \Magento\Catalog\Api\Data\ProductInterface[] | |
*/ | |
public function getProducts($fieldName, $fieldValue, $filterType) | |
{ | |
$searchCriteria = $this->searchCriteriaBuilder->addFilter($fieldName, $fieldValue, $filterType)->create(); | |
$products = $this->productRepository->getList($searchCriteria); | |
return $products->getItems(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment