Developer forum

Forum » Development » Add Orderline programatically - Exception at get_RewardPoints()

Add Orderline programatically - Exception at get_RewardPoints()

Jonas Mersholm
Reply

Using the following code in an ASHX handler:

            var line = new OrderLine();
            line.Product = null;
            line.ProductName = "Modul test.";
            line.ProductNumber = "0001";
            line.ProductID = "0";
            line.SetUnitPrice(100);
            
            line.SetOrderLineType(OrderLine.OrderLineType.Fixed);
            line.Quantity = 1;
            line.ProductVariantText = "Beskrivelse.";
            
            

 

            var Cart = Context.Cart ?? new Order();

            Cart.OrderLines.Add(line);
            Cart.ClearCachedPrices();

            Context.SetCart(Cart);

 

Results in the following error:

Stack Trace: 
 

[NullReferenceException: Object reference not set to an instance of an object.]
   Dynamicweb.eCommerce.Orders.OrderLine.get_RewardPoints() +19
   Dynamicweb.eCommerce.Orders.Order.get_RewardTotalPoints() +100
   Dynamicweb.eCommerce.Orders.Order.SaveOrder(String IDStr, Boolean saveOldVersion) +6381
   Dynamicweb.eCommerce.Orders.Order.Save(String IDStr, Boolean saveOldVersion) +49
   Dynamicweb.eCommerce.Frontend.Cart.CartCatch.SaveCart() +42
   Dynamicweb.eCommerce.Common.Context.SetCart(Order cart) +273
   BasketController.basket.ProcessRequest(HttpContext context) +562
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +341
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

 

IRequiresSessionState is implemented. It seems like it tries to calculate a reward, which doesent exist - or whatever. 

Any ideas?


Replies

 
Viktor
Reply

Hi,

I've tried to reproduce the problem, but get exception on the start of your code:

System.NullReferenceException: Object reference not set to an instance of an object. at Dynamicweb.eCommerce.Orders.OrderLine.set_Product(Product Value) in E:\dynamicweb.net\Dynamicweb\Dynamicweb\eCommerce\Orders\OrderLine.vb:line 219 at Dynamicweb.eCommerce.Cart.CheckoutHandlers.QuickPayPaymentWindow.StartCheckout(Order order) in e:\dynamicweb.net\Dynamicweb\Dynamicweb.eCommerce.Cart.CheckoutHandlers\QuickPayPaymentWindow.cs:line 184 at Dynamicweb.eCommerce.Cart.Frontend.RenderCheckoutStep(Order order, Int32 stepIndex) in E:\dynamicweb.net\Dynamicweb\Dynamicweb\eCommerce\Cart\Frontend.vb:line 1246 at Dynamicweb.eCommerce.Cart.Frontend.GetContent() in E:\dynamicweb.net\Dynamicweb\Dynamicweb\eCommerce\Cart\Frontend.vb:line 246

That exception happened on setting orderline.Product equal to NULL, I have no idea how you dodged this.

I actually get same situation that you have but only when Context.Cart is present and contains orderlines with type product and if those orderline has not related to order I.e. orderLine.Order == null. I am not sure that it is a bug, if yes we will fix it.

Anyway you can escape the exception by simple refering to the order on creating of orderlines:

 


                var Cart = Context.Cart ?? new Order();        

                var line = new OrderLine();
                line.Product = null;
                line.ProductName = "Modul test.";
                line.ProductNumber = "0001";
                line.ProductID = "0";
                line.SetUnitPrice(100);
                
                line.SetOrderLineType(OrderLine.OrderLineType.Fixed);
                line.Quantity = 1;
                line.ProductVariantText = "Beskrivelse.";

                line.Order = Cart ;

                Cart.OrderLines.Add(line);
                Cart.ClearCachedPrices();

                Context.SetCart(Cart);


Best regards,Viktor Letavin
DynamicWEB Software A/S, Russian team

 
Jonas Mersholm
Reply

Hi Viktor,

Thank you. I got over the exception now.

 

Another problem arises though.

When executing the code, and looking into my cart page, the following text/error is displayed:

Parameter 'idList' contains no IDs

 

 

 
Nicolai Høeg Pedersen
Reply

