Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved with NoSuchEntityException catch for item in stock #3

Merged
merged 2 commits into from
Jan 22, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Model/Resolver/EntityUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ public function resolve(
$collection = $this->productCollectionFactory->create()
->addAttributeToFilter('status', ['eq' => Status::STATUS_ENABLED]);
$product = $collection->addIdFilter($id)->getFirstItem();
$isInStock = $this->stockItemRepository->get($id)->getIsInStock();
$isInStock = false;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey!
If stockItemRepository fails to get stock status by product id is it really means that this product is out of stock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AleksandrsKondratjevs Technically yes. If we are unable to get the product, how can we verify the stock?


try {
$isInStock = $this->stockItemRepository->get($id)->getIsInStock();
throw new NoSuchEntityException();
} catch (NoSuchEntityException $e) {
// Ignoring error is safe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of ignoring the error I would return null; but it is a preference. Merging.

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is going on here? So you throw the error manually to achieve what? This seem to me to work OK even without doing that throw new NoSuchEntityException();.


$isOutOfStockDisplay = $this->scopeConfig->getValue(
self::XML_PATH_CATALOGINVENTORY_SHOW_OUT_OF_STOCK,
Expand Down