diff --git a/app/code/Magento/Catalog/Block/Product/ProductList/Related.php b/app/code/Magento/Catalog/Block/Product/ProductList/Related.php index 3a85443d070d4..78a9e8c4594df 100644 --- a/app/code/Magento/Catalog/Block/Product/ProductList/Related.php +++ b/app/code/Magento/Catalog/Block/Product/ProductList/Related.php @@ -131,4 +131,19 @@ public function getIdentities() } return $identities; } + + /** + * Find out if some products can be easy added to cart + * + * @return bool + */ + public function canItemsAddToCart() + { + foreach ($this->getItems() as $item) { + if (!$item->isComposite() && $item->isSaleable() && !$item->getRequiredOptions()) { + return true; + } + } + return false; + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Block/Product/ProductList/RelatedTest.php b/app/code/Magento/Catalog/Test/Unit/Block/Product/ProductList/RelatedTest.php index cd759fefce044..414e617530bc9 100644 --- a/app/code/Magento/Catalog/Test/Unit/Block/Product/ProductList/RelatedTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Block/Product/ProductList/RelatedTest.php @@ -41,4 +41,47 @@ public function testGetIdentities() $this->block->getIdentities() ); } + + /** + * @dataProvider canItemsAddToCartDataProvider + * @param bool $isComposite + * @param bool $isSaleable + * @param bool $hasRequiredOptions + * @param bool $canItemsAddToCart + */ + public function testCanItemsAddToCart($isComposite, $isSaleable, $hasRequiredOptions, $canItemsAddToCart) + { + $product = $this->getMock( + 'Magento\Catalog\Model\Product', + ['isComposite', 'isSaleable', 'getRequiredOptions'], + [], + '', + false + ); + $product->expects($this->any())->method('isComposite')->willReturn($isComposite); + $product->expects($this->any())->method('isSaleable')->willReturn($isSaleable); + $product->expects($this->any())->method('getRequiredOptions')->willReturn($hasRequiredOptions); + + $itemsCollection = new \ReflectionProperty( + 'Magento\Catalog\Block\Product\ProductList\Related', + '_itemCollection' + ); + $itemsCollection->setAccessible(true); + $itemsCollection->setValue($this->block, [$product]); + + $this->assertEquals( + $canItemsAddToCart, + $this->block->canItemsAddToCart() + ); + } + + public function canItemsAddToCartDataProvider() + { + return [ + [false, true, false, true], + [false, false, false, false], + [true, false, false, false], + [true, false, true, false], + ]; + } } diff --git a/app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml b/app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml index 7260913b0c67c..65b0f6c600093 100644 --- a/app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml +++ b/app/code/Magento/Catalog/view/frontend/templates/product/list/items.phtml @@ -23,6 +23,7 @@ switch ($type = $block->getType()) { $items = $block->getAllItems(); $limit = $block->getPositionLimit(); $shuffle = (int) $block->isShuffled(); + $canItemsAddToCart = $block->canItemsAddToCart(); $showWishlist = true; $showCompare = true; @@ -44,6 +45,7 @@ switch ($type = $block->getType()) { $items = $block->getItems(); $limit = 0; $shuffle = 0; + $canItemsAddToCart = $block->canItemsAddToCart(); $showWishlist = true; $showCompare = true; @@ -70,6 +72,7 @@ switch ($type = $block->getType()) { $showCart = false; $templateType = null; $description = false; + $canItemsAddToCart = false; } break; @@ -91,6 +94,7 @@ switch ($type = $block->getType()) { $showCart = false; $templateType = null; $description = false; + $canItemsAddToCart = false; } break; @@ -110,6 +114,7 @@ switch ($type = $block->getType()) { $showCart = true; $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW; $description = false; + $canItemsAddToCart = false; } break; @@ -129,6 +134,7 @@ switch ($type = $block->getType()) { $showCart = true; $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW; $description = false; + $canItemsAddToCart = false; } break; @@ -150,6 +156,7 @@ switch ($type = $block->getType()) { $showCart = true; $templateType = \Magento\Catalog\Block\Product\ReviewRendererInterface::SHORT_VIEW; $description = ($mode == 'list') ? true : false; + $canItemsAddToCart = false; } break; @@ -173,7 +180,7 @@ switch ($type = $block->getType()) {