Skip to content

Commit

Permalink
Make single product visible by direct URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilja Lapkovskis committed Jul 10, 2019
1 parent 1d5bea4 commit cffd426
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 16 deletions.
12 changes: 9 additions & 3 deletions src/Model/Resolver/Products/DataProvider/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Catalog\Api\Data\ProductSearchResultsInterfaceFactory;
use Magento\Framework\Api\SearchResultsInterface;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CriteriaCheck;

/**
* Product field data provider, used for GraphQL resolver processing.
Expand Down Expand Up @@ -93,9 +94,14 @@ public function getList(
$this->collectionProcessor->process($collection, $searchCriteria, $attributes);

if (!$isChildSearch) {
$visibilityIds = $isSearch
? $this->visibility->getVisibleInSearchIds()
: $this->visibility->getVisibleInCatalogIds();
$singleProduct = CriteriaCheck::isSingleProductFilter($searchCriteria);
if ($singleProduct) {
$visibilityIds = $this->visibility->getVisibleInSiteIds();
} else {
$visibilityIds = $isSearch
? $this->visibility->getVisibleInSearchIds()
: $this->visibility->getVisibleInCatalogIds();
}
$collection->setVisibility($visibilityIds);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@


use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor
\StockProcessor as MagentoStockProcessor;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\StockProcessor as MagentoStockProcessor;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\CatalogInventory\Api\StockConfigurationInterface;
use Magento\CatalogInventory\Model\ResourceModel\Stock\Status as StockStatusResource;
use Magento\Framework\Api\SearchCriteriaInterface;
use ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CriteriaCheck;

class StockProcessor extends MagentoStockProcessor implements CollectionProcessorInterface
{
Expand All @@ -28,7 +28,10 @@ class StockProcessor extends MagentoStockProcessor implements CollectionProcesso
* @param StockConfigurationInterface $stockConfig
* @param StockStatusResource $stockStatusResource
*/
public function __construct(StockConfigurationInterface $stockConfig, StockStatusResource $stockStatusResource)
public function __construct(
StockConfigurationInterface $stockConfig,
StockStatusResource $stockStatusResource
)
{
$this->stockConfig = $stockConfig;
$this->stockStatusResource = $stockStatusResource;
Expand All @@ -40,16 +43,7 @@ public function process(
array $attributeNames
): Collection
{
$singleProduct = false;
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
$filters = $filterGroup->getFilters();
$type = $filters[0]->getConditionType();
$field = $filters[0]->getField();
if ($type === 'eq' && $field === 'url_key') {
$singleProduct = true;
break;
}
}
$singleProduct = CriteriaCheck::isSingleProductFilter($searchCriteria);
if (!$singleProduct && !$this->stockConfig->isShowOutOfStock()) {
$this->stockStatusResource->addIsInStockFilterToCollection($collection);
}
Expand Down
30 changes: 30 additions & 0 deletions src/Model/Resolver/Products/DataProvider/Product/CriteriaCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace ScandiPWA\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product;


use Magento\Framework\Api\SearchCriteriaInterface;

class CriteriaCheck
{
/**
* @param SearchCriteriaInterface $searchCriteria
* @return bool
*/
static public function isSingleProductFilter(SearchCriteriaInterface$searchCriteria)
{
$singleProduct = false;
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
$filters = $filterGroup->getFilters();
$type = $filters[0]->getConditionType();
$field = $filters[0]->getField();
if ($type === 'eq' && $field === 'url_key') {
$singleProduct = true;
break;
}
}

return $singleProduct;
}
}

0 comments on commit cffd426

Please sign in to comment.