Skip to content

Commit

Permalink
Merge pull request #2 from AleksandrsKondratjevs/issue-1640
Browse files Browse the repository at this point in the history
Respect category active status & out of stock product display
  • Loading branch information
alfredsgenkins authored Jan 12, 2021
2 parents 3cb9f1a + bfea621 commit b8352f9
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions src/Model/Resolver/EntityUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
Expand All @@ -21,12 +22,20 @@
use Magento\UrlRewrite\Model\UrlFinderInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewriteGraphQl\Model\Resolver\UrlRewrite\CustomUrlLocatorInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\Store\Model\ScopeInterface;

/**
* UrlRewrite field resolver, used for GraphQL request processing.
*/
class EntityUrl implements ResolverInterface
{
/**
* Config key 'Display Out of Stock Products'
*/
const XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK = 'cataloginventory/options/show_out_of_stock';

/**
* @var UrlFinderInterface
*/
Expand All @@ -47,22 +56,47 @@ class EntityUrl implements ResolverInterface
*/
private $productCollectionFactory;

/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var StockItemRepository
*/
private $stockItemRepository;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;


/**
* @param UrlFinderInterface $urlFinder
* @param StoreManagerInterface $storeManager
* @param CustomUrlLocatorInterface $customUrlLocator
* @param CollectionFactory $productCollectionFactory
* @param CategoryRepositoryInterface $categoryRepository
* @param StockItemRepository $stockItemRepository
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
UrlFinderInterface $urlFinder,
StoreManagerInterface $storeManager,
CustomUrlLocatorInterface $customUrlLocator,
CollectionFactory $productCollectionFactory
CollectionFactory $productCollectionFactory,
CategoryRepositoryInterface $categoryRepository,
StockItemRepository $stockItemRepository,
ScopeConfigInterface $scopeConfig
) {
$this->urlFinder = $urlFinder;
$this->storeManager = $storeManager;
$this->customUrlLocator = $customUrlLocator;
$this->productCollectionFactory = $productCollectionFactory;
$this->categoryRepository = $categoryRepository;
$this->stockItemRepository = $stockItemRepository;
$this->scopeConfig = $scopeConfig;
}

/**
Expand Down Expand Up @@ -105,11 +139,29 @@ public function resolve(
$collection = $this->productCollectionFactory->create()
->addAttributeToFilter('status', ['eq' => Status::STATUS_ENABLED]);
$product = $collection->addIdFilter($id)->getFirstItem();
if (!$product->hasData()) {
$isInStock = $this->stockItemRepository->get($id)->getIsInStock();

$isOutOfStockDisplay = $this->scopeConfig->getValue(
self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK,
ScopeInterface::SCOPE_STORE
);

/*
* return 404 page if product has no data
* or out of stock product display is disabled
*/
if (!$product->hasData() || !$isOutOfStockDisplay && !$isInStock) {
return null;
}

$result['sku'] = $product->getSku();
} elseif ($type === 'CATEGORY') {
$storeId = $this->storeManager->getStore()->getId();
$category = $this->categoryRepository->get($id, $storeId);

if (!$category->getIsActive()) {
return null;
}
}
}

Expand Down

0 comments on commit b8352f9

Please sign in to comment.