PrestaSharp icon indicating copy to clipboard operation
PrestaSharp copied to clipboard

Get combination (id_prodcut, id_product_option_value)

Open zbroinx opened this issue 8 years ago • 8 comments

Hi, I'm trying to get a combination, based on id_product and product_option_value id. Is this possible? Can anyone help me? Thanks

zbroinx avatar Feb 17 '17 18:02 zbroinx

I like LINQ,

if you know that will only be one combination, you can do:

combination comb = CombinationsFactory.GetAll().Where(c => c.id_product == your_product.id && c.associations.product_option_values.Any(pov => pov.id == your_product_option_value.id)).First();

or else:

List<combination> listComb = CombinationsFactory.GetAll().Where(c => c.id_product == your_product.id && c.associations.product_option_values.Any(pov => pov.id == your_product_option_value.id));

I don't know if this is 100% correct, because I didn't test it. But it'll be something like this. When I have the time I'll test it. Other way is to use method GetByFilter with 2 dictionaries.

LuisDavidR avatar May 05 '17 15:05 LuisDavidR

Hello, I tried your solution but I always have an Exception : Source could not be null. How can I solve that ? Thanks,

fredoche1810 avatar Dec 06 '17 13:12 fredoche1810

Hi,

Without more info I can't know for sure what's your problem, but try to use only:

List<combination> listComb = CombinationsFactory.GetAll();

and see which combinations you have.

Try to check, for example, the first combination in your list and see its id_product and the associations.product_option_values.(product_value_id, don't remember that well), and then try to reproduce the LINQ query I gave in my older post, with that IDs.

I think your problem is one of the following (assuming you have combinations, products and product_option_values created):

  • You don't have the correct product ID on the list of combinations you're trying to get;
  • You don't have the correct association with your product_option_value;

Good luck. If you give me more info, maybe I can help you better ;)

LuisDavidR avatar Dec 06 '17 16:12 LuisDavidR

@LuisDavidR Thanks. I tried your sample code like this : List<Bukimedia.PrestaSharp.Entities.combination> Combination2 = CombinationFactory.GetAll(); MessageBox.Show(Combination2.ToString());

I receive the error on the line MessageBox 2017-12-06

fredoche1810 avatar Dec 06 '17 17:12 fredoche1810

Your Combination2 is going to be a List of combinations, it will not be possible to see its content. You have to do something like:

combination only1Combination = CombinationFactory.GetAll().First(); //Gets only the first combination
MessageBox.Show(only1Combination.id_product); //Shows you the product_id of your first combination
MessageBox.Show(only1Combination.associations.product_option_values.First().id); //Shows you the first product_value_id of your first combination

LuisDavidR avatar Dec 06 '17 17:12 LuisDavidR

@LuisDavidR That's the problem ... I don't receive a list of combination. On the line only1Combination = CombinationFactory.GetAll().First(); //Gets only the first combination I already receive an exception that the source could not be null ...

fredoche1810 avatar Dec 07 '17 07:12 fredoche1810

Then that means you didn't create a combination in your Prestashop store or you're not connecting correctly to it.

LuisDavidR avatar Dec 07 '17 10:12 LuisDavidR

@LuisDavidR No that's not the problem. I finally found the problem. I was working on an older version of Prestasharp and there was a bug. I use the new version and everything works. Sorry for disturbation but thanks for your help

fredoche1810 avatar Dec 08 '17 12:12 fredoche1810