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.

  • Baz 9 posts 50 karma points
    Apr 18, 2013 @ 15:05
    Baz
    0

    Adding dynamic properties via javascript

    I'm aware this can be down via the steps outlined in http://www.publicvoid.dk/DynamicOrderPropertiesAddingCustomInformationToBasketsOrdersAndOrderLines.aspx

    but can this be done via javascript?

    I'm using the ucommerce razor store which uses AJAX to AddToBasket which does a lot of stuff in the background but I don't see anywhere that I can dynamic properties which can later be retrieved once I am in the cart.

    Thanks

  • Nickolaj Lundgreen 233 posts 1132 karma points
    Apr 18, 2013 @ 15:09
    Nickolaj Lundgreen
    0

    Hi Baz

     

    You would have to make your own AddToBasket webservice (this is pretty simple), and add the option to include dynamic properties.

     

    Could you describe the problem you are trying to solve with dynamic properties ?

  • Baz 9 posts 50 karma points
    Apr 18, 2013 @ 15:29
    Baz
    0

    Hi,

     

    Thanks for replying.

    I have a variable which I need to multiply by the price.  This variable is called 'weeks'.

    I can hack the price by multiplying qty with weeks before it is sent to the webservice but I will still need to get this variable from somewhere so I can then divide the updated price by weeks.

     

    I've just thought of something this second while typing this out.  I should be able to get the weeks by dividiing the price by qty but I was hoping for something a little cleaner.  I may look in to writing my own AddToBasket webservice if this doesn't work out.

     

    Thanks a lot.

  • Nickolaj Lundgreen 233 posts 1132 karma points
    Apr 18, 2013 @ 15:36
    Nickolaj Lundgreen
    0

    I think you should go the custom webservice route. That way you can add the weeks property on the orderline, and update the price on the orderline.

    Let me know if run into trouble writing the AddToBasket webservice :)

  • Baz 9 posts 50 karma points
    Apr 18, 2013 @ 16:10
    Baz
    0

    Yeah, I think you may be correct.  The other way could pose a significant headache if the user decided to edit quantity in the cart.

    Is there any tutorial on how to re-write the AddToBasket method?

    Am I correct in saying I need to re-do this:

                addToBasket: function(options, onSuccess, onError) {

                     var defaults = {

                        quantity: 1, 

                        sku: '',

                        variantSku: '',

                        addToExistingLine: true

                    };

                    var extendedOptions = $.extend(defaults, options);

                    callServiceStack({ AddToBasket: extendedOptions }, onSuccess, onError);

                },

     

    Which exists in the uCommerce.jQuery.js ?

     

  • Nickolaj Lundgreen 233 posts 1132 karma points
    Apr 18, 2013 @ 16:30
    Nickolaj Lundgreen
    0

    That is correct.

     

    Your webservice would have to do something like this:

    //Fetch basket
    var basket = SiteContext.Current.OrderContext.GetBasket();
    var orderline = basket.PurchaseOrder.AddProduct(...) - Or use TransactionLibrary.AddToBasket( 
    //Its possible to set the unit price here, but I would create a PipelineTasket that loops the entire orderline collection, and set the correct price, based on the "weeks" property. (That way you only have to update the "weeks" property when a user update the product in the basket - the pipeline will then take care of the rest)

    I hope this give you some inspiration

  • Baz 9 posts 50 karma points
    Apr 19, 2013 @ 17:06
    Baz
    0

    Hi again,

     

    Having some trouble getting this WebMethod to work.  I have took the code from here https://bitbucket.org/uCommerce/ucommerce-razor-store/src/c8cb974bfd19/src/uCommerce.RazorStore/Services/Commands/AddToBasket.cs?at=default that I want to try and implement.  I think I should be OK with implementing the code but not sure where to start off and how to get the javascript to call this method.

    Will I need to also rewrite the Transaction.AddToBasket to include the weeks property? Or will I just add this as a dynamic property?

    I've tried to find some online resources that will help with solve this but there doesn't seem to be any.  

    Any help would be very much appreciated.

     

    Thanks,

     

    Baz

  • Søren Spelling Lund 1797 posts 2786 karma points
    May 24, 2013 @ 10:16
    Søren Spelling Lund
    0

    Hi Baz,

    You can grab the Razor Store code from Bitbucket and add additional properties in the webservice called by the existing call. If you want to supply extra data from JS I would recommend doing a new service. It's pretty simple to do in the existing Razor code. Here's a sample:

    Back-end:

    Add a new class to the Services folder in the Razor source code:

    public class AddToBasketWithDynamicOrderProperties
    {
        public int? CatalogId { get; set; }
        public int Quantity { get; set; }
        public string Sku { get; set; }
        public string VariantSku { get; set; }
        public bool AddToExistingLine { get; set; }
        public Dictionary Properties { get; set; }
    }
    
    public class AddToBasketResponse : IHasResponseStatus
    {
        public ResponseStatus ResponseStatus { get; set; }
    }
    
    public class AddToBasketWithDynamicOrderPropertiesService : ServiceBase, IUCommerceApiService
    {
       protected override object Run(AddToBasket request)
        {
            TransactionLibrary.AddToBasket(request.Quantity, request.Sku, request.VariantSku, request.AddToExistingLine, true, request.CatalogId);
    
            var basket = TransactionLibrary.GetBasket().PurchaseOrder;
    
            foreach (var key in request.Properties)
            {
                basket[key] = request.Properties[key].Value;
            }
            return new AddToBasketResponse();
        }
    } 

    In the JS API you need to support properties by modifying the /scripts/uCommerce.demostore.productpage.js file to add a new JS method:

    The method used to support adding to cart is called wireupAddToCartButton. You need to add the values you want sent to that:

    Hope this helps.

  • Søren Spelling Lund 1797 posts 2786 karma points
    May 24, 2013 @ 10:22
    Søren Spelling Lund
    0

    Sorry about the crazy large images BTW :)

  • Qaisar 31 posts 77 karma points
    Oct 13, 2014 @ 10:03
    Qaisar
    0

    Hi Søren,

    I have created a new service but I want to add properties on each order line. I am not able to get hold of the current orderline object with in this service class. Can you help?

  • Qaisar 31 posts 77 karma points
    Oct 13, 2014 @ 10:04
    Qaisar
    0

    Hi Søren,

    I have created a new service but I want to add properties on each order line. I am not able to get hold of the current orderline object with in this service class. Can you help?

  • Morten Skjoldager 440 posts 1499 karma points
    Oct 20, 2014 @ 11:34
    Morten Skjoldager
    0

    ObjectFactory.Instance.Resolve<TransactionLibraryInternal>(); will give you the orderLine when you call AddToBasket. This is the mplementation that the static api will return for you.

  • Qaisar 31 posts 77 karma points
    Oct 20, 2014 @ 11:57
    Qaisar
    0

    Thanks Morten - I will try this :)

  • Morten Skjoldager 440 posts 1499 karma points
    Oct 21, 2014 @ 14:08
    Morten Skjoldager
    0

    Awsome sauce!

Please Sign in or register to post replies

Write your reply to:

Draft