PrestaSharp
PrestaSharp copied to clipboard
Get combination (id_prodcut, id_product_option_value)
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
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.
Hello, I tried your solution but I always have an Exception : Source could not be null. How can I solve that ? Thanks,
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 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
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 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 ...
Then that means you didn't create a combination in your Prestashop store or you're not connecting correctly to it.
@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