Developer forum

Forum » Development » AddToFavorite and api

AddToFavorite and api

Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Hi,

I'm looking for some examples of how to use the AddToFavorite with the api.
I need to save a product to a certain list using the following code:

foreach (CustomerProductList customerProductList in customerProductLists)
{
                            Product product = new Product();
                            product = new ProductService().GetProductByNumber(productNo, true);

                            CustomerProductList.AddToFavorites(product.Id, product.VariantId, product.LanguageId, customerProductList.ID);
                            CustomerProductList.ClearCache(thisUser.ID);
}

When I look into the database, the table EcomCustomerFavoriteProducts is empty, even though I don't get any errors.

Any suggestions?

--
Best regards
Kurt Moskjaer Andersen


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Hi Kurt,

I think your code should work (depending on the context).

Make sure that you have created at least one customer product list for the user - otherwise you will never enter the foreach loop.

If you want to add a product to a list that belongs to a user who is not the currently logged in user then you should use the AddToFavorites method which takes a userId as argument.

Best regards,
Morten

Votes for this answer: 1
 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Hi Morten,

I have already imported the lists themselves, so just need to fill them with products.

Well, guess I have to look at it again :)

Thanks.

/Kurt

 
Kurt Moskjær Andersen
Kurt Moskjær Andersen
Reply

Hi again Morten,

I even tried using the following command too, through the API, but it doesn't work either:

CustomerProductList.AddToFavorites(product.Id, product.VariantId, product.LanguageId, customerProductList.Type, customerProductList.Description, customerProductList.ID.ToString(), adminUser.ID);

I'm using DW 9.5.2

/Kurt

 

You must be logged in to post in the forum