Developer forum

Forum » Development » Create new cart and populate with orderlines

Create new cart and populate with orderlines

Dmitrij Jazel
Reply

Hello guys,

User has no cart asigned, and if he clicks button I want to initiate new cart and add an orderline to it in the code.

Last time I did it was a while ago, now we are in DW 8.5 and things surely changed in that part of API. Was surised this didn't work :-S

this is what I have so far.

var prod = Dynamicweb.eCommerce.Products.Product.GetProductByID(prodID);
            try
            {
                if (prod != null)
                {
                    var newCart = new Order();
                    newCart.IsCart = true;
                    newCart.LanguageID = "LANG1";
                    newCart.ShopID = "SHOP1";

                    OrderLine newLine = new OrderLine();
                    newLine.SetProductInformation(prod);
                    newLine.Quantity = 1;
                    newCart.OrderLines.Add(newLine);
                    newCart.Save();
                }
                else
                {
                    logger.addEntry("Prod Is null");
                }
            }
            catch (Exception exc)
            {
                logger.addEntry(exc.ToString());
            }

 

ProdID exists, no extra extensebility code is added to this default fresh application 8.5.0.1

Getting a nullpointer exception when calling newCart.Save();

Exceptiontext:

System.NullReferenceException: Objektreferanse er ikke satt til en objektforekomst. ved Dynamicweb.eCommerce.Orders.OrderLine.get_Price() ved Dynamicweb.eCommerce.Orders.OrderLineCollection.get_Price() ved Dynamicweb.eCommerce.Orders.Order.get_PriceBeforeFees() ved Dynamicweb.eCommerce.Orders.Order.FindPaymentFee(FeeCollection FeeCollection) ved Dynamicweb.eCommerce.Orders.Order.get_PaymentFee() ved Dynamicweb.eCommerce.Orders.Order.SaveOrder(String IDStr, Boolean saveOldVersion) ved Dynamicweb.eCommerce.Orders.Order.Save(String IDStr, Boolean saveOldVersion) ved Dynamicweb.eCommerce.Orders.Order.Save() ved CustomProject.btn_addProd_Click(Object sender, EventArgs e) i c:\Projects\CustomProject\myFile.cs:linje 37 (sorry for norwegian error) :-)

As far as I tryed to see what's the issue, there is something wrong with orderline. I did try doing something like this:

newLine.SetUnitPrice(prod.Price, true);

But it still does not help :-/

 

Anything I should do differently here?

 

Kind regards,

Dmitrij

 


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Dmitrij,

Instead of creating the orderline yourself, I would recommend you to use either one of the overloads for Order.CreateOrderLine or CartCatch.OrderLineBuilder to create the orderline for you.

- Jeppe

 
Dmitrij Jazel
Reply

Hej Jeppe,

Thanks for that, well null pointer is gone :)

And I have this so far:

var newCart = new Order();
newCart.IsCart = true;
newCart.LanguageID = "LANG1";
newCart.ShopID = "SHOP1";


newCart.CreateOrderLine(prod, 1);
newCart.Save();

but the product is not added to the cart :-/ cart is still empty. And I am getting no error messages.

 

 
Dmitrij Jazel
Reply

Ok and now I also got something here, if I login with extranet user.

And I invoke the same code I am using so far (my previous post). I do get this exception here, I haven't seen that one before.

System.InvalidOperationException: Denne SqlTransaction er fullført og kan ikke brukes mer. ved System.Data.SqlClient.SqlTransaction.ZombieCheck() ved System.Data.SqlClient.SqlTransaction.Commit() ved Dynamicweb.DataManager.Dispose(Boolean disposeManagedObjects) ved Dynamicweb.DataManager.Dispose() ved Dynamicweb.eCommerce.Orders.OrderLineCollection.Save(String OrderID) ved Dynamicweb.eCommerce.Orders.Order.SaveOrder(String IDStr, Boolean saveOldVersion) ved Dynamicweb.eCommerce.Orders.Order.Save(String IDStr, Boolean saveOldVersion) ved Dynamicweb.eCommerce.Orders.Order.Save() ved CustomProject.myFile.btn_addProd_Click(Object sender, EventArgs e) i c:\Projects\CustomProject\myFile.ascx.cs

 

