Jump to content

[Solved] Given Combination IDs list, I need a php script to add an attribute "AAA" with value "XXX" to combinations


Hokuto ShinKen

Recommended Posts

Hi everyone,

I have a list of Combination IDs regarding combinations I need to update on an automated fashion.

I would need someone that would be able to provide me with a kind of script that @Andrei H provided me in the following post (Kudos to him :) )

 A script that would loop through the combinations IDs and add to the combinations an attribute let's call it "AAA" with a value let's call it "XXX".

I am still an infant in PHP and to not talk about Prestashop internals and objects and will not be able to do it myself.
I have some hundreds products to update and cannot contemple to do this manually if it was even possible.

Thanks a lot in advance.

PS: I tried to check the DB structure to see if I could go with an easy SQL solution for this, but really I haven' t understood how combinations IDs and attributes are linked here.

Best regards to Everyone.

 

Edited by Hokuto ShinKen (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Hello,

The below script should do the work.

Please test it in a staging environment first. Or with some test combinations.

You will need to provide the combination ids in that array and the id of the new attribute that will be added to them. This is the id of the attribute and not the id of the attribute group. You can get it by going to Attributes and Features, and select the desired attribute group (e.g. Size), then you will have a list of all the size attributes - pick the one you want.

As per usual, for running the script, create a file called script.php (or any other name you want), copy the below code inside it, modify it to fulfill your needs and access <website_url>/script.php

<?php

require dirname(__FILE__) . '/config/config.inc.php';

$combinationIds = [44, 45, 46, 47];
$newAttributeId = 6;

foreach ($combinationIds as $id) {
    $combination = new Combination($id);

    $attributeIds = [];
    $existingAttributes = $combination->getAttributesName(1);

    foreach ($existingAttributes as $attribute) {
        $attributeIds[] = $attribute['id_attribute'];
    }

    if (in_array($newAttributeId, $attributeIds)) {
        continue;
    }

    $attributeIds[] = $newAttributeId;

    $combination->setAttributes($attributeIds);
    $combination->save();
}

echo 'All set!';

Let me know if it works or if you have any issues with it.
 

Edited by Andrei H (see edit history)
Link to comment
Share on other sites

  • 2 months later...
  On 11/24/2024 at 8:48 AM, Andrei H said:

Hello,

The below script should do the work.

Please test it in a staging environment first. Or with some test combinations.

You will need to provide the combination ids in that array and the id of the new attribute that will be added to them. This is the id of the attribute and not the id of the attribute group. You can get it by going to Attributes and Features, and select the desired attribute group (e.g. Size), then you will have a list of all the size attributes - pick the one you want.

As per usual, for running the script, create a file called script.php (or any other name you want), copy the below code inside it, modify it to fulfill your needs and access <website_url>/script.php

<?php

require dirname(__FILE__) . '/config/config.inc.php';

$combinationIds = [44, 45, 46, 47];
$newAttributeId = 6;

foreach ($combinationIds as $id) {
    $combination = new Combination($id);

    $attributeIds = [];
    $existingAttributes = $combination->getAttributesName(1);

    foreach ($existingAttributes as $attribute) {
        $attributeIds[] = $attribute['id_attribute'];
    }

    if (in_array($newAttributeId, $attributeIds)) {
        continue;
    }

    $attributeIds[] = $newAttributeId;

    $combination->setAttributes($attributeIds);
    $combination->save();
}

echo 'All set!';

Let me know if it works or if you have any issues with it.
 

Expand  

Hi @Andrei H,

Thanks a lot for your support a hundred times again.
I am sorry to reply to late as I missed the notification.

I have been able to achieve the same using another script I got from using Claude AI.

But I will keep yours since it is much shorter and clean :)

Thanks, I will mark this as fixed.

Have a nice evening.

Link to comment
Share on other sites

  • Hokuto ShinKen changed the title to [Solved] Given Combination IDs list, I need a php script to add an attribute "AAA" with value "XXX" to combinations

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...