Skip to content

Commit

Permalink
Added product id to filters, fixed current_category issue. Added opti…
Browse files Browse the repository at this point in the history
…on to load category by id.
  • Loading branch information
alfredsgenkins committed Apr 3, 2020
1 parent 7fdb02f commit 83d9ace
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,19 @@ public function apply(Filter $filter, AbstractDb $collection)
/** @var Collection $collection */
$category = $this->categoryFactory->create();

$categoryId = $this->categoryResourceCollection->addAttributeToFilter($filterField, $filterValue)
->addAttributeToSelect(['entity_id'])->getFirstItem()->getEntityId();
if ($filterField !== 'id') {
$categoryId = $this->categoryResourceCollection
->addAttributeToFilter($filterField, $filterValue)
->addAttributeToSelect(['entity_id'])
->getFirstItem()
->getEntityId();

$this->categoryResourceModel->load($category, $categoryId);
} else {
$this->categoryResourceModel->load($category, $filterValue);
}

$this->categoryResourceModel->load($category, $categoryId);
$this->registry->register('current_category', $category);

$collection->addCategoryFilter($category);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function apply(Filter $filter, AbstractDb $collection)
->addAttributeToFilter($attributeName, [$conditionType => $attributeValue])
->addAttributeToFilter('status', Status::STATUS_ENABLED);


$simpleSelect->getSelect()
->reset(\Zend_Db_Select::COLUMNS)
->columns(['e.entity_id']);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* ScandiPWA_CatalogGraphQl
*
* @category ScandiPWA
* @package ScandiPWA_CatalogGraphQl
* @author Viktors Pliska <[email protected]>
* @copyright Copyright (c) 2018 Scandiweb, Ltd (https://scandiweb.com)
*/

declare(strict_types=1);

namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Api\Filter;

/**
* Product filter allows to filter products collection using custom defined filters from search criteria.
*/
class ProductIdFilter implements CustomFilterInterface
{
/**
* @param Filter $filter
* @param AbstractDb $collection
* @return bool
*/
public function apply(Filter $filter, AbstractDb $collection)
{
$attributeValue = $filter->getValue();
$conditionType = $filter->getConditionType();

$collection->getSelect()
->where($collection->getConnection()->prepareSqlCondition(
'e.entity_id',
[$conditionType => $attributeValue]
));

return true;
}
}
4 changes: 3 additions & 1 deletion src/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
<item name="conditions" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ConditionsFilter</item>
<item name="category_url_key" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter</item>
<item name="category_url_path" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter</item>
<item name="category_id" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter</item>

<item name="price" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductPriceFilter</item>
<item name="min_price" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductPriceFilter</item>
<item name="max_price" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductPriceFilter</item>
<item name="category_id" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\CategoryFilter</item>

<item name="store" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter</item>
<item name="store_id" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductStoreFilter</item>
<item name="website_id" xsi:type="object">Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor\ProductWebsiteFilter</item>

<item name="id" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ProductIdFilter</item>
</argument>
<argument name="defaultFilter" xsi:type="object">ScandiPWA\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ConfigurableProductAttributeFilter</argument>
</arguments>
Expand Down
1 change: 1 addition & 0 deletions src/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MediaGalleryImageOfType @doc(description: "Products thumbnail image") {
}

input ProductFilterInput {
id: FilterTypeInput @doc(description: "Product id")
category_url_key: FilterTypeInput @doc(description: "Category url_key the product belongs to")
category_url_path: FilterTypeInput @doc(description: "Category url_path the product belongs to")
conditions: FilterTypeInput @doc(description: "Searches products by JSON formatted conditions")
Expand Down

0 comments on commit 83d9ace

Please sign in to comment.