Are you looking for a hook (to do something) after customers or visitors added the product to the cart? That is after the user clicked on the button shown in the screenshot below?

WooCommerce Add to cart button

The right hook is “woocommerce_add_to_cart”.

Usage of “woocommerce_add_to_cart”

The example code snippet below will send an email to the administrator whenever the product is added to the cart with a quantity greater than 10. Useful if you want to know who just added such a large amount of products to the cart. You can reach out to them if they don’t buy. Seriously. : )

add_action( 'woocommerce_add_to_cart', 'sa_send_email', 10, 6 );

function sa_send_email( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data  ) {

    $product = wc_get_product( $product_id );
    $user =   wp_get_current_user();

    $name  = ! empty( $user->user_firstname  ) ? $user->user_firstname : 'Unknown';
    $email = ! empty( $user->user_email  ) ? $user->user_email : 'Unknown';
    
    if ( $quantity > 10 ) {

        $subject = "Product Added to cart!";
        $message = "Product " . $product->get_title() . " with quantity " . $quantity . " has just been added to the cart by " . $name . " (email: " . $email . " )";

        wp_mail( get_option( 'admin_email' ), $subject, $message );
    }
}

You can see the parameters available are:

$cart_item_key – The cart item key.

$product_id – The product id.

$quantity – The quantity.

$variation_id – The variation id.

$variation – The variation.

$cart_item_data – The cart item data.

Are you also looking for a hook after the order is completed?

or if you want to track your visitor’s journey check out the Customer Journey For WooCommerce plugin.

That’s all.

WooCommerce hook after product added to the cart
Tagged on:     

Sanjeev Aryal

Don't bury your thoughts, put your vision into reality ~ Bob Marley.

Leave a Reply

Your email address will not be published. Required fields are marked *

× WhatsApp