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.
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.
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)
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 {
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?
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?
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.
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
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 ?
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.
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 :)
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 ?
That is correct.
Your webservice would have to do something like this:
I hope this give you some inspiration
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
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:
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.
Sorry about the crazy large images BTW :)
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?
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?
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.
Thanks Morten - I will try this :)
Awsome sauce!
is working on a reply...