Hi Jonas

Do you have a stack trace?

NP

 
Jonas Mersholm
Reply

Hi Nicolai.

No stack-trace. It's not a a .net exception/fault. It simply prints out "Parameter 'idList' contains no IDs" in raw-text.

You can have a look at the attached image.

 

Udklip.JPG
 
Nicolai Høeg Pedersen
Reply

Hi Jonas

I think that is because the orderline product is null. The exception comes from the Product object. The cart renderer tries to render a product instance, and since it is not there, it tries to instantiate it based on the id.

 
Jonas Mersholm
Reply

Hi Nicolai,

I tried just deleting the product line, and therefor not setting it. Same problem.

Cant i add a custom order-line, without attaching a product? 

 

Best regards.

 
Nicolai Høeg Pedersen
Reply

Not setting it is the same thing as setting it to null.

You set productid to "0" - set it to string.empty or null as well. Then it will not try to load a product of ID "0".

Another possibility is to set the product property = new product()

NP

 
Jonas Mersholm
Reply

Still the same error with the following code

 

            var line = new OrderLine();
            line.Product = new Dynamicweb.eCommerce.Products.Product();
            line.ProductName = "Modul test.";
            line.ProductNumber = "diplom0001";
            line.SetUnitPrice(100);
            line.SetOrderLineType(OrderLine.OrderLineType.Fixed);
            line.Quantity = 1;
            line.ProductVariantText = "Beskrivelse.";
            line.Order = Cart ;
            
            Cart.OrderLines.Add(line);
            Cart.ClearCachedPrices();
            Context.SetCart(Cart);

 

Altså tried setting productID to string.empty as suggested. Same problem still.

 

Thanks.

 

Jonas

 
Nicolai Høeg Pedersen
Reply

Hi Jonas.

Not sure what to do - I've asked a developer to take a look at this and get back to you if it is possible.

 
Jonas Mersholm
Reply

Hi Nicolai,

Have you/The developer, found anything exciting? 

 

Best.

Jonas

 
Nicolai Høeg Pedersen
Reply

no news yet...

 
Nicolai Høeg Pedersen
Reply

Hi Jonas

The  empty product you add to the orderline does not have a stock level - it is 0 when you do like this. Then the product is removed from the cart (if that is your settings).

So try adding stock to the empty product.

That said, I'm still not sure it is possible though.

BR Nicolai

 
Jonas Mersholm
Reply

Hi Nicolai,

Still the same "Parameter 'idList' contains no IDs".

Kinda weird not being able to add a custom, pure-text order-line to the cart.

 
Jonas Mersholm
Reply

Right. This is driving me nuts haha!

Tried adding the ProductID of an existing product.

line.ProductID = "PROD75346";
            line.ProductName = "Modul test.";
            line.SetUnitPrice(100);
            line.SetOrderLineType(OrderLine.OrderLineType.Fixed);
            line.Quantity = 1;
            line.ProductVariantText = "Beskrivelse.";
            line.Order = Cart ;

Now. The Cart displays the product correctly - BUT - The values ( ProductName, UnitPrice, Quantity, VariantText ) Has no, whatsoever, influence on the cart item. It still displays the eCom products price and informations..

Any help appreciated.

Jonas

 
Kristian Kirkholt
Reply

Hi Jonas

The problem #21009 Exception on seting context cart with product orderline has now been fixed in version 8.8.0.0

You are able to find this build in the download section:

http://developer.dynamicweb-cms.com/downloads/dynamicweb-8.aspx

Please contact Dynamicweb Support if you need any additional help regarding this.

Kind Regards
Dynamicweb Support
Kristian Kirkholt

 
Jonas Mersholm
Reply

Hi Kristian.

I wonder why stuff like this, is not released in hot-fixes for the stable release of the DW software? 
I mean. Working with smaller customers, updating to a new version (even an unstable/not recommended) one, is mostly okay. But when working with very large solutions, hosted on-site, with 50 people working daily on updating and editing content- simply pressing the "update"- button, into a non stable/recommended version is just not a possibility.

Thanks.

 
Nicolai Høeg Pedersen
Reply

Hi Jonas

