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.

  • Filip Lundby 128 posts 149 karma points
    Oct 01, 2014 @ 10:02
    Filip Lundby
    0

    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?

  • Morten Skjoldager 440 posts 1499 karma points
    Oct 01, 2014 @ 11:48
    Morten Skjoldager
    0

    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

  • Filip Lundby 128 posts 149 karma points
    Oct 02, 2014 @ 12:17
    Filip Lundby
    0

    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())

    var membershipUser = Membership.GetUser();
    if (membershipUser!=null)
    {
        bool uCommerceMemberExists = Customer.Exists(c => c.MemberId == membershipUser.ProviderUserKey.ToString());
        if (!TransactionLibrary.HasBasket() && !uCommerceMemberExists)
        {
            PurchaseOrder basket = SiteContext.Current.OrderContext.GetBasket(true).PurchaseOrder;
            Customer customer = new Customer
            {
                MemberId = membershipUser.ProviderUserKey.ToString(),
            };
            basket.Customer = customer;
            basket.Save();
        }
        else
        {
            PurchaseOrder basket = PurchaseOrder.All().Where(x => x.Customer.MemberId == membershipUser.ProviderUserKey.ToString() && x.OrderStatus.OrderStatusId == 0).FirstOrDefault();
            basket.Save(); // <-- Object reference not set to an instance of an object
        }
    }
    

     

  • Filip Lundby 128 posts 149 karma points
    Oct 02, 2014 @ 13:24
    Filip Lundby
    0

    Correction to this line:

    PurchaseOrder basket =PurchaseOrder.All().Where(x => x.Customer.MemberId== membershipUser.ProviderUserKey.ToString()&& x.OrderStatus.OrderStatusId == 0).FirstOrDefault();

    Should be ("1" instead of "0"):

    PurchaseOrder basket =PurchaseOrder.All().Where(x => x.Customer.MemberId== membershipUser.ProviderUserKey.ToString()&& x.OrderStatus.OrderStatusId==1).FirstOrDefault();

    This solves the error. I then thought I would be able to retrieve the basket in the basket-template like this:

    PurchaseOrder basket =SiteContext.Current.OrderContext.GetBasket(true).PurchaseOrder;

    But this gives me the wrong basket - not the one retrieved on log in.

     

  • Filip Lundby 128 posts 149 karma points
    Oct 09, 2014 @ 10:20
    Filip Lundby
    0

    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);

     

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

    Where exactly are you getting a null references exception? Can you paste in the stack trace ? 

  • Filip Lundby 128 posts 149 karma points
    Oct 23, 2014 @ 15:43
    Filip Lundby
    100

    Got it working. Had to change:

    order.OrderStatus.OrderStatusId=1;

    to:

    order.OrderStatus = OrderStatus.Get((int)OrderStatusCode.Basket);

     

  • Morten Skjoldager 440 posts 1499 karma points
    Oct 27, 2014 @ 09:34
    Morten Skjoldager
    0

    Awsome :) 

Please Sign in or register to post replies

Write your reply to:

Draft