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.
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
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).
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
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.
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".
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;
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
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
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.
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!
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).
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;
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
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
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.
is working on a reply...