Developer forum

Forum » Development » Get total amount of item just bought

Get total amount of item just bought

Dmitrij Jazel
Reply
Hello DW guys :-)

I am trying using product
Dynamicweb.eCommerce.Products.Product
and I wanted to know how can you get a total amount of this product just bought (total units added to ShoppingCart).

Kind regards,
Dmitrij

Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Dmitrij

If I understand you correctly, you want to know how many of a given product is in the Shopping Cart at the time the Cart is completed?

If that's the case then you'll want to create a notification subscriber to listen for CheckoutDoneOrderIsComplete. The CheckoutDoneOrderIsCompleteArgs object contains a reference to the Order object. Iterate through the OrderLines collection on the Order and check for your Product.


If I misunderstood what you're trying to do, please let me know :)

- Jeppe
 
Dmitrij Jazel
Reply
 Hej Jeppe :-)

Yes, you understood me correctly, I actually am getting the reference to the eCom product exactly in the way how you tell me I just use:
OrderIsPassedToCheckoutHandler
This is what I eventually do:

From Event args, I get:
Dynamicweb.eCommerce.Products.ProductCollection
Where after I simply iterate through it and get the instance of:
Dynamicweb.eCommerce.Products.Product
Where I am trying to call the total amount added to the cart of the item I am currently iterating on.

Hope it is more clear now :-)

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer
I don't know where you're getting the ProductCollection from since it's not part of the OrderIsPassedToCheckoutHandlerArgs object. Are you creating it yourself?

In any event, the Product object knows nothing about the number of it that's being added to the Cart. Only the OrderLine knows that. That value is OrderLine.Quantity. That property tells you how many of the given product is on any given orderline.

In other words, something like this:
            double totalCountOfProducts = 0.0;
            foreach (var orderline in order.OrderLines)
            {
                if (orderline.Product == myProduct)
                {
                    totalCountOfProducts += orderline.Quantity;
                }
            }
Votes for this answer: 0
 
Dmitrij Jazel
Reply
Hmm, well here is the implementation of what I currently have, and it seams that I can get the collection of items through the event args.
Maybe I am doing this in a wrong way, maybe you could suggest another way to do it (I am using dynamicweb 8.0.1.3).
Since I need to save the order information and than save items that where in the order, obviously I need to save how many items the user added to the cart. But I do not like the idea of having foreach in another foreach loop. Since I would have to call it to find out how many items did the user added.

public override void OnNotify(string notification, NotificationArgs args)
        {
            //errorMessage("Method called");
            //Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsCompleteArgs myArgs = (Dynamicweb.Notifications.eCommerce.Cart.CheckoutDoneOrderIsCompleteArgs)args;
            Dynamicweb.Notifications.eCommerce.Cart.OrderIsPassedToCheckoutHandlerArgs myArgs = (Dynamicweb.Notifications.eCommerce.Cart.OrderIsPassedToCheckoutHandlerArgs)args;
            string orderID = myArgs.Order.ID;

            string orderState = myArgs.Order.StateID;
            string orderDate = myArgs.Order.Date.ToLongDateString();
            //...

            SqlConnection conn;
            SqlCommand comm;

            string connectionString = CONNSTR;
            conn = new SqlConnection(connectionString);

            string query = "INSERT INTO EXPORT_ORDERS Values (@OrderStateID, @OrderNr, @OrderDate, @OrderTime, @OrderTotalPrice, @OrderDeliveryData, @OrderStatus, @OrderUserName, @OrderAdress, @OrderSeller, @OrderOurRef, @OrderTheirRef, @OrderTheirRek, @MyOrderStatus)";

            comm = new SqlCommand(query, conn);

            int counter = 1;
            try
            {
                comm.Connection.Open();
                // just for test purposes 7 is a standart ammount passed 
                comm = new SqlCommand(query, conn);

                //OrderStateID
                comm.Parameters.AddWithValue("@OrderStateID", orderState);
				//...

                try
                {
                   //...
                }
                catch (Exception)
                {
                    errorMessage(status);
                }
                finally
                {
                }

            }
            catch
            {
                //...
                errorMessage(status);
            }
            finally
            {
                conn.Close();
                status = "counter: " + counter.ToString();
            }

			// PASSING PRODUCTS HERE
            passOrderProducts(myArgs.Order.Products);

        }

		// PRODUCTS PASSED - need to iterate through order products and gether information about them...
        private void passOrderProducts(Dynamicweb.eCommerce.Products.ProductCollection productCollection)
        {
            // SQL connection information etc...			
            try
            {
                comm.Connection.Open();
                foreach (Dynamicweb.eCommerce.Products.Product product in productCollection)
                {

                    // just for test purposes 7 is a standart ammount passed 
                    comm = new SqlCommand(query, conn);

                    //totalCount
                    comm.Parameters.AddWithValue("@totalCount", product.); // <-- need to find how many products where added in the cart here

                    // other parameters here...

                    try
                    {
                        //...
                    }
                    catch (Exception)
                    {
                       //...
                        errorMessage(status);
                    }
                    finally
                    {

                    }

                }

                try
                {
                    
                }
                catch (Exception)
                {
                    //...
                    errorMessage(status);
                }
                finally
                {

                }

            }
            catch
            {
                //...
                errorMessage(status);
            }
            finally
            {
                conn.Close();
                status = "counter: " + counter.ToString();
            }           
        }
Is that a good way or a bad way? Some suggestions would be more than appreciated ;-)
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

I would recommend that instead of passing a ProductCollection to the passOrderProducts--or at least in addition to--you send the OrderLineCollection from myArgs.Order.OrderLines and iterate through that. Mainly because you need the number of products which the ProductCollection is unable to tell you.

 
Dmitrij Jazel
Reply
It works!!! :)))
Thanks Jeppe!

 

You must be logged in to post in the forum