PrestaSharp icon indicating copy to clipboard operation
PrestaSharp copied to clipboard

How to add a product attribute

Open JM63 opened this issue 5 years ago • 9 comments

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

JM63 avatar Jun 26 '19 11:06 JM63

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.

mowcixo avatar Jun 26 '19 11:06 mowcixo

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

Capturar

JM63 avatar Jun 26 '19 11:06 JM63

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:

  • There is a bug in your PrestaShop version, that forces you to create product_option with at least one product_option_value, so you need to specify the same product_option.id and product_option_value.id_attribute_group when creating, that will be fixed in PrestaShop 1.7.6.0 (https://github.com/PrestaShop/PrestaShop/pull/12201)
  • I use 1 as language id, you need to specify yours.
  • I use "select" as group_type but there are: "select", "radio" and "color" values, you need to specify yours.

Let me know if that works for you :)

mowcixo avatar Jun 26 '19 12:06 mowcixo

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

JM63 avatar Jun 26 '19 12:06 JM63

Hello @JM63 you need more help about that?

mowcixo avatar Jul 08 '19 12:07 mowcixo

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

JM63 avatar Jul 09 '19 08:07 JM63

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 avatar Jul 09 '19 09:07 mowcixo

Hello @JM63 did that work for you?

mowcixo avatar Jul 16 '19 07:07 mowcixo

@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:

  • how to work with combinations with multiple languages in general?
  • how to write new combinations which include i.e. 5 sizes and 1 color and add quantity?
  • how to read the combinations and quantities of a product?
  • how to update the quantity of a specific products combination?

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!

tom75 avatar Jul 17 '19 10:07 tom75