I've made a PricingService so that when I'm logged in prices are shown without VAT. This all works fine.
My trouble is this:
1) Not logged in - I add some products to the basket. 2) I then log in and go to the basket - products that are already added are shown with VAT. If I remove the items and re-add them, prices are correct - without VAT.
Empting the basket whenever I log in/out seems like a bad idea. Repopulating the basket on log in/out seems bad too.
You can rely on a custom property that you set on the basket whenever the user logs in the first time with his basket. That way you know that the basket relies on a user that in some point in time was logged in. So your pricing logic will be consistent if he logs in / out all the time. BTW, you should've already set the customer with the member when he logs in?
Found this article, but it seems outdated, so I'm trying to get the code up to date:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Umbraco.Core;
using UCommerce.EntitiesV2;
using UCommerce.Runtime;
using UCommerce.Catalog;
using UCommerce.Transactions;
using UCommerce.Infrastructure;
using UCommerce.Api;
namespace AdvancedBasket
{
public class CustomOrderContext : OrderContext
{
protected ICatalogContext CatalogContext { get; set; }
public CustomOrderContext(ICatalogContext catalogContext, IRepository<PurchaseOrder> orderRepository)
: base(catalogContext, orderRepository)
{
CatalogContext = catalogContext;
}
public override Basket GetBasket(bool create)
{
// Member is not logged on use default behavior
if (!HttpContext.Current.User.Identity.IsAuthenticated) return base.GetBasket(create);
// Otherwise try and load a basket for the current member, create one if it doesn't exist
Basket basket = GetBasketForCurrentMember() ?? CreateBasket();
return basket;
}
public override Basket GetBasket()
{
return GetBasket(false);
}
private Basket GetBasketForCurrentMember()
{
var member = ApplicationContext.Current.Services.MemberService.GetByUsername(HttpContext.Current.User.Identity.Name);
PurchaseOrder order = PurchaseOrder.SingleOrDefault(
x => x.OrderProperties.Where(y => y.Order.OrderId == x.OrderId && y.Key == "MemberId" && y.Value == member.Id.ToString()).Count() > 0
&& x.OrderStatus.OrderStatusId == 1);
if (order != null) return new Basket(order);
return null;
}
private Basket CreateBasket()
{
var catalogId = 0;
int.TryParse(CatalogContext.CurrentCatalog.Name, out catalogId);
var catalog = ProductCatalog.SingleOrDefault(c => c.Name.Equals(CatalogContext.CurrentCatalog.Name)) ??
ProductCatalog.SingleOrDefault(c => c.ProductCatalogId == catalogId);
if (catalog == null)
throw new InvalidOperationException("Cannot create basket when not in product catalog. Make sure that SiteContext.Current.CatalogContext.CurrentCatalog.Name contains the name of the current product catalog.");
return CreateBasket(catalog.PriceGroup.Currency);
}
private Basket CreateBasket(Currency currency)
{
var member = ApplicationContext.Current.Services.MemberService.GetByUsername(HttpContext.Current.User.Identity.Name);
var catalogGroup = ProductCatalogGroup.SingleOrDefault(g => g.Name.Equals(CatalogContext.CurrentCatalogGroup.Name));
if (catalogGroup == null)
throw new InvalidOperationException("Cannot create basket without product catalog group. Make sure that SiteContext.Current.CatalogContext.CurrentCatalogGroup.Name contains the name of the current product catalog group.");
PurchaseOrder order = new PurchaseOrder();
order.ProductCatalogGroup = catalogGroup;
order.OrderStatus.OrderStatusId = 1; // Compile-error: "The property or indexer 'UCommerce.EntitiesV2.OrderStatus.OrderStatusId' cannot be used in this context because the set accessor is inaccessible"
order.BillingCurrency = currency;
order.BasketId = Guid.NewGuid();
order.CreatedDate = DateTime.Now;
order.Save();
// Set the member id on the order so we can retrieve it later on
order["MemberId"] = member.Id.ToString();
return new Basket(order);
}
}
}
I'm not able to change the orderstatus id: "The property or indexer 'UCommerce.EntitiesV2.OrderStatus.OrderStatusId' cannot be used in this context because the set accessor is inaccessible"
I tried to add the following just before order.Save() - but then I get "Object reference not set to an instance of an object." at third line.
var newOrderStatus = OrderStatus.Get((int)OrderStatusCode.NewOrder);
var orderService = ObjectFactory.Instance.Resolve<IOrderService>();
orderService.ChangeOrderStatus(order, newOrderStatus);
Dealing with cart/basket in a b2b-b2c combo
Hi,
I've made a PricingService so that when I'm logged in prices are shown without VAT. This all works fine.
My trouble is this:
1) Not logged in - I add some products to the basket.
2) I then log in and go to the basket - products that are already added are shown with VAT. If I remove the items and re-add them, prices are correct - without VAT.
Empting the basket whenever I log in/out seems like a bad idea. Repopulating the basket on log in/out seems bad too.
What would be best practice?
You can rely on a custom property that you set on the basket whenever the user logs in the first time with his basket. That way you know that the basket relies on a user that in some point in time was logged in. So your pricing logic will be consistent if he logs in / out all the time. BTW, you should've already set the customer with the member when he logs in?
Hope that helps.
Best regards
Morten
Sounds right, but I feel like I'm missing something. What I've tried:
When the user logs in, I mark the order with the umbraco member id - if user already have a basket I'll retrieve it (error at basket.Save())
Correction to this line:
Should be ("1" instead of "0"):
This solves the error. I then thought I would be able to retrieve the basket in the basket-template like this:
But this gives me the wrong basket - not the one retrieved on log in.
Found this article, but it seems outdated, so I'm trying to get the code up to date:
I'm not able to change the orderstatus id: "The property or indexer 'UCommerce.EntitiesV2.OrderStatus.OrderStatusId' cannot be used in this context because the set accessor is inaccessible"
I tried to add the following just before order.Save() - but then I get "Object reference not set to an instance of an object." at third line.
Where exactly are you getting a null references exception? Can you paste in the stack trace ?
Got it working. Had to change:
to:
Awsome :)
is working on a reply...