We try not to merge more than needed. Otherwise there would be no difference between 8.7 and 8.8, and we do not want that. This was a fix so you could do something that DW was not designed to do. If you cannot upgrade to 8.8 (which is very stable by the way), you can simply request to get i merged down. This fix is small and simple so in this case it is not a big problem.

But we would like to keep merges to a minimum (to reduce risk) - so if you are on a new project you should upgrade.

Let me know which version you want this to be merged to.

NP

 
Jonas Mersholm
Reply

Hi Nicolai, makes sense :)

Our client is currently operation on 8.5.1.25. I will have a talk with them about upgrading to the current stable, but it's impossible to get them up onto 8,8, until it is listed as "recommended" on the webpage hehe! Also, it's a very large website with lots of functionality.

Thanks

 
Nicolai Høeg Pedersen
Reply

Ok. Let us know when you need a merge and to which version.

BR Nicolai

 
Nicolai Høeg Pedersen
Reply

This has now been merged to 8.7.2 and can be found n 8.7.2.6

http://developer.dynamicweb.com/releases/dynamicweb-8-7-2/hot-fixes-8-7-2.aspx

http://developer.dynamicweb.com/downloads/dynamicweb-8.aspx

BR Nicolai

 
Jonas Mersholm
Reply

Hi Nicolai,

With the new update, and the following code:

            var line = new OrderLine();
            line.ProductID = "PROD75346";
            line.ProductName = "Modul test.";
            line.AllowOverridePrices = true;
            line.SetUnitPrice(100);
            line.SetOrderLineType(OrderLine.OrderLineType.Fixed);
            line.Quantity = 1;
            line.ProductVariantText = "Beskrivelse.";
            line.Order = Cart ;
            Cart.OrderLines.Add(line);
            Cart.ClearCachedPrices();
            Cart.ForcePriceRecalculation();
            Cart.Save();
            Context.SetCart(Cart);

The product is actually added to the cart, but the price is not overwritten.

The same goes for a productID of "string.Empty", the product is simply not added to the cart.

Same goes with a "new" product, with a stocklevel set, the line is not added to the cart, unless i persist the product to the database.

Best.

Jonas

 
Nicolai Høeg Pedersen
Reply

Hm.

Just be sure. You want to add a product that does not exist in databae to the cart...?

I'll have to check out if that can be done - and it might need a lot of changes if it does not already work.

 
Jonas Mersholm
Reply

Hi Nicolai,

I dont neccesarily want to add a product, just an orderline with a text and a price.

The scenario we have to work on, is very complex, and does not work well with the strict grouping in the eCom, therefor i would like to make Item-Types as products, and then through a small script, add those to the cart. Either as a fake product, or as a simple "textual" orderline.

 

Best Jonas

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Jonas,

CartV2 is programmed to automatically remove products that don't exist (any longer). This is by design. You can modify which orderlines are removed by implementing a NotificationSubscriber that listens for RemoveNonExistingProducts. You can then remove the line you just added.

I've modified your code a little bit and implemented simple logic to not have my orderline removed.

using Dynamicweb.eCommerce.Common;
using Dynamicweb.eCommerce.Orders;
using Dynamicweb.Extensibility;

namespace EmptyProductOrderline
{
    [Subscribe(Dynamicweb.Notifications.Standard.Page.Loaded)]
    public class AddEmptyProdutSubscriber : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            var cart = Context.Cart ?? new Order { IsCart = true };

            var line = new OrderLine();
            line.ProductID = "DUMMY666";
            line.ProductName = "Modul test";
            line.SetUnitPrice(100);
            line.Quantity = 1;
            line.Order = cart;
            cart.OrderLines.Add(line);
            cart.ClearCachedPrices();
            cart.ForcePriceRecalculation();
            cart.Save();

            Context.SetCart(cart);
        }
    }

    [Subscribe(Dynamicweb.Notifications.eCommerce.Cart.BeforeRemovingNonExistingProducts)]
    public class RemoveNonExistingProductsSubscriber : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            var a = args as Dynamicweb.Notifications.eCommerce.Cart.BeforeRemovingNonExistingProductsArgs;
            a.OrderLinesToRemove.Clear();
        }
    }
}

Hope this helps :)

- Jeppe

 

You must be logged in to post in the forum