Developer forum

Forum » Development » Customized Cart with orderlines

Customized Cart with orderlines

Umar Farooq
Reply

Hi,

I am trying to add Order lines to a cart for non existing product. I can see that it creates an order  in backend but in frontend if i refresh the cart page it seems like it removes the order line from a cart if product doesn't exist  and after that order in backend also gets updated and order line gets removed from there as well. 

Is this the intended behavious or i am doing something wrong in the code? 

 OrderService orderService = new OrderService();
 OrderLineService orderLineService = new OrderLineService();
ShopService shopService = new ShopService();
var area = Dynamicweb.Services.Areas.GetArea(1);
OrderContext context = OrderContext.GetOrderContextById("ORDERCONTEXT5");
Dynamicweb.Ecommerce.Common.Context.CartContext = context;
var cart = Dynamicweb.Ecommerce.Common.Context.Cart;
if(cart == null)
            {
                cart = new Order()
                {
                    IsCart = true,
                    LanguageId = area.EcomLanguageId,
                    OrderContextId = context.Id,
ShopId = area.EcomShopId,
ShopName = shop.Name
                };
                Dynamicweb.Ecommerce.Common.Context.SetCart(cart);
            }
var OrderlineBuilder = new OrderLineBuilderConfig()
            {
                ProductId = string.Empty,
                Quantity = 1,
                VariantId = string.Empty,
                VariantText = string.Empty,
                UnitId = string.Empty,
                OrderLineType = OrderLineType.Product,
                ReferenceUrl = string.Empty,
                PageId = 2235,
                ProductName = product.Name,
                ProductNumber = string.Empty,
            };
            OrderLine ol = orderLineService.OrderLineBuilder(cart, OrderlineBuilder);
orderLineService.SetUnitPrice(ol, 100);
orderLineService.Save(ol);
 orderService.Save(cart);
Dynamicweb.Ecommerce.Common.Context.SetCart(cart);

 

 


Replies

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi Umar,

The cart will remove products that don't exist as it doesn't make sense to have them in there. Why would you want to do that anyway? Or, put another way, what are you trying to achieve?

If you want to have some products in the cart that aren't shown in the frontend, then you should still have them created but either place them outside your group structure or change some product information so they're not shown. If you use Repositories and Indexes to show products, you can use the back catalog features to exclude these products from the index. Go to Settings -> Ecommerce -> Advanced configuration -> General and the section Additional Fields. Enable "Show back catalog fields on products". Now go to the products that should be excluded and tick the "Exclude from index" checkbox. Rebuild your index and these products are no longer visible in frontend.

- Jeppe

 

You must be logged in to post in the forum