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.

  • Lukman 9 posts 29 karma points
    Jun 25, 2014 @ 16:05
    Lukman
    0

    Add to Basket Customization

    I have a doubt, which is I am developing a site with shopping items and donation items. It should have clear separation when showing. If I added a shopping item to the cart, then I must not able to add a donation item and vice versa. How can I achieve the same?. I have fount a file named basket.cshtml and code as shown below

    public static bool? AddToBasket(string addToBasketKey, string quantityKey,UCommerce.EntitiesV2.Product variant) { var request = HttpContext.Current.Request;

        if (request.Form.AllKeys.All(x => x != addToBasketKey))
        {
            return null;
        }
    
        if (variant == null)
        {
            return false;
        }
    
        var quantity = Convert.ToInt32(request.Form[quantityKey]);
    
    
        TransactionLibrary.AddToBasket(quantity, variant.Sku, variant.VariantSku);
        TransactionLibrary.ExecuteBasketPipeline();
        var returnUrl = request.RawUrl;
    
        if (returnUrl.Contains("item-added"))
            HttpContext.Current.Response.Redirect(returnUrl);
    
        if (returnUrl.Contains("?"))
            HttpContext.Current.Response.Redirect(string.Format("{0}&item-added=true", returnUrl));
    
        HttpContext.Current.Response.Redirect(string.Format("{0}?item-added=true", returnUrl));
    
        return true;}
    

    What condition I should make inorder to check if existing products belongs to standard products or donation items. I have created a seperate catogory definition and added the donation items as donation catgory definition instead of Default Category Definition.

    Please help me.

    Thanking you

  • Nickolaj Lundgreen 233 posts 1132 karma points
    Jun 25, 2014 @ 16:53
    Nickolaj Lundgreen
    0

    Hi Lukman

    Are the donation and regular products two different product definitions ? (it's an easy way to tell them apart).

    If so you can make a check that loops the orderlines in the basket, fetches the product and check its product definition against the one you are displaying (if they match you can display an "add to basket"-button

  • Lukman 9 posts 29 karma points
    Jun 25, 2014 @ 16:57
    Lukman
    0

    Yes,

    I have created another Category Definition as "Donation Category Definition" besides Default Category Definition.

    Could you give me a example code of Checking by loop for my code which I have given above.

  • Nickolaj Lundgreen 233 posts 1132 karma points
    Jun 25, 2014 @ 17:09
    Nickolaj Lundgreen
    0

    Hi Lukman

    There are several different types of definitions (Category, Product, Campaign definition etc.).

    It sounds like you are create "category" definitions to distinguish the two product types. Normally you would do this by using product definitons!

    var product = Product.All().First();
    var basket = SiteContext.Current.OrderContext.GetBasket();
    var orderLine = basket.PurchaseOrder.OrderLines.FirstOrDefault();
     var orderLineProduct = Product.FirstOrDefault(x => x.Sku == orderLine.Sku && x.VariantSku == orderLine.VariantSku);
    if (product.ProductDefinition == orderLineProduct.ProductDefinition)
    {
        //Match : Product is allowed in the basket - show a button
    }
    else
    {
        //No Match : Product is not allowed - show a "no button " text
    }
    

    The most simple code - compares the first product in the basket with the present product (replace "Product.All().First() with "variant" ".

    OBS. this only supports two different product types (for simplicity).

  • Lukman 9 posts 29 karma points
    Jun 25, 2014 @ 17:43
    Lukman
    0

    Thank you,

    @using UCommerce.EntitiesV2 @using UCommerce.Extensions @using UCommerce.Runtime @using umbraco.MacroEngines @using UCommerce.Api @functions{ public static bool? AddToBasket(string addToBasketKey, string quantityKey, UCommerce.EntitiesV2.Product variant) { var request = HttpContext.Current.Request;

        if (request.Form.AllKeys.All(x => x != addToBasketKey))
        {
            return null;
        }
    
        if (variant == null)
        {
            return false;
        }
    
        var quantity = Convert.ToInt32(request.Form[quantityKey]);
    
    
        var product = variant;
        var basket = SiteContext.Current.OrderContext.GetBasket();
        var orderLine = basket.PurchaseOrder.OrderLines.FirstOrDefault();
        var orderLineProduct = UCommerce.EntitiesV2.Product.FirstOrDefault(x => x.Sku == orderLine.Sku && x.VariantSku == orderLine.VariantSku);
        if (product.ProductDefinition == orderLineProduct.ProductDefinition)
        {
            //Match : Product is allowed in the basket - show a button
            return false;
        }
        else
        {
            //No Match : Product is not allowed - show a "no button " text
            TransactionLibrary.AddToBasket(quantity, variant.Sku, variant.VariantSku);
            TransactionLibrary.ExecuteBasketPipeline();
            var returnUrl = request.RawUrl;
    
            if (returnUrl.Contains("item-added"))
                HttpContext.Current.Response.Redirect(returnUrl);
    
            if (returnUrl.Contains("?"))
                HttpContext.Current.Response.Redirect(string.Format("{0}&item-added=true", returnUrl));
    
            HttpContext.Current.Response.Redirect(string.Format("{0}?item-added=true", returnUrl));
            return true;
        }
    
    }
    

    Here is my code which I have given. But when I given

    var orderLineProduct = Product.FirstOrDefault(x => x.Sku == orderLine.Sku && x.VariantSku == orderLine.VariantSku);I got an error saying that FirstOrDefault() is not a part of Product. So I replaced the Line with the following

    var orderLineProduct = UCommerce.EntitiesV2.Product.FirstOrDefault(x => x.Sku == orderLine.Sku && x.VariantSku == orderLine.VariantSku);

    I need to check the basket contain any other products other than donation if I am adding Donation or vice versa. I also configured Product Definition as "Donation" and some other Definitions for some other standard products. Please give me exact solution. I will be so happy if you assist me.

    Thank you

  • Lukman 9 posts 29 karma points
    Jun 26, 2014 @ 08:26
    Lukman
    0

    Hi,

    I have tried with the above code. But I can add the donation and other standard products together. Please give any other alternative method.

    Is there any method using Category Definition, otherwise please provide solution using product definition.

    Thank you

  • Søren Spelling Lund 1797 posts 2786 karma points
    Jun 30, 2014 @ 11:14
    Søren Spelling Lund
    0

    Hi Lukman,

    The problem probably is that your server side code is never executed because there's also a bit of Javascript hooked up to the add to cart button to handle this. By default the JS will override the postback.

    You can either make your check on the client using Javascript or you can disable the Javascript method for adding to basket, which would cause your server side code to execute.

    The JS behavior is attached to the button in the file called "uCommerce.demostore.productpage.js".

    Hope this helps.

Please Sign in or register to post replies

Write your reply to:

Draft