Developer forum

Forum » Development » Cart function

Reply
Hi,

If I were to make a small function that allowed only one orderline to be added to the cart per order. So if a second product is added the cart the cart must first be emthyed and the add the new product.

Any ideas?



Replies

 
Reply
 Hi,

Try something like this,

using System;
using System.Collections.Generic;

using Dynamicweb;
using Dynamicweb.eCommerce;
using Dynamicweb.eCommerce.Orders;
using Dynamicweb.eCommerce.Frontend.Cart;

namespace DwExAddIn
{
[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.Line.Added)]
public class EcomCartLineAddedObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
{
public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
{
if (args == null || !(args is Dynamicweb.Notifications.eCommerce.Cart.Line.AddedArgs))
return;

Dynamicweb.Notifications.eCommerce.Cart.Line.AddedArgs added = (Dynamicweb.Notifications.eCommerce.Cart.Line.AddedArgs)args;

Order cart = added.Cart;

List linesToRemove = new List(cart.OrderLines.Count);
foreach (OrderLine line in cart.OrderLines)
{
if (line.ID != added.AddedLine.ID)
{
linesToRemove.Add(line.ID);
}
}

foreach (string orderLineId in linesToRemove)
{
cart.OrderLines.RemoveLine(orderLineId);
}

cart.Save();
CartCatch.SaveCart();
}
}
}

//Dmitry
 
Reply
Hi Dmitry, 

That was just what I was looking for.

Thanks!

/Rasmus
 
Reply
One last thing:

I need to do the same if the total order price has changed.

I've tryed:
foreach (OrderLine line in cart.OrderLines)
{
    if (line.ID != added.AddedLine.ID || line.Price != added.AddedLine.Price)
    {
        linesToRemove.Add(line.ID);
    }
}

This in only the product price and not the totalorder price.
 
Reply
I've solved the last bit by adding this:

But it fires on both increased and decreased, how come? Im only using IncreasedArgs.



[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.Line.Increased)]
public class EcomCartLineIncreasedObserver : Dynamicweb.Extensibility.NotificationSubscriber
{
    public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
    {
        if (args == null || !(args is Dynamicweb.Notifications.eCommerce.Cart.Line.IncreasedArgs))
            return;

        Dynamicweb.Notifications.eCommerce.Cart.Line.IncreasedArgs item = (Dynamicweb.Notifications.eCommerce.Cart.Line.IncreasedArgs)args;

        if (item.IncreasedLine.Quantity > 1)
        {
            item.IncreasedLine.Quantity = item.AmountIncreased;
            item.IncreasedLine.Save();
        }

    }
}
 
Reply

Hi

Dynamicweb.Notifications.eCommerce.Cart.Line.Increased fires only when you increase quantity. We can’t reproduce the event when we decrease the quantity.
BTW you always can check link’s quantity before reduce its amount in depend from what events come: increase or decrease

 if (item.IncreasedLine.Quantity > 1)
{
      item.IncreasedLine.Quantity = item.AmountIncreased;
      item.IncreasedLine.Save();
}

 Kind Regards
Zhukovskiy Yury

 
Rasmus Samsig
Reply

Hi

Dynamicweb.Notifications.eCommerce.Cart.Line.Increased fires only when you increase quantity. We can’t reproduce the event when we decrease the quantity.
BTW you always can check link’s quantity before reduce its amount in depend from what events come: increase or decrease

 if (item.IncreasedLine.Quantity > 1)
{
      item.IncreasedLine.Quantity = item.AmountIncreased;
      item.IncreasedLine.Save();
}

 Kind Regards
Zhukovskiy Yury


Thansks Zhukovskiy. That makes sense.

I have a bigger problem atm.
I only want to fire the
notifications when a product is in a shop called shop1

I thought that I could do something like this:
foreach (Dynamicweb.eCommerce.Shops.Shop sh in Dynamicweb.eCommerce.Shops.Shop.getShops())
    {
        if (sh.Name == "SHOP1")
        {
            //something

        }
}

But it fires also when a product is in a shop called shop2

Do you see the problem?

/Rasmus
 
Yury Zhukovskiy
Reply

Hi Rasmus,

Dynamicweb.eCommerce.Shops.Shop.getShops() always contains shop with ID = “SHOP1”

You need to check product’s shop property, for example:

            if (Product.Shop.ID == "SHOP1")

            {

                //do something

            }

Kind Regards 

Zhukovskiy Yury

 
Rasmus Samsig
Reply
Thanks for the quick reply.

But that means that every product needs to have set a shopID on it. As default it has none. :/
So I guess thats why it's not working.
But maybe I should use the groupID?

namespace OneProductOnly
{
[Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.Line.Added)]
public class EcomCartLineAddedObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
{
public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
{

foreach (Dynamicweb.eCommerce.Shops.Shop sh in Dynamicweb.eCommerce.Shops.Shop.getShops())
{

        foreach (Group g in sh.Groups)
        {
            foreach (Product p in g.Products)
            {
                if (p.Shop.ID == "SHOP1")
                {
                    if (args == null || !(args is Dynamicweb.Notifications.eCommerce.Cart.Line.AddedArgs))
                        return;


                    Dynamicweb.Notifications.eCommerce.Cart.Line.AddedArgs added =                     (Dynamicweb.Notifications.eCommerce.Cart.Line.AddedArgs)args;

                    Order cart = added.Cart;

                    List linesToRemove = new List(cart.OrderLines.Count);
                    foreach (OrderLine line in cart.OrderLines)
                    {
                        if (line.ID != added.AddedLine.ID)
                        {
                            linesToRemove.Add(line.ID);
                        }

                    }


                    foreach (string orderLineId in linesToRemove)
                    {
                        cart.OrderLines.RemoveLine(orderLineId);
                    }

                    cart.Save();
                    CartCatch.SaveCart();

                }
            }
        }


}
}
}
}

 
Rasmus Samsig
Reply
Damn.. That didn't work :/
 
Yury Zhukovskiy
Reply

Sorry, you have to check Product.DefaultShopID.

 

            if (Product.DefaultShopID == "SHOP1")

            {

                //do something

            }

 
Rasmus Samsig
Reply

Sorry, you have to check Product.DefaultShopID.

 

            if (Product.DefaultShopID == "SHOP1")

            {

                //do something

            }

No problem. :)

It did'nt solve my problem.

I've got 2 shops. Both with over 200 products.
All of them have SHOP1 as default.shop.id. Or am I missing something?
Is there anything else that a can do a check on?

 
Rasmus Samsig
Reply
Problem solved!
I think :)

The Cart module has a ShopID :)

Thank you for your time and help..

/Rasmus

 
Yury Zhukovskiy
Reply
Perfect!

 

You must be logged in to post in the forum