Developer forum

Forum » Development » Modifying the basket/order from C#

Modifying the basket/order from C#

Bo Møller
Reply
 Hey everybody,

We've been trying for quite some hours to make a script that adds products to the basket/order from C#.
What we want to do, is to fetch a querystring with the name of the product to add, and then add it. We haven't got this to work yet.

Please check the code below:

using Dynamicweb;
using Dynamicweb.eCommerce.Orders;
using Dynamicweb.eCommerce.Frontend;
using Dynamicweb.eCommerce.Products;

namespace CustomModules.BasketMagic
{

    public class OrderTemplateExtender1 : OrderTemplateExtender
    {

        public override void ExtendTemplate(Dynamicweb.Templatev2.Template template, TemplateExtenderRenderingState renderingState)
        {
            if (renderingState == TemplateExtenderRenderingState.After)
            {
                //string CartMagic = Base.ChkString(Base.Request("CartMagic"));


                if (Base.ChkString(Base.Request("CartMagic")) == "true")
                {
                    try
                    {
                        Base.w("CART MAGIC!");
                        string ProductID = Base.ChkString(Base.Request("CartMagicProductID"));
                        int Quantity = Base.ChkInteger(Base.Request("CartMagicQuantity"));

                        string isLogo = Base.ChkString(Base.Request("CartMagicIsLogo"));
                        string isLeaflet = Base.ChkString(Base.Request("CartMagicIsLeaflet"));
                        string isGreeting = Base.ChkString(Base.Request("CartMagicIsGreeting"));
                        string isCharityPrint = Base.ChkString(Base.Request("CartMagicCharityPrint"));


                        foreach (Dynamicweb.eCommerce.Orders.OrderLine ord in this.Order.OrderLines) 
                        {
                            Base.w("John: " + ord.ProductName);
                        }


                        this.Order.OrderLines.Clear();

                        Product olCardProduct = new Product();

                        foreach (Product p in Dynamicweb.eCommerce.Products.Product.getAllProducts())
                        {
                            if (p.ID == "PROD18"){
                                olCardProduct = p;
                            }
                        }
                        
                        OrderLine olCard = new OrderLine(olCardProduct);
                        
                        olCard.Quantity = Quantity;
                        olCard.Save();
                        Base.w(olCard.Quantity);
                        OrderLineCollection olc = new OrderLineCollection();
                        olc.Add(olCard);
                        olc.Save(this.Order.ID);
                        this.Order.OrderLines.Add(olc);
                        this.Order.Save();
                        Base.w(this.Order.OrderLines.Count);
                        
                        foreach (Dynamicweb.eCommerce.Orders.OrderLine ord in this.Order.OrderLines)
                        {
                            Base.w("Product Name: " + ord.ProductName);

                        }

                        ////this.Order.OrderLines.Clear();
                        ////Indæs OrderLineCollection
                        //OrderLineCollection olc = new OrderLineCollection();

                        ////Indlæs orderline for kortet
                        //OrderLine olCard = new OrderLine(new Product(ProductID));
                        //olCard.Quantity = Quantity;
                        //olc.Add(olCard);

                        //if (isLogo != "" || isGreeting != "" || isCharityPrint != "")
                        //{
                        //    //Indlæs orderline for opstartsgebyr
                        //    OrderLine olStartPay = new OrderLine(new Product("PROD48"));
                        //    olStartPay.Quantity = 1;
                        //    olc.Add(olStartPay);
                        //    //Indlæs orderline for gebyr per kort
                        //    OrderLine olExtraPerCard = new OrderLine(new Product("PROD18"));
                        //    olExtraPerCard.Quantity = Quantity;
                        //    olc.Add(olExtraPerCard);
                        //}
                        //else if (isLeaflet != "")
                        //{
                        //    //Indlæs orderline for indlægssedler
                        //    OrderLine olLeaflet = new OrderLine(new Product("PROD49"));
                        //    olLeaflet.Quantity = Quantity;
                        //    olc.Add(olLeaflet);
                        //}


                        //this.Order.OrderLines.Add(olc);
                        //this.Order.OrderLines.Save(this.Order.ID);
                        //this.Order.Save();
                    }
                    catch (System.Exception ex)
                    {
                        Base.w(ex.Message);
                    }
                }
                else {
                }
            
            }
                else
                {
                    //TODO: AFTER THE ORDER WAS RENDERED
                }
            }
        }
    }




Replies

 
Morten Snedker
Reply
 And your

Base.w("CART MAGIC!");

is actually triggered?

/Snedker

 

You must be logged in to post in the forum