Developer forum

Forum » Development » Event before adding product to cart?

Event before adding product to cart?

Dimitrij Jazel
Reply

Hi Guys,

Am trying to find the right event.

I need to validate some stuff before I add to the cart. I also need to possibly alter Quantity on the fly if certain conditions match.

Tried looking up in Dynamicweb.Notifications.eCommerce.Order.* but can't seem to find it.

I know that it exists somewhere, as I did that on other projects. But the problem is I can't remember what project and where. So hope I can get a quick tip on what event is that? :)

 


Replies

 
Dimitrij Jazel
Reply

Ahh found it. :)

Dynamicweb.Notifications.eCommerce.Cart.Line.*

 
Dimitrij Jazel
Reply

Actually I do need some help with this.

It does not seem that my code is getting triggered.

Am not 100% sure why.

 

Here is what I am currently trying to use:

using Dynamicweb;
using Dynamicweb.eCommerce.Products;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CustomCodes.CustomModules
{
    [Dynamicweb.Extensibility.Subscribe(Dynamicweb.Notifications.eCommerce.Cart.Line.Added)]
    public class CartLineAdded : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            var CartLineAddedArgs = args as Dynamicweb.Notifications.eCommerce.Cart.Line.AddedArgs;

            Dynamicweb.LogToFile.Log("triggered!", "/dmj/olQtyDebug", LogToFile.LogType.ManyEntriesPerFile);
            var ol = CartLineAddedArgs.AddedLine; 
            if (ol!= null)
            {
                ol.Quantity = 0.5;
            }
            else
            {
                Dynamicweb.LogToFile.Log("OL is null", "/dmj/olQtyDebug", LogToFile.LogType.ManyEntriesPerFile);
            }
        }
    }
}
 
Nicolai Pedersen
Reply

So you get no logfile entries?

And can you provide a dump of your setup? Are you sure this code is compiled to a dll and added to /bin?

 
Dimitrij Jazel
Reply

Hi Nicolai,

q:Are you sure this code is compiled to a dll and added to /bin?

a: yes, they are added to correct bin.

 

q: And can you provide a dump of your setup? 

a: Not sure What you mean by that? Is it some kind of DW dump file, that DW can generate, or you just need some info on how the solution is setted up?

Well in short, it is Premium 2014 version 8.8.1.18 - Custom application, and there is nothing very special about it.

Root is appication, and added 2 virtual folders /Admin and /Files in IIS.

 
Nicolai Pedersen
Reply

And logfile entries?

 
Dimitrij Jazel
Reply

No log entries, since that is the whole point with the method, to see if it is getting triggered or not.

Well the problem is that suddenly DW started adding 1,1 instead of just "1" product to cart.

meaning it adds 0,1 to the quantity.

I found out why is that, and that is because upon adding order to cart, it calls url "http://test.localsite.no/Default.aspx?id=20&productid=SSE023J1&cartcmd=add&quantity=1&quantity=1". And funny part is that I am sure that I have only 1 name="quantity" tag inside whole DOM, and it still somehow calls quantity 2 times.

This causes DW to think that Quantity is 1,1 = meaning price should be also multiplied by 1,1.

 

This was not like that it it suddenly started happening, trying to find out why this can happen.

Same thing happens when in product list, I am using a url "http://test.localsite.no/Default.aspx?id=20&productid=SSE023J1&cartcmd=add&quantity=1" but the network inspector shows that it called "http://test.localsite.no/Default.aspx?id=20&productid=SSE023J1&cartcmd=add&quantity=1&quantity=1" instead.

In order to generate that link, I am using this tag right here: "product.GetString("Ecom:Product:SavedForLater.AddToBasketLink");",

In product list, I have a link "http://test.localsite.no/Default.aspx?productid=SSE023J1&cartcmd=add" adds 1,1 to cart on some products.

 
Nicolai Pedersen
Reply

Sounds like you both have a quantity field and quantity parameter on the url used for submitting the form

 
Dimitrij Jazel
Reply

Sounds logical, and that is what I was trying to locate, but can't seem to find it.

This is how actual form looks like:

<form method="post" action="/Default.aspx">
                <input type="hidden" id="prodID" name="id" value="20">
                <div class="pro-details-color-quantity fix">
                    <div class="pro-details-quantity">                        
                        <div class="pro-qty fix">
                            <input type="text" value="1" name="quantity" class="cart-plus-minus-box" id="quantity_MAIN">
                        <span class="inc qtybtn"><i class="zmdi zmdi-chevron-up"></i></span><span class="dec qtybtn"><i class="zmdi zmdi-chevron-down"></i></span></div>
                    </div>
                </div>
                <!-- Product Action -->
                <div class="pro-details-action fix">
                    <input type="hidden" name="cartcmd" value="add">
                    <input id="product-id-input" type="hidden" name="productid" value="SSE029J1">
                    
<!-- This is the submit -->
                            <input class="pro-details-act-btn btn-text prodDetalesButtonAddToCart addToCart_ajax" fb_content_name="*SEIKO ASTRON GPS SOLAR 45MM SAFIR 100M  CHRONO XL" fb_content_category="GROUP1" fb_content_ids="SSE029J1" fb_unitprice="21950" fb_currency="NOK" type="submit" value="Legg i Handlekurv" addtocart_url="/Default.aspx?id=20&amp;productid=SSE029J1&amp;cartcmd=add&amp;quantity=1">

                    
                            <span class="pro-details-act-btn btn-icon btn_prod_wishlist" prodref="SSE029J1" userref="1VKZD78A1AMYIDTU6UJB263R" pageid="20" wlcmd="add"><i class="zmdi zmdi-star-outline"></i></span>

                    
                </div>
            </form>

Now I can see only 1 quantity field there.

fb_param attributes are needed only for Facebook pixel.

 
Nicolai Pedersen
Reply

You have it twice. See dump

Capture.PNG
 
Dimitrij Jazel
Reply

My last sentense in my last post.

This is used for Facebook pixel.

And I don't think that this is how Form posting works. I udnerstand if this URL would be an form Action or part of other input like quantity, or cartcmd etc... but it's not.

 

I ended up replacing form post with Ajax call.

 

But back to the original question, Cart.Line.Added is still not beying invoked.

Any suggestion on what can I do to invoke it?

 

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Dimitrij,

If you are adding the same product multiple times then you might need to subscribe to the Cart.Line.Increased notification as well.

And try using clean templates without any javascript and then include a simple form on your product list or details page...

<form method="post" action="/">
    <input type="hidden" name="ProductID" value="<!--@Ecom:Product.ID-->" />
    <input type="hidden" name="CartCmd" value="add" />
    <input type="number" name="Quantity" value="1" />
    <button type="submit">Add to cart</button>
</form>

 

You must be logged in to post in the forum