if I extract it from try-catch, I am getting this server error:

Denne SqlTransaction er fullført og kan ikke brukes mer.

Beskrivelse: Det oppstod et ubehandlet unntak under kjøring av gjeldende webforespørsel. Gå gjennom stakksporingen hvis du vil ha mer informasjon om feilen og hvor den oppstod i koden. 

Unntaksdetaljer: System.InvalidOperationException: Denne SqlTransaction er fullført og kan ikke brukes mer.

http://screencast.com/t/RSrOs7KIn 

Hope this helps.

Dmitrij

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

I have just tested this code on 8.5.0.1 and it works as expected.

[Subscribe(Dynamicweb.Notifications.Standard.Page.Loaded)]
public class Subscriber : NotificationSubscriber
{
	public override void OnNotify(string notification, NotificationArgs args)
	{
		if (Context.Cart != null)
			return;

		var prod = Product.GetProductByID("PROD1");
		if (prod == null)
			return;

		var cart = new Order {IsCart = true, LanguageID = Context.LanguageID};
		var ol = cart.CreateOrderLine(prod);

		Context.SetCart(cart);
	}
}

Where is your code being executed from? Maybe it's too early or too late in the flow?

Votes for this answer: 1
 
Dmitrij Jazel
Reply

Allright, I see that you are not calling "cart.Save();" instead using you are using Context.SetCart(cart);

In my case, I do not have access to Context object, you are getting that one from NotificationArgs args object.

I am executing this code out from user control. it is because this code is a part of other project I am working on that includes some sync with other NAV etc...

As a result I am getting a list of product IDs that I must fill the cart with. And set it to current user.

 

Usually I am able to access whatever Dynamicweb API object in this way by simply referencing Dynamicweb namespace.

The difference is that don't have access to Context (at the moment).

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Well, you still need to fully qualify the type. In my example, I added a couple of using statements:

using Dynamicweb.eCommerce.Common;
using Dynamicweb.eCommerce.Orders;
using Dynamicweb.eCommerce.Products;

The important one here is Dynamicweb.eCommerce.Common. Visual Studio should be able to help you if you type in a correctly cased type that exists outside your current scope but inside your current App Domain, by simply pressing "Ctrl+,".

 
Dmitrij Jazel
Reply

OK so I managed to get to through, and yes Namespaces did the trick.

Dynamicweb.eCommerce.Common.Context.* 

Now it saves a product in the cart :), the only issue is still - cause it is UserControl it did the call to DW api, but I must refresh a page again - than I can see the product in the cart.

So far my code is:

string prodID = "PROD5";
var prod = Product.GetProductByID(prodID);
if (prod != null)
{
    var cart = new Order { IsCart = true, LanguageID = Dynamicweb.eCommerce.Common.Context.LanguageID };
    var ol = cart.CreateOrderLine(prod); // adds nice orderline
    Dynamicweb.eCommerce.Common.Context.SetCart(cart); // alternative to cart.Save();
}
else
{
    logger.log("prod is null");
}

Now, I will try and see if that is enough for my other part of API code, and I think it should be, but in case I run into more issues I will let you know :-)

Appreciate allot your help! :-)

 

Regards,

Dmitrij

 
Dmitrij Jazel
Reply

Hello Jeppe,

One more question,

 

On this solution, we are using Custom Price Provider.

And when we fill in new cart with new procuducts. The cart Orderlines does not seem to update.

After we update cart, we are trying to call cart.ForcePriceRecalculation();

But this does not seem to solve the trick, I hope that is correct methid to call. But would be great to know what exactly you must do in order to update prices correctly.

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Try calling OrderLine.ClearCachedPrices().

 

You must be logged in to post in the forum