0

I'm successfully import from a CSV file a massive number of SIMPLE products. Than I created the CONFIGURABLE products and assigned the simples by

$configurableproduct_load->setCanSaveConfigurableAttributes(true);
$configurableproduct_load->getTypeInstance()->setUsedProductAttributeIds(array(258,267,178), $configurableproduct_load);
$configurableAttributesData = $configurableproduct_load->getTypeInstance()->getConfigurableAttributesAsArray($configurableproduct_load);
$configurableproduct_load->setConfigurableAttributesData($configurableAttributesData);

$configurableproduct_load->setAssociatedProductIds($associatedProductIds);
$configurableproduct_load->save();

where 258, 267, 178 are the attributes id in the sort that I want. However, Magento create the CONFIGURABLE product assigned to it the configuration in the ASCENDING order: 178,258,267. This always appens, with all combinations of attribute id.

Frontend result

How I can assign my own sorting?

1
  • Of course, array(258, 267, 178), is just a sample. But I always get the issue with the given array :/ the defined order is completely ignored.
    – Samuele
    Commented Jun 3, 2023 at 19:01

2 Answers 2

0

Try these steps

1.Retrieve the configurable product attributes and their values:

$configurableAttributesData = $configurableproduct_load->getTypeInstance()->getConfigurableAttributesAsArray($configurableproduct_load);

2Modify the sorting order of the attributes in the $configurableAttributesData array based on your desired order. For example, if you want the order to be 258, 267, 178,

$newOrder = array(258, 267, 178);
usort($configurableAttributesData, function ($a, $b) use ($newOrder) {
    $posA = array_search($a['attribute_id'], $newOrder);
    $posB = array_search($b['attribute_id'], $newOrder);
    return $posA - $posB;
});

3.Set the updated configurable attributes data to the configurable product:

$configurableproduct_load->setConfigurableAttributesData($configurableAttributesData);

4.Save the configurable product to apply the changes:

 $configurableproduct_load->save();



By using the **usort** function and providing a custom comparison function, you can define your own sorting order based on the attribute IDs. The configurable attributes will be sorted according to the order specified in the **$newOrder** array.
2
  • No :/ unfortunely nothing changed. Please, note also that the original sequenze of $attributeId given to first argument (array) of setUsedProductAttributeIds was as wished, but never assigned as aspected :/
    – Samuele
    Commented Jun 3, 2023 at 18:34
  • In each my random check, Magento assign the attribute in "attribute_id" sort order ascending. Never seen nothing before 2.3.2 version
    – Samuele
    Commented Jun 3, 2023 at 18:36
0

Finally I found the solution: to sort the attributes programmatically I set the key "position" of each attribute after created $configurableAttributesData

I hope this will be usefully for everyone ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.