Developer forum

Forum » Rapido » Not able to update custom field in ecommorderline in Checkout Page

Not able to update custom field in ecommorderline in Checkout Page

Tan Pang Lin Dynamicweb Employee
Tan Pang Lin
Reply

Dear Support

We had created a custom order line field in checkout page to let the customer choose the delivery date for order line item. I’ve tried a few ways but still there are some issue in different scenarios below.

For case 1 and 2 below, I have written this javascript function to call cartcmd command in custom.js file.

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

var SetDeliveryDate = function (cartOrderlinesFeedPageId, orderLineId, productId, quantity, deliveryDate, minDeliveryDate) {

 

    var deliveryDatePara = '&EcomOrderLineFieldInput_Deliverydate=' + deliveryDate;

    deliveryDate = new Date(deliveryDate);

    minDeliveryDate = new Date(minDeliveryDate);

    clearTimeout(updateDelay);

    if (deliveryDate >= minDeliveryDate) {

        updateDelay = setTimeout(function () {

            Cart.UpdateCart('Cart', '/Default.aspx?ID=' + cartOrderlinesFeedPageId,

                'CartCmd=DelOrderLine&key=' + orderLineId + '&redirect=false',

                true);

            Cart.UpdateCart('Cart',

                '/Default.aspx?ID=' + cartOrderlinesFeedPageId,

                'CartCmd=add&productid=' + productId + '&quantity=' + quantity + deliveryDatePara + '&redirect=false',

                true);

        }, 800);

    } else {

        alert("Please choose a delivery date later than " + minDeliveryDate.getDate() + "-" + (minDeliveryDate.getMonth() + 1) + "-" + minDeliveryDate.getFullYear());

    }

}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

  1. Remove the order line and recreate it with custom orderline value using “cartcmd = add”

For this case, the orderline is recreated with the correct delivery date but user didn’t accept this as the order line will move to last in order summary page (Sorting is not according to the added items).    Refer to this video to see the effect,   https://www.loom.com/share/7612b4f977764a7cbb8c0ae02220b057

 

  1. Did not remove the order line but use the “cartcmd=add” to send the custom order line value

For this case, the new order line is created (duplicate order line) with the correct delivery date at the last position in the cart. Since it create duplicate and order lines didn’t merge as custom order line field value is different.

 

  1. I tried the last solution to update the database directly by using custom code below. I am able to see the data is updated if I query using sql but custom order line field values are reset as blank after page is loaded and the value is gone if I query again in database.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    var cmdBuilder = new CommandBuilder();

        cmdBuilder.Add(@"select OrderLineFieldValues FROM [EcomOrderLines] where OrderLineId={0}", orderLineId);

        var deliveryDate = HttpContext.Current.Request.QueryString.Get("EcomOrderLineFieldInput_Deliverydate");

        var orderLineCustomFields = "";

 

        using (System.Data.IDataReader dr = Dynamicweb.Data.Database.CreateDataReader(cmdBuilder))

        {

            while (dr.Read())

            {

                orderLineCustomFields = dr["OrderLineFieldValues"].ToString();

            }

        }

 

 

        XmlDocument doc = new XmlDocument();

        doc.LoadXml(orderLineCustomFields);

 

        //var nodes = doc.SelectNodes("//OrderLineFieldValueCollection/OrderLineFieldValue");

        XmlNodeList xmlnodes = doc.SelectNodes("OrderLineFieldValueCollection/OrderLineFieldValue");

        foreach (XmlNode x in xmlnodes)

        {

            if (x["OrderLineFieldSystemName"].InnerText == "Deliverydate")

            {

                x["Value"].InnerText = deliveryDate;

            }

        }

      

        cmdBuilder = new CommandBuilder();

        cmdBuilder.Add(@"update [EcomOrderLines] set OrderLineFieldValues={0} where OrderLineId={1}", doc.InnerXml, orderLineId);

        Database.ExecuteNonQuery(cmdBuilder);

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Looking forward to hear from you.

Thank you

Pang Lin

 

 


Replies

 
Karsten Thuen Dynamicweb Employee
Karsten Thuen
Reply

Hi Tan Pang Lin

As far as I understand your case, you only need to be able to set the delivery date directly in the cart. For this it would be much simpler and easier to implement using a custom field in the cart: https://doc.dynamicweb.com/template-tags/ecommerce/shopping-cart/cart/customorderfields 

My suggestion is that you reuse the design/markup from the Comment block in the cart. 

I hope it helps.

 

Best regards
Karsten Thuen

 
Thaw Htun Lynn
Reply

Dear Karsten,

Thank you for your reply. We are not able to use the custom order field because the customer wants to capture the delivery date at the order line level (meaning for each line item). We are having trouble because we are not able to update the custom order line fields.

Best regards,

Thaw

 
Karsten Thuen Dynamicweb Employee
Karsten Thuen
Reply

Hi Again

I am suspecting that there may be a bug in the Dynamicweb tag that receives the saved field, as I saw it on a recent project. Can you please test getting the value like this?

 

string myField = "";

foreach (LoopItem field in product.GetLoop("Order.OrderLineFields"))
        {
            if (field.GetString("Ecom:Order:OrderLine.OrderLineField.SystemName") == "MyField") {
                myField = field.GetString("Ecom:Order:OrderLine.MyField.Value.Clean"));
            }
        }

And simply save it through the form submit - No complicated javascript.

I think that is my best bet on this.

 

Best regards
Karsten Thuen

 
Karsten Thuen Dynamicweb Employee
Karsten Thuen
Reply

Oh, "product" on the above example is actually an orderline inside an orderline loop :)

 
Thaw Htun Lynn
Reply

Hi Karsten,

We are having some problem to apply the above code because the cart template is using javascript template with JSON feed. Do I need to create a new template without using script template to use your example?

Best regards,

Thaw

 
Karsten Thuen Dynamicweb Employee
Karsten Thuen
Reply

Hi Thaw

I am sorry for the late answer. I just gave the general solution. In the scenario where we are using the json feed, you will have to implement it in the feed and then use it in the corresponding template.

 

Best regards
Karsten Thuen

 
Thaw Htun Lynn
Reply

Hi Karsten,

Thank you very much for your reply. We did found a way to update the custom fields using the server side API calls and it is working fine now.


Best regards,

Thaw

 

You must be logged in to post in the forum