Skip to content

Commit 821ee76

Browse files
committed
MAGETWO-60605: Exception when adding configurable product by sku from customer account if associated simple product is out of stock
2 parents ec5a7f5 + 10770c3 commit 821ee76

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

app/code/Magento/ConfigurableProduct/Pricing/Price/ConfigurablePriceResolver.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public function __construct(
5353

5454
/**
5555
* @param \Magento\Framework\Pricing\SaleableInterface|\Magento\Catalog\Model\Product $product
56-
* @return float
57-
* @throws \Magento\Framework\Exception\LocalizedException
56+
* @return float | null
5857
*/
5958
public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $product)
6059
{
@@ -64,12 +63,7 @@ public function resolvePrice(\Magento\Framework\Pricing\SaleableInterface $produ
6463
$productPrice = $this->priceResolver->resolvePrice($subProduct);
6564
$price = $price ? min($price, $productPrice) : $productPrice;
6665
}
67-
if ($price === null) {
68-
throw new \Magento\Framework\Exception\LocalizedException(
69-
__('Configurable product "%1" does not have sub-products', $product->getSku())
70-
);
71-
}
7266

73-
return (float)$price;
67+
return $price === null ? null : (float)$price;
7468
}
7569
}

app/code/Magento/ConfigurableProduct/Test/Unit/Pricing/Price/ConfigurablePriceResolverTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,18 @@ protected function setUp()
5353

5454
/**
5555
* situation: There are no used products, thus there are no prices
56-
*
57-
* @expectedException \Magento\Framework\Exception\LocalizedException
5856
*/
5957
public function testResolvePriceWithNoPrices()
6058
{
6159
$product = $this->getMockBuilder(
6260
\Magento\Catalog\Model\Product::class
6361
)->disableOriginalConstructor()->getMock();
6462

65-
$product->expects($this->once())->method('getSku')->willReturn('Kiwi');
63+
$product->expects($this->never())->method('getSku');
6664

6765
$this->lowestPriceOptionsProvider->expects($this->once())->method('getProducts')->willReturn([]);
6866

69-
$this->resolver->resolvePrice($product);
67+
$this->assertNull($this->resolver->resolvePrice($product));
7068
}
7169

7270
/**
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Pricing\Price;
7+
8+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Catalog\Model\Product\Attribute\Source\Status;
11+
12+
class FinalPriceTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @var ProductRepositoryInterface
16+
*/
17+
private $productRepository;
18+
19+
protected function setUp()
20+
{
21+
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
22+
}
23+
24+
/**
25+
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
26+
*/
27+
public function testNullPriceForConfigurbaleIfAllChildIsOutOfStock()
28+
{
29+
//prepare configurable product
30+
$configurableProduct = $this->productRepository->get('configurable', false, null, true);
31+
foreach ($configurableProduct->getExtensionAttributes()->getConfigurableProductLinks() as $productId) {
32+
$product = $this->productRepository->getById($productId);
33+
$product->getExtensionAttributes()->getStockItem()->setIsInStock(0);
34+
$this->productRepository->save($product);
35+
}
36+
37+
$finalPrice = Bootstrap::getObjectManager()->create(
38+
FinalPrice::class,
39+
[
40+
'saleableItem' => $configurableProduct,
41+
'quantity' => 1
42+
]
43+
);
44+
45+
static::assertNull($finalPrice->getValue());
46+
}
47+
48+
/**
49+
* @magentoDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
50+
*/
51+
public function testNullPriceForConfigurbaleIfAllChildIsDisabled()
52+
{
53+
//prepare configurable product
54+
$configurableProduct = $this->productRepository->get('configurable', false, null, true);
55+
foreach ($configurableProduct->getExtensionAttributes()->getConfigurableProductLinks() as $productId) {
56+
$product = $this->productRepository->getById($productId);
57+
$product->setStatus(Status::STATUS_DISABLED);
58+
$this->productRepository->save($product);
59+
}
60+
61+
$finalPrice = Bootstrap::getObjectManager()->create(
62+
FinalPrice::class,
63+
[
64+
'saleableItem' => $configurableProduct,
65+
'quantity' => 1
66+
]
67+
);
68+
69+
static::assertNull($finalPrice->getValue());
70+
}
71+
}

0 commit comments

Comments
 (0)