Can add already existing product as variant

ONLY if the reference is forced
This commit is contained in:
Cédric 2019-11-23 16:14:21 +01:00 committed by GitHub
parent 6484ba10c5
commit 6086ecef86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -514,7 +514,23 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$db->begin(); $db->begin();
$forced_refvar = trim($forced_refvar);
if (!empty($forced_refvar) && $forced_refvar != $product->ref) {
$existingProduct = new Product($db);
$result = $existingProduct->fetch('', $forced_refvar);
if ($result > 0) {
$newproduct = $existingProduct;
} else {
$existingProduct = false;
$newproduct = clone $product; $newproduct = clone $product;
$newproduct->ref = $forced_refvar;
}
} else {
$forced_refvar = false;
$existingProduct = false;
$newproduct = clone $product;
}
//Final weight impact //Final weight impact
$weight_impact = $forced_weightvar; $weight_impact = $forced_weightvar;
@ -530,10 +546,6 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$price_impact = 0; $price_impact = 0;
} }
if ($forced_refvar !== false) {
$forced_refvar = trim($forced_refvar);
}
$newcomb = new ProductCombination($db); $newcomb = new ProductCombination($db);
$existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations); $existingCombination = $newcomb->fetchByProductCombination2ValuePairs($product->id, $combinations);
@ -541,7 +553,6 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$newcomb = $existingCombination; $newcomb = $existingCombination;
} else { } else {
$newcomb->fk_product_parent = $product->id; $newcomb->fk_product_parent = $product->id;
if ($newcomb->create($user) < 0) { // Create 1 entry into product_attribute_combination (1 entry for all combinations) if ($newcomb->create($user) < 0) { // Create 1 entry into product_attribute_combination (1 entry for all combinations)
$db->rollback(); $db->rollback();
return -1; return -1;
@ -577,14 +588,13 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
if ($forced_pricevar === false) { if ($forced_pricevar === false) {
$price_impact += (float) price2num($variations[$currcombattr][$currcombval]['price']); $price_impact += (float) price2num($variations[$currcombattr][$currcombval]['price']);
} }
if (empty($forced_refvar)) {
if ($forced_refvar === false) {
if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) { if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
$newproduct->ref .= $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR . $prodattrval->ref; $newproduct->ref .= $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR . $prodattrval->ref;
} else { } else {
$newproduct->ref .= '_'.$prodattrval->ref; $newproduct->ref .= '_'.$prodattrval->ref;
} }
} else {
$newproduct->ref = $forced_refvar;
} }
//The first one should not contain a linebreak //The first one should not contain a linebreak
@ -600,6 +610,9 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$newproduct->weight += $weight_impact; $newproduct->weight += $weight_impact;
// Now create the product
//print 'Create prod '.$newproduct->ref.'<br>'."\n";
if ($existingProduct === false) {
//To avoid wrong information in price history log //To avoid wrong information in price history log
$newproduct->price = 0; $newproduct->price = 0;
$newproduct->price_ttc = 0; $newproduct->price_ttc = 0;
@ -608,11 +621,9 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
// A new variant must use a new barcode (not same product) // A new variant must use a new barcode (not same product)
$newproduct->barcode = -1; $newproduct->barcode = -1;
$result = $newproduct->create($user);
// Now create the product if ($result < 0)
//print 'Create prod '.$newproduct->ref.'<br>'."\n";
$newprodid = $newproduct->create($user);
if ($newprodid < 0)
{ {
//In case the error is not related with an already existing product //In case the error is not related with an already existing product
if ($newproduct->error != 'ErrorProductAlreadyExists') { if ($newproduct->error != 'ErrorProductAlreadyExists') {
@ -650,8 +661,14 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$db->rollback(); $db->rollback();
return -1; return -1;
} }
}
$newproduct->weight += $weight_impact; } else {
$result = $newproduct->update($newproduct->id, $user);
if ($result < 0)
{
$db->rollback();
return -1;
}
} }
$newcomb->fk_product_child = $newproduct->id; $newcomb->fk_product_child = $newproduct->id;