Skip to content

Commit e9de16a

Browse files
MAGETWO-98522: Adding out of stock items to comparison shows success message but fails
- Delete button if product is not available for comparison.
1 parent e457037 commit e9de16a

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\ViewModel\Product\Checker;
9+
10+
use Magento\Framework\View\Element\Block\ArgumentInterface;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
use Magento\CatalogInventory\Api\StockConfigurationInterface;
13+
14+
/**
15+
* Check is available add to compare.
16+
*/
17+
class AddToCompareAvailability implements ArgumentInterface
18+
{
19+
/**
20+
* @var StockConfigurationInterface
21+
*/
22+
private $stockConfiguration;
23+
24+
/**
25+
* @param StockConfigurationInterface $stockConfiguration
26+
*/
27+
public function __construct(StockConfigurationInterface $stockConfiguration)
28+
{
29+
$this->stockConfiguration = $stockConfiguration;
30+
}
31+
32+
/**
33+
* Is product available for comparison.
34+
*
35+
* @return bool
36+
*/
37+
public function isAvailableForCompare(ProductInterface $product): bool
38+
{
39+
return $this->isInStock($product) ||
40+
!$this->isInStock($product) && $this->stockConfiguration->isShowOutOfStock();
41+
}
42+
43+
/**
44+
* Get is in stock status.
45+
*
46+
* @return bool
47+
*/
48+
private function isInStock(ProductInterface $product): bool
49+
{
50+
$quantityAndStockStatus = $product->getQuantityAndStockStatus();
51+
if (!$quantityAndStockStatus) {
52+
return $product->isSalable();
53+
}
54+
55+
return isset($quantityAndStockStatus['is_in_stock'])
56+
? $quantityAndStockStatus['is_in_stock']
57+
: false;
58+
}
59+
}

app/code/Magento/Catalog/view/frontend/layout/catalog_product_view.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@
9191
<container name="product.info.social" label="Product social links container" htmlTag="div" htmlClass="product-social-links">
9292
<block class="Magento\Catalog\Block\Product\View" name="product.info.addto" as="addto" template="Magento_Catalog::product/view/addto.phtml">
9393
<block class="Magento\Catalog\Block\Product\View\AddTo\Compare" name="view.addto.compare" after="view.addto.wishlist"
94-
template="Magento_Catalog::product/view/addto/compare.phtml" />
94+
template="Magento_Catalog::product/view/addto/compare.phtml" >
95+
<arguments>
96+
<argument name="addToCompareViewModel" xsi:type="object">Magento\Catalog\ViewModel\Product\Checker\AddToCompareAvailability</argument>
97+
</arguments>
98+
</block>
9599
</block>
96100
<block class="Magento\Catalog\Block\Product\View" name="product.info.mailto" template="Magento_Catalog::product/view/mailto.phtml"/>
97101
</container>

app/code/Magento/Catalog/view/frontend/templates/product/view/addto/compare.phtml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
/** @var $block \Magento\Catalog\Block\Product\View\Addto\Compare */
1010
?>
1111

12+
<?php $viewModel = $block->getData('addToCompareViewModel'); ?>
13+
<?php if ($viewModel->isAvailableForCompare($block->getProduct())): ?>
1214
<a href="#" data-post='<?= /* @escapeNotVerified */ $block->getPostDataParams() ?>'
1315
data-role="add-to-links"
1416
class="action tocompare"><span><?= /* @escapeNotVerified */ __('Add to Compare') ?></span></a>
17+
<?php endif; ?>
18+

0 commit comments

Comments
 (0)