Skip to content

Commit 75c5285

Browse files
committed
MAGETWO-61095: When attempting to place a reorder after a product is disabled, product still gets added to the cart
1 parent 1db394f commit 75c5285

File tree

4 files changed

+84
-7
lines changed

4 files changed

+84
-7
lines changed

app/code/Magento/Quote/Model/Quote.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,12 @@ public function addProduct(
15721572
);
15731573
}
15741574

1575+
if (!$product->isSalable()) {
1576+
throw new \Magento\Framework\Exception\LocalizedException(
1577+
__('Product that you are trying to add is not available.')
1578+
);
1579+
}
1580+
15751581
$cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced($request, $product, $processMode);
15761582

15771583
/**

app/code/Magento/Quote/Test/Unit/Model/QuoteTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,10 @@ public function testAddProductNoCandidates()
854854
->with($this->equalTo(['qty' => 1]))
855855
->will($this->returnValue($requestMock));
856856

857+
$this->productMock->expects($this->once())
858+
->method('isSalable')
859+
->willReturn(true);
860+
857861
$typeInstanceMock = $this->getMock(
858862
\Magento\Catalog\Model\Product\Type\Simple::class,
859863
[
@@ -936,6 +940,10 @@ public function testAddProductItemPreparation()
936940
->method('create')
937941
->will($this->returnValue($collectionMock));
938942

943+
$this->productMock->expects($this->once())
944+
->method('isSalable')
945+
->willReturn(true);
946+
939947
$typeInstanceMock->expects($this->once())
940948
->method('prepareForCartAdvanced')
941949
->will($this->returnValue([$productMock]));

dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
99
use Magento\TestFramework\Helper\Bootstrap;
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Catalog\Model\ProductRepository;
1012

1113
/**
1214
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -32,7 +34,7 @@ public function testCollectTotalsWithVirtual()
3234
$productRepository = Bootstrap::getObjectManager()->create(
3335
\Magento\Catalog\Api\ProductRepositoryInterface::class
3436
);
35-
$product = $productRepository->get('virtual-product');
37+
$product = $productRepository->get('virtual-product', false, null, true);
3638
$quote->addProduct($product);
3739
$quote->collectTotals();
3840

@@ -46,9 +48,9 @@ public function testSetCustomerData()
4648
{
4749
/** @var \Magento\Quote\Model\Quote $quote */
4850
$quote = Bootstrap::getObjectManager()->create(\Magento\Quote\Model\Quote::class);
49-
/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
51+
/** @var CustomerInterfaceFactory $customerFactory */
5052
$customerFactory = Bootstrap::getObjectManager()->create(
51-
\Magento\Customer\Api\Data\CustomerInterfaceFactory::class
53+
CustomerInterfaceFactory::class
5254
);
5355
/** @var \Magento\Framework\Api\DataObjectHelper $dataObjectHelper */
5456
$dataObjectHelper = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\DataObjectHelper::class);
@@ -73,7 +75,7 @@ public function testUpdateCustomerData()
7375
/** @var \Magento\Quote\Model\Quote $quote */
7476
$quote = Bootstrap::getObjectManager()->create(\Magento\Quote\Model\Quote::class);
7577
$customerFactory = Bootstrap::getObjectManager()->create(
76-
\Magento\Customer\Api\Data\CustomerInterfaceFactory::class
78+
CustomerInterfaceFactory::class
7779
);
7880
/** @var \Magento\Framework\Api\DataObjectHelper $dataObjectHelper */
7981
$dataObjectHelper = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\DataObjectHelper::class);
@@ -118,9 +120,9 @@ public function testUpdateCustomerData()
118120
public function testGetCustomerGroupFromCustomer()
119121
{
120122
/** Preconditions */
121-
/** @var \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerFactory */
123+
/** @var CustomerInterfaceFactory $customerFactory */
122124
$customerFactory = Bootstrap::getObjectManager()->create(
123-
\Magento\Customer\Api\Data\CustomerInterfaceFactory::class
125+
CustomerInterfaceFactory::class
124126
);
125127
$customerGroupId = 3;
126128
$customerData = $customerFactory->create()->setId(1)->setGroupId($customerGroupId);
@@ -302,7 +304,7 @@ public function testAddProductUpdateItem()
302304
$productRepository = Bootstrap::getObjectManager()->create(
303305
\Magento\Catalog\Api\ProductRepositoryInterface::class
304306
);
305-
$product = $productRepository->get('simple-1');
307+
$product = $productRepository->get('simple-1', false, null, true);
306308

307309
$quote->addProduct($product, 50);
308310
$quote->setTotalsCollectedFlag(false)->collectTotals();
@@ -423,4 +425,29 @@ public function testReserveOrderId()
423425
$quote->reserveOrderId();
424426
$this->assertNotEquals('100000001', $quote->getReservedOrderId());
425427
}
428+
429+
/**
430+
* Test to verify that disabled product cannot be added to cart
431+
* @magentoDataFixture Magento/Quote/_files/disabled_product.php
432+
* @magentoAppIsolation enabled
433+
*/
434+
public function testAddedProductToQuoteIsSalable()
435+
{
436+
$productId = 1;
437+
$objectManager = Bootstrap::getObjectManager();
438+
439+
/** @var ProductRepository $productRepository */
440+
$productRepository = $objectManager->get(ProductRepository::class);
441+
442+
/** @var \Magento\Quote\Model\Quote $quote */
443+
$product = $productRepository->getById($productId, false, null, true);
444+
445+
$this->setExpectedException(
446+
LocalizedException::class,
447+
'Product that you are trying to add is not available.'
448+
);
449+
450+
$quote = $objectManager->create(\Magento\Quote\Model\Quote::class);
451+
$quote->addProduct($product);
452+
}
426453
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $objectManager \Magento\TestFramework\ObjectManager */
8+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
10+
$product->isObjectNew(true);
11+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
12+
->setId(1)
13+
->setAttributeSetId(4)
14+
->setWebsiteIds([1])
15+
->setName('Simple Product')
16+
->setSku('simple')
17+
->setPrice(10)
18+
->setWeight(1)
19+
->setShortDescription("Short description")
20+
->setTaxClassId(0)
21+
->setDescription('Description with <b>html tag</b>')
22+
->setMetaTitle('meta title')
23+
->setMetaKeyword('meta keyword')
24+
->setMetaDescription('meta description')
25+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
26+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)
27+
->setStockData(
28+
[
29+
'use_config_manage_stock' => 1,
30+
'qty' => 100,
31+
'is_qty_decimal' => 0,
32+
'is_in_stock' => 1,
33+
]
34+
)
35+
->setCanSaveCustomOptions(true)
36+
->setHasOptions(true);

0 commit comments

Comments
 (0)