Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ivan Saric 40 posts 89 karma points
    Jan 03, 2014 @ 16:38
    Ivan Saric
    0

    Member.CurrentMemberId() System.NullReferenceException

    Hi,

    I have a problem with custom OrderContext. I'm using Umbraco 6.1.6 and I did all changes from here http://www.publicvoid.dk/ChangingTheDefaultBasketBehaviorOfUCommerce.aspx 

    Problem is in GetBasketForCurrentMember() method because umbraco.cms.businesslogic.member.Member.CurrentMemberId() throws error : System.NullReferenceException.

    My code is:

    using System;

    using System.Linq;

    using UCommerce.EntitiesV2;

    using UCommerce.Runtime;

    using umbraco.cms.businesslogic.member;

     

    namespace coffeine.custom

    {

        public class CustomOrderContext : OrderContext

        {

            protected new ICatalogContext CatalogContext { get; set; }

     

            public CustomOrderContext(ICatalogContext catalogContext)

                : base(catalogContext)

            {

                CatalogContext = catalogContext;

            }

     

            public override Basket GetBasket(bool create)

            {

                // Member is not logged on use default behavior

                if (!umbraco.cms.businesslogic.member.Member.IsLoggedOn()) 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()

            {

                int currentMemberId = umbraco.cms.businesslogic.member.Member.CurrentMemberId();

                PurchaseOrder order = PurchaseOrder.SingleOrDefault(

                    x => x.OrderProperties.Where(y => y.Order.Id == x.OrderId && y.Key == "MemberId" && y.Value == currentMemberId.ToString()).Count() > 0

                        && x.OrderStatus.Id == 1);

     

                if (order != null) return new Basket(order);

     

                return null;

            }

     

            private new 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.CurrentCatalogName contains the name of the current product catalog.");

     

                return CreateBasket(catalog.PriceGroup.Currency);

            }

     

            private new Basket CreateBasket(Currency currency)

            {

                var catalogGroup = ProductCatalogGroup.SingleOrDefault(g => g.Name.Equals(CatalogContext.CurrentCatalog.ProductCatalogGroup.Name));

     

                if (catalogGroup == null)

                    throw new InvalidOperationException("Cannot create basket without product catalog group. Make sure that SiteContext.Current.CatalogContext.CurrentCatalogGroupName contains the name of the current product catalog group.");

     

                PurchaseOrder order = new PurchaseOrder();

                order.OrderStatus = new OrderStatus((int)PurchaseOrder.StatusCode.Basket);

                order.ProductCatalogGroup = catalogGroup;

                order.BillingCurrency = currency;       //.Currency.Id = currency.CurrencyId;

                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"] = umbraco.cms.businesslogic.member.Member.CurrentMemberId().ToString();

     

                return new Basket(order);

            }

        }

    }

  • Ivan Saric 40 posts 89 karma points
    Jan 03, 2014 @ 17:05
    Ivan Saric
    0

    I forgot to write that I'm using UsersMembershipProvider:

    <add name="UsersMembershipProvider" type="umbraco.providers.UsersMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" passwordFormat="Hashed" />

  • Ivan Saric 40 posts 89 karma points
    Jan 05, 2014 @ 17:11
    Ivan Saric
    0

    I found solution, problem was in membership provider. I changed to UmbracoMembershipProvider and I got CurrentMemberId. There is another problem, method GetBasketForCurrentMember LINQ query :

    PurchaseOrder order = PurchaseOrder.SingleOrDefault(

                    x => x.OrderProperties.Where(y => y.Order.Id == x.OrderId && y.Key == "MemberId" && y.Value == currentMemberId.ToString()).Count() > 0

                        && x.OrderStatus.Id == 1);

    throws an exception: 

    could not resolve property: Id of: UCommerce.EntitiesV2.PurchaseOrder [.SingleOrDefault[UCommerce.EntitiesV2.PurchaseOrder](NHibernate.Linq.NhQueryable`1[UCommerce.EntitiesV2.PurchaseOrder], Quote((x, ) => (AndAlso(GreaterThan(.Count[UCommerce.EntitiesV2.OrderProperty](.Where[UCommerce.EntitiesV2.OrderProperty](x.OrderProperties, (y, ) => (AndAlso(AndAlso(Equal(y.Order.Id, x.OrderId), String.op_Equality(y.Key, p1)), String.op_Equality(y.Value, p2))), ), ), 0), Equal(x.OrderStatus.Id, 1)))), )]

    What is wrong with this LINQ?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies