Can add already existing product as variant
ONLY if the reference is forced
This commit is contained in:
parent
6484ba10c5
commit
6086ecef86
@ -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;
|
||||||
@ -529,11 +545,7 @@ WHERE c.fk_product_parent = ".(int) $productid." AND p.tosell = 1";
|
|||||||
if ($forced_pricevar === false) {
|
if ($forced_pricevar === false) {
|
||||||
$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();
|
// A new variant must use a new barcode (not same product)
|
||||||
return -1;
|
$newproduct->barcode = -1;
|
||||||
}
|
$result = $newproduct->create($user);
|
||||||
|
|
||||||
/**
|
if ($result < 0)
|
||||||
* If there is an existing combination, then we update the prices and weight
|
{
|
||||||
* Otherwise, we try adding a random number to the ref
|
//In case the error is not related with an already existing product
|
||||||
*/
|
if ($newproduct->error != 'ErrorProductAlreadyExists') {
|
||||||
|
$this->error[] = $newproduct->error;
|
||||||
if ($newcomb->fk_product_child) {
|
$this->errors = $newproduct->errors;
|
||||||
$res = $newproduct->fetch($existingCombination->fk_product_child);
|
$db->rollback();
|
||||||
} else {
|
return -1;
|
||||||
$orig_prod_ref = $newproduct->ref;
|
}
|
||||||
$i = 1;
|
|
||||||
|
/**
|
||||||
do {
|
* If there is an existing combination, then we update the prices and weight
|
||||||
$newproduct->ref = $orig_prod_ref.$i;
|
* Otherwise, we try adding a random number to the ref
|
||||||
$res = $newproduct->create($user);
|
*/
|
||||||
|
|
||||||
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++;
|
|
||||||
} while ($res < 0);
|
do {
|
||||||
}
|
$newproduct->ref = $orig_prod_ref.$i;
|
||||||
|
$res = $newproduct->create($user);
|
||||||
if ($res < 0) {
|
|
||||||
$db->rollback();
|
if ($newproduct->error != 'ErrorProductAlreadyExists') {
|
||||||
return -1;
|
$this->errors[] = $newproduct->error;
|
||||||
}
|
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;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user