How to stop creating product as virtual in magento 2 programatically?
-
02-03-2021 - |
Question
Some time we forgot to add weight to the product and its become an automatically of type virtual product.
I am trying to generate a product of a type "simple" in case we forgot to add weight ...
I would like to by-pass a condition where product type is set as "virtual".
I have done following workaround for this:
in the file : vendor/magento/module-catalog/Model/Product/TypeTransitionManager.php
under function processProduct()
i have tried by commenting following line:
$product->setTypeId($productTypeId);
But it is not working.. How can we handle this programmatically?
Solution
Overwrite following class:
vendor/magento/module-configurable-product/Controller/Adminhtml/Product/Initialization/Helper/Plugin/Configurable.php
then modify getVariationMatrix method.
/**
* Get variation-matrix from request
*
* @return array
*/
protected function getVariationMatrix()
{
$result = [];
$configurableMatrix = $this->request->getParam('configurable-matrix-serialized', "[]");
if (isset($configurableMatrix) && $configurableMatrix != "") {
$configurableMatrix = json_decode($configurableMatrix, true);
foreach ($configurableMatrix as $item) {
if (isset($item['newProduct']) && $item['newProduct']) {
$result[$item['variationKey']] = $this->mapData($item);
if (isset($item['qty'])) {
$result[$item['variationKey']]['quantity_and_stock_status']['qty'] = $item['qty'];
}
$result[$item['variationKey']]['product_has_weight'] = 1;
$result[$item['variationKey']]['weight'] = 1;
}
}
}
return $result;
}
Actually, you need to pass following params:
$result[$item['variationKey']]['product_has_weight'] = 1;
$result[$item['variationKey']]['weight'] = 1;