Skip to content

Product name not shown in message when trying to add more products to cart then there are in stock #8660

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ public function checkQuoteItemQty(StockItemInterface $stockItem, $qty, $summaryQ
{
$result = $this->objectFactory->create();
$result->setHasError(false);

$product = $this->productFactory->create();
$product->load($stockItem->getProductId());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use ProductRepository here? Is product name always missing or only in some cases?

Copy link
Author

Choose a reason for hiding this comment

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

@orlangur the name is always missing, because there is no product available.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you need to load Product you have to rely on ProductRepostioryInterface::get method.
loading product using the load method of Product Model you are using deprecated method of Abstract Model https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Model/AbstractModel.php#L526

$stockItem->setProduct($product);
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, there is unit test failure:

Fatal error: Call to undefined method Mock_StockItemInterface_837dff1c::setProduct() in /home/travis/build/magento/magento2/app/code/Magento/CatalogInventory/Model/StockStateProvider.php on line 112

Is there a way to achieve the same using only defined interfaces?

Copy link
Author

Choose a reason for hiding this comment

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

@orlangur
This is weird, because the interface already loaded Magento\CatalogInventory\Model\Stock\Item
And there is a public function setProduct available.
In my system it is working fine, no errors.

Copy link
Contributor

@orlangur orlangur Feb 23, 2017

Choose a reason for hiding this comment

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

I do believe that the code works ;)

The problem is that this method is not part of StockItemInterface.

Lame way is just add setProduct to mock in failed test. The downside is that the code we have do not follow contract defined by StockItemInterface.

Better way is achieve the same correct behavior but use only defined contracts and do not rely on existing implementation. Why wrong product instance appear in StockItem at all? Of course, it may appear, that correct fix require much more efforts in such case breaking contract with setProduct call may be acceptable at it is lesser evil than having bug.

Copy link
Contributor

Choose a reason for hiding this comment

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

as your method accepts StockItemInterface as parameter, you should rely just on the contract provided by this interface https://github.com/magento/magento2/blob/develop/app/code/Magento/CatalogInventory/Api/Data/StockItemInterface.php
setProduct does not belong to StockItemInterface.
And couldn't belong, because StockItemInterface represents a relation between Stock and Product. You can consider it as a link entity, which holds the information about "what product" "in which quantity" stored on "what stock"


$qty = $this->getNumber($qty);

Expand Down