Bukimedia / PrestaSharp

CSharp .Net client library for the PrestaShop API via web service
GNU General Public License v3.0
154 stars 152 forks source link

How to add a product attribute #361

Open JM63 opened 5 years ago

JM63 commented 5 years ago

How to add product attribute with prestasharp, i have searched but can not find how to do it. One tip please ...

mowcixo commented 5 years ago

Hello @JM63, what do you mean with attribute? A product option? A product feature? If you're not sure just post an image of what you want in your control panel please.

JM63 commented 5 years ago

Thanks for the quick reply, i've uploaded an image to check ..

Capturar

mowcixo commented 5 years ago

Perfect, those are product_options, so you need to do:

var optionFactory = new ProductOptionFactory("api_url", "api_token", null);
var optionValueFactory = new ProductOptionValueFactory("api_url", "api_token", null);

var productOption = new product_option { id = 1, group_type = "select" };
productOption.name.Add(new language(1, "option name"));
productOption.public_name.Add(new language(1, "Options"));

var optionValues = new List<product_option_value>();

var optionValue = new product_option_value();
optionValue.name.Add(new language(1, feature.Title));
optionValue.id_attribute_group = productOption.id;

optionValue = optionValueFactory.AddList(optionValue);

productOption.associations.product_option_values.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value { id = productOptionValue.id.Value });

return optionFactory.Add(productOption);

Note that:

Let me know if that works for you :)

JM63 commented 5 years ago

Thanks in advance, it was a big help, I'm going to test it now.

mowcixo commented 5 years ago

Hello @JM63 you need more help about that?

JM63 commented 5 years ago

With this code I can enter the options of the product and its values, but the product remains as a simple product and not as a combined product and without the inserted options. What am I doing wrong?

var poFactory = new ProductOptionFactory(_prestashopConfig.baseurl, _prestashopConfig.account, _prestashopConfig.password);
var povFactory = new ProductOptionValueFactory(_prestashopConfig.baseurl, _prestashopConfig.account, _prestashopConfig.password);

var productOption = new product_option
{
    id = 1,
    group_type = "select",
    is_color_group = 0
};
productOption.name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language(1, "Size"));
productOption.public_name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language(1, "Size"));
productOption = poFactory.Add(productOption);

var optionValues = new List<product_option_value>();

var pov = new product_option_value();
pov.id_attribute_group = productOption.id;
pov.name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language(1, "Nano"));
// To get pov.id
pov = povFactory.Add(pov);

var pov1 = new product_option_value();
pov1.id_attribute_group = productOption.id;
pov1.name.Add(new Bukimedia.PrestaSharp.Entities.AuxEntities.language(1, "TeraLarge"));
// To get pov1.id
pov1 = povFactory.Add(pov1);

optionValues.Add(pov);
optionValues.Add(pov1);

productOption.associations.product_option_values.Add(
    new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value
    {
        id = pov.id.Value
    });
productOption.associations.product_option_values.Add(
    new Bukimedia.PrestaSharp.Entities.AuxEntities.product_option_value
    {
        id = pov1.id.Value,
    });

var prod = new Bukimedia.PrestaSharp.Entities.product();
prod.associations.product_option_values
mowcixo commented 5 years ago

You need to create the combinations separately, for any of your values, for example:

var combinationFactory = new CombinationFactory(_prestashopConfig.baseurl, _prestashopConfig.account, _prestashopConfig.password);
var combination = new combination { id_product = prod.id, minimal_quantity = 0 };
combination.associations.product_option_values.Add(pov);
combinationFactory.Add(combination);

The combination entity has multiple values, as weight, reference, etc... You should take a look to https://github.com/Bukimedia/PrestaSharp/blob/master/PrestaSharp/Entities/combination.cs to see what you need to fill, what I wrote is a minimal example.

Try this and let me know :)

mowcixo commented 5 years ago

Hello @JM63 did that work for you?

tom75 commented 5 years ago

@mowcixo - as i am interested in the same topic, i am just adding my questions here.

i would need to create combinations for specific products and also update the quantity of these combinations regularely, but, for a multilanguage shop. so, basically my questions are then the following ones:

i know, the questions are a bit overlapping, but, maybe you could let me know more detailed samples which are including also the multilanguage part, that would help a lot!

thanks in advance!