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();
$newproduct = clone $product; $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->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 (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) { if ($forced_refvar === false) {
$newproduct->ref .= $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR . $prodattrval->ref; if (isset($conf->global->PRODUIT_ATTRIBUTES_SEPARATOR)) {
} else { $newproduct->ref .= $conf->global->PRODUIT_ATTRIBUTES_SEPARATOR . $prodattrval->ref;
$newproduct->ref .= '_'.$prodattrval->ref; } else {
} $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,58 +610,65 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
$newproduct->weight += $weight_impact; $newproduct->weight += $weight_impact;
//To avoid wrong information in price history log
$newproduct->price = 0;
$newproduct->price_ttc = 0;
$newproduct->price_min = 0;
$newproduct->price_min_ttc = 0;
// A new variant must use a new barcode (not same product)
$newproduct->barcode = -1;
// Now create the product // Now create the product
//print 'Create prod '.$newproduct->ref.'<br>'."\n"; //print 'Create prod '.$newproduct->ref.'<br>'."\n";
$newprodid = $newproduct->create($user); if ($existingProduct === false) {
if ($newprodid < 0) //To avoid wrong information in price history log
{ $newproduct->price = 0;
//In case the error is not related with an already existing product $newproduct->price_ttc = 0;
if ($newproduct->error != 'ErrorProductAlreadyExists') { $newproduct->price_min = 0;
$this->error[] = $newproduct->error; $newproduct->price_min_ttc = 0;
$this->errors = $newproduct->errors;
$db->rollback();
return -1;
}
/** // A new variant must use a new barcode (not same product)
* If there is an existing combination, then we update the prices and weight $newproduct->barcode = -1;
* Otherwise, we try adding a random number to the ref $result = $newproduct->create($user);
*/
if ($newcomb->fk_product_child) { if ($result < 0)
$res = $newproduct->fetch($existingCombination->fk_product_child); {
} else { //In case the error is not related with an already existing product
$orig_prod_ref = $newproduct->ref; if ($newproduct->error != 'ErrorProductAlreadyExists') {
$i = 1; $this->error[] = $newproduct->error;
$this->errors = $newproduct->errors;
$db->rollback();
return -1;
}
do { /**
$newproduct->ref = $orig_prod_ref.$i; * If there is an existing combination, then we update the prices and weight
$res = $newproduct->create($user); * Otherwise, we try adding a random number to the ref
*/
if ($newproduct->error != 'ErrorProductAlreadyExists') { if ($newcomb->fk_product_child) {
$this->errors[] = $newproduct->error; $res = $newproduct->fetch($existingCombination->fk_product_child);
break; } else {
} $orig_prod_ref = $newproduct->ref;
$i = 1;
$i++; do {
} while ($res < 0); $newproduct->ref = $orig_prod_ref.$i;
} $res = $newproduct->create($user);
if ($res < 0) { if ($newproduct->error != 'ErrorProductAlreadyExists') {
$db->rollback(); $this->errors[] = $newproduct->error;
return -1; break;
} }
$newproduct->weight += $weight_impact; $i++;
} while ($res < 0);
}
if ($res < 0) {
$db->rollback();
return -1;
}
}
} 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;