Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


These support forums are now closed for new topics and comments.
Please head on over to http://eureka.ucommerce.net/ for support.

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Dec 11, 2012 @ 17:15
    Dan Diplo
    0

    How to delete a PriceGroupPrice via the API?

    I've been working on a simple web-service that allows a client to query and add products which utilises the uCommerce API. This has worked well so far. However, I've now got to the stage of updating a product. I can update simple fields, but not sure how to update relationships?

    For instance, the web-service allows a product to be updated, so I've made it so you can pass in a List<PriceGroupPrice> for the Product. My intention was that I would delete the existing PriceGroupPrice collection for the product and then add the new list that has been passed in (that way you don't need to work out what has changed, you just delete all existing relations and then add the new ones). So I have some code that looks a bit like this:

    var prod = Product.Get(206);
    
    foreach (var pgp in prod.PriceGroupPrices)
    {
        pgp.Delete();
    }
    
     After deleting them I was then going to add the new ones in (like you would do for a new product).
    However, when I call this I get following error:
    deleted object would be re-saved by cascade (remove deleted object from associations)

    So, what is the way around this? Am I approaching this correctly, or is there a better way of updating the prices? Thanks.

     

  • Søren Spelling Lund 1797 posts 2786 karma points
    Dec 17, 2012 @ 10:23
    Søren Spelling Lund
    0

    Hi Dan,

    In general when you want to delete an object that belongs to a, for lack of better word, "larger" object like in the product - price scenario you want to use the RemoveWhatever method instead of deleting it outright and then saving the "larger" object.

    This is what you'd use to remove a single price:

     

    var prod =Product.Get(206);

    prod.PriceGroupPrices.Remove(price); // Usually there's a RemoveInsertNameOfClass method, but in this case there isn't so just use the collection directly

    prod.Save(); // Save cascades to associated objects and saves the entire thing including deleting any removed objects

    And for your specific scenario where you want to clear all prices:

    var prod = Product.Get(206); prod.PriceGroupPrices.Clear(); // Removes all prices

    prod.Save(); 

    Hope this helps.

     

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Dec 17, 2012 @ 14:12
    Dan Diplo
    0

    Thanks Soren! I'd used the Remove method for categories but couldn't find one for PriceGroups, so assumed there was a different way. Thanks for pointing me in the right direction,

  • Søren Spelling Lund 1797 posts 2786 karma points
    Dec 21, 2012 @ 12:40
    Søren Spelling Lund
    0

    BTW I'll add the missing method in an upcoming release.

Please Sign in or register to post replies

Write your reply to:

Draft