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.

  • Kieran Harvey 22 posts 55 karma points
    Oct 10, 2012 @ 06:27
    Kieran Harvey
    0

    Delete a Product

    hey all.

    Just wondering if any one else has had issues with deleteing products from ucommerce. I have tried 2 methods and neither seem to delete the records from the database.

    Method 1.

    //[uCommerce_CategoryProductRelation]
    ProductRelation.Delete( x => x.Product == product );
    
    //[uCommerce_PriceGroupPrice]
    PriceGroupPrice.Delete( x => x.Product == product );
    
    //[uCommerce_ProductDescription]
    ProductDescription.Find( x => x.Product == product );
    
    //[uCommerce_ProductProperty]
    ProductProperty.Delete( x => x.Product == product );
    
    //[uCommerce_ProductRelation]
    ProductRelation.Delete( x => x.RelatedProduct == product || x.Product == product );
    
    //[uCommerce_ProductReviewComment]
    foreach( var review in product.ProductReviews ) {
        ProductReviewComment.Delete( x => x.ProductReview == review );
    }
    
    //[uCommerce_ProductReview]
    ProductReview.Delete( x => x.Product == product );
    
    product.Delete();

    Method 2

    //[uCommerce_CategoryProductRelation]
    ProductRelation.Find( x => x.Product == product ).ForEach( d => d.Delete() );
    
    //[uCommerce_PriceGroupPrice]
    PriceGroupPrice.Find( x => x.Product == product ).ForEach( d => d.Delete() ); ;
    
    //[uCommerce_ProductDescription]
    ProductDescription.Find( x => x.Product == product ).ForEach( d => d.Delete() ); ;
    
    //[uCommerce_ProductProperty]
    ProductProperty.Find( x => x.Product == product ).ForEach( d => d.Delete() ); ;
    
    //[uCommerce_ProductRelation]
    ProductRelation.Find( x => x.RelatedProduct == product || x.Product == product ).ForEach( d => d.Delete() ); ;
    
    //[uCommerce_ProductReviewComment]
    foreach( var review in product.ProductReviews ) {
        ProductReviewComment.Find( x => x.ProductReview == review ).ForEach( d => d.Delete() ); ;
    }
    
    //[uCommerce_ProductReview]
    ProductReview.Find( x => x.Product == product ).ForEach( d => d.Delete() ); ;
    
    product.Delete();

     

    Going by documentation from uCommerce I should be able to just call

    Products.Delete(x=>x.ProductId == product.ProductId)

    and it will delete all related records(also showing the same error)

    When you delete an object which has relationships with other objects using the default API uCommerce will handle removing or updating the related objects, e.g. PurchaseOrder.Delete() also deletes associated order lines, customer, addresses, etc..

    However

    i am getting back 

    deleted object would be re-saved by cascade (remove deleted object from associations)

    any help would be appreciated.

  • Søren Spelling Lund 1797 posts 2786 karma points
    Oct 10, 2012 @ 08:27
    Søren Spelling Lund
    0

    Which version of uCommerce are you using? 

    Could you try this piece of code:

    var product = Product.Get(someProductId);

    product.Delete();

  • Kieran Harvey 22 posts 55 karma points
    Oct 11, 2012 @ 00:01
    Kieran Harvey
    0

    version: uCommerce 2.6.1.0

    The Product that is comming back is a legit product. It deletes all the associated properties for the product just falls over on the product itself.

  • Kieran Harvey 22 posts 55 karma points
    Oct 11, 2012 @ 03:24
    Kieran Harvey
    2

    ok so i have run this to the most basic i can get it.

    Product.Delete( x => x.ProductId == 180 );
    Product.Delete( x => x.ProductId == 181 );

    this fails on first pass. Same exception as thrown above. but if i try and delete it again than we have success. Is there any access to the context so I can Delete all Product Associations and then fire the context off to delete the Product itself.

    Solution

    As it turns out someone me needs to RTFM. I was trying to delete everything that was associated to the product as, mistakenly, I thought uCommrece wouldn't be smart enough to do that for me. The errors were coming from the context trying to delete something that I had already tried to delete. All you need to do is:

    Product.Delete( x => x.ProductId == product_Id_To_Delete );

    KJ

  • Søren Spelling Lund 1797 posts 2786 karma points
    Oct 17, 2012 @ 10:53
    Søren Spelling Lund
    1

    Beautiful isn't it :)

    To clarify: Just delete to top-most level. uCommerce will cascade the the delete to any child objects like variants, relations, etc.

    So for a structure like this:

    Product (id = 1)
      Variant (id = 20)
      Variant (id = 30)

    Just go:

    Product.Delete(x => x.ProductId == 1);

    This will delete Variants 20 and 30 too along with anything else associated with the product. 

Please Sign in or register to post replies

Write your reply to:

Draft