diff --git a/app/code/Magento/ConfigurableProduct/Model/Product/VariationHandler.php b/app/code/Magento/ConfigurableProduct/Model/Product/VariationHandler.php index 7ba330f5a060c615b74bcb6cba86e5116e36f767..321870b96d32abf13c48fdc0975efc9ba5248f17 100644 --- a/app/code/Magento/ConfigurableProduct/Model/Product/VariationHandler.php +++ b/app/code/Magento/ConfigurableProduct/Model/Product/VariationHandler.php @@ -148,10 +148,14 @@ class VariationHandler \Magento\Catalog\Model\Product $parentProduct, $postData ) { + $typeId = isset($postData['weight']) && !empty($postData['weight']) + ? ProductType::TYPE_SIMPLE + : ProductType::TYPE_VIRTUAL; + $product->setStoreId( \Magento\Store\Model\Store::DEFAULT_STORE_ID )->setTypeId( - $postData['weight'] ? ProductType::TYPE_SIMPLE : ProductType::TYPE_VIRTUAL + $typeId )->setAttributeSetId( $parentProduct->getNewVariationsAttributeSetId() ); diff --git a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/VariationHandlerTest.php b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/VariationHandlerTest.php index fb991879bbca391abc170e55c061762ab6aa4f2c..62f5a8089e350d247a75595d24214a08745acf47 100644 --- a/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/VariationHandlerTest.php +++ b/app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/VariationHandlerTest.php @@ -8,6 +8,7 @@ namespace Magento\ConfigurableProduct\Test\Unit\Model\Product; +use Magento\Catalog\Model\Product\Type; use Magento\ConfigurableProduct\Model\Product\VariationHandler; /** @@ -162,23 +163,30 @@ class VariationHandlerTest extends \PHPUnit_Framework_TestCase /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + * @dataProvider dataProviderTestGenerateSimpleProducts + * @param int|string|null $weight + * @param string $typeId */ - public function testGenerateSimpleProducts() + public function testGenerateSimpleProducts($weight, $typeId) { $productsData = [ - 6 => - [ - 'image' => 'image.jpg', - 'name' => 'config-red', - 'configurable_attribute' => '{"new_attr":"6"}', - 'sku' => 'config-red', - 'quantity_and_stock_status' => - [ - 'qty' => '', - ], - 'weight' => '333', - ] + [ + 'image' => 'image.jpg', + 'name' => 'config-red', + 'configurable_attribute' => '{"new_attr":"6"}', + 'sku' => 'config-red', + 'quantity_and_stock_status' => + [ + 'qty' => '', + ], + ] ]; + + // Do not add 'weight' attribute if it's value is null! + if ($weight !== null) { + $productsData[0]['weight'] = $weight; + } + $stockData = [ 'manage_stock' => '0', 'use_config_enable_qty_increments' => '1', @@ -218,7 +226,7 @@ class VariationHandlerTest extends \PHPUnit_Framework_TestCase ) ->disableOriginalConstructor() ->getMock(); - $productTypeMock = $this->getMockBuilder(\Magento\Catalog\Model\Product\Type::class) + $productTypeMock = $this->getMockBuilder(Type::class) ->setMethods(['getSetAttributes']) ->disableOriginalConstructor() ->getMock(); @@ -236,7 +244,7 @@ class VariationHandlerTest extends \PHPUnit_Framework_TestCase ->willReturn('new_attr_set_id'); $this->productFactoryMock->expects($this->once())->method('create')->willReturn($newSimpleProductMock); $newSimpleProductMock->expects($this->once())->method('setStoreId')->with(0)->willReturnSelf(); - $newSimpleProductMock->expects($this->once())->method('setTypeId')->with('simple')->willReturnSelf(); + $newSimpleProductMock->expects($this->once())->method('setTypeId')->with($typeId)->willReturnSelf(); $newSimpleProductMock->expects($this->once()) ->method('setAttributeSetId') ->with('new_attr_set_id') @@ -265,6 +273,27 @@ class VariationHandlerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(['product_id'], $this->model->generateSimpleProducts($parentProductMock, $productsData)); } + /** + * @return array + */ + public function dataProviderTestGenerateSimpleProducts() + { + return [ + [ + 'weight' => 333, + 'type_id' => Type::TYPE_SIMPLE, + ], + [ + 'weight' => '', + 'type_id' => Type::TYPE_VIRTUAL, + ], + [ + 'weight' => null, + 'type_id' => Type::TYPE_VIRTUAL, + ], + ]; + } + public function testProcessMediaGalleryWithImagesAndGallery() { $this->product->expects($this->atLeastOnce())->method('getMediaGallery')->with('images')->willReturn([]);