16 hours ago, PrestaServicePro said:If the attribute group (e.g., "Size") does not exist, create it.
Ensure the Attributes Exist:
For each combination, ensure that the required attribute exists or create it if it does not.
Here’s the updated code to address these issues:
private function addProductSizes($idProducto, $tallas) { $firstLoop = true; // Variable para verificar la primera vuelta del foreach // Ensure the attribute group exists $attributeGroupName = 'Size'; $attributeGroupId = AttributeGroup::getIdByName($attributeGroupName); if (!$attributeGroupId) { $attributeGroup = new AttributeGroup(); $attributeGroup->is_color_group = false; $attributeGroup->group_type = 'select'; $attributeGroup->name = [$this->context->language->id => $attributeGroupName]; $attributeGroup->public_name = [$this->context->language->id => $attributeGroupName]; if ($attributeGroup->add()) { $attributeGroupId = $attributeGroup->id; } else { echo "Error al crear el grupo de atributos '{$attributeGroupName}'.\n"; return; } } foreach ($tallas as $talla) { $combinacion = new Combination(); $combinacion->id_product = $idProducto; $combinacion->reference = $talla['talla']; $combinacion->price = $talla['precio']; $combinacion->minimal_quantity = 1; // Establecer default_on a true en la primera vuelta, false en las demás if ($firstLoop) { $combinacion->default_on = true; $firstLoop = false; // Cambiar la variable para que solo sea true en la primera vuelta } else { $combinacion->default_on = false; } // Guardar la combinación if ($combinacion->add()) { // Ensure the attribute exists $attributeName = $talla['talla']; $attributeId = Attribute::getAttributeIdByName($attributeGroupId, $attributeName); if (!$attributeId) { $attribute = new Attribute(); $attribute->id_attribute_group = $attributeGroupId; $attribute->name = [$this->context->language->id => $attributeName]; if ($attribute->add()) { $attributeId = $attribute->id; } else { echo "Error al crear el atributo '{$attributeName}'.\n"; return; } } // Asociar el atributo a la combinación $combinacion->setAttributes([$attributeId]); // Administrar stock StockAvailable::setQuantity($idProducto, $combinacion->id, $talla['cantidad']); echo "Combinación para la talla {$talla['talla']} añadida exitosamente.\n"; } else { echo "Error al añadir la combinación para la talla {$talla['talla']}.\n"; } } }Ensure that the language ID is correctly set for attribute names. This example uses $this->context->language->id.
Added basic error handling to provide feedback if attribute groups or attributes cannot be created.
the function getIdByName not exists in AttributeGroup.php file and the object Attribute not exists
I used your code to modify it for add colors combinations and sizes combinations
private function addProductCombinations($idProducto, $tallasYColores) {
$firstLoop = true; // Variable para verificar la primera vuelta del foreach
// Obtener el id_lang del contexto actual
$idLang = $this->context->language->id;
// Ensure the attribute group for sizes exists
$attributeGroupNameSize = 'Size';
$attributeGroupIdSize = AttributeGroup::getIdByName($attributeGroupNameSize);
if (!$attributeGroupIdSize) {
$attributeGroupSize = new AttributeGroup();
$attributeGroupSize->is_color_group = false;
$attributeGroupSize->group_type = 'select';
$attributeGroupSize->name = [$idLang => $attributeGroupNameSize];
$attributeGroupSize->public_name = [$idLang => $attributeGroupNameSize];
if ($attributeGroupSize->add()) {
$attributeGroupIdSize = $attributeGroupSize->id;
} else {
echo "Error al crear el grupo de atributos '{$attributeGroupNameSize}'.\n";
return;
}
}
// Ensure the attribute group for colors exists
$attributeGroupNameColor = 'Color';
$attributeGroupIdColor = AttributeGroup::getIdByName($attributeGroupNameColor);
if (!$attributeGroupIdColor) {
$attributeGroupColor = new AttributeGroup();
$attributeGroupColor->is_color_group = true;
$attributeGroupColor->group_type = 'color';
$attributeGroupColor->name = [$idLang => $attributeGroupNameColor];
$attributeGroupColor->public_name = [$idLang => $attributeGroupNameColor];
if ($attributeGroupColor->add()) {
$attributeGroupIdColor = $attributeGroupColor->id;
} else {
echo "Error al crear el grupo de atributos '{$attributeGroupNameColor}'.\n";
return;
}
}
foreach ($tallasYColores as $item) {
$combinacion = new Combination();
$combinacion->id_product = $idProducto;
$combinacion->reference = $item['talla'] . '-' . $item['color'];
$combinacion->price = $item['precio'];
$combinacion->minimal_quantity = 1;
// Establecer default_on a true en la primera vuelta, false en las demás
if ($firstLoop) {
$combinacion->default_on = true;
$firstLoop = false; // Cambiar la variable para que solo sea true en la primera vuelta
} else {
$combinacion->default_on = false;
}
// Guardar la combinación
if ($combinacion->add()) {
// Ensure the size attribute exists
$attributeNameSize = $item['talla'];
$attributeIdSize = Attribute::getAttributeIdByName($attributeGroupIdSize, $attributeNameSize);
if (!$attributeIdSize) {
$attributeSize = new Attribute();
$attributeSize->id_attribute_group = $attributeGroupIdSize;
$attributeSize->name = [$idLang => $attributeNameSize];
if ($attributeSize->add()) {
$attributeIdSize = $attributeSize->id;
} else {
echo "Error al crear el atributo '{$attributeNameSize}'.\n";
return;
}
}
// Ensure the color attribute exists
$attributeNameColor = $item['color'];
$attributeIdColor = Attribute::getAttributeIdByName($attributeGroupIdColor, $attributeNameColor);
if (!$attributeIdColor) {
$attributeColor = new Attribute();
$attributeColor->id_attribute_group = $attributeGroupIdColor;
$attributeColor->name = [$idLang => $attributeNameColor];
if ($attributeColor->add()) {
$attributeIdColor = $attributeColor->id;
} else {
echo "Error al crear el atributo '{$attributeNameColor}'.\n";
return;
}
}
// Asociar los atributos (talla y color) a la combinación
$combinacion->setAttributes([$attributeIdSize, $attributeIdColor]);
// Administrar stock
StockAvailable::setQuantity($idProducto, $combinacion->id, $item['cantidad']);
echo "Combinación para la talla {$item['talla']} y color {$item['color']} añadida exitosamente.\n";
} else {
echo "Error al añadir la combinación para la talla {$item['talla']} y color {$item['color']}.\n";
}
}
}