I can't seem to find this in the docs or through google. So basically I'm just looking for the Javascript event that fires when an item is added to the cart. For some reason my default added to cart notification isn't working.

EDIT: So I'm thinking because I'm including the woocommerce.php file in my themes default folder as well as my woocommerce template overrides.


Read the source. From woocommerce-ajax.php (in the plugin's root folder)

Javascript/Ajax add to cart

 * AJAX add to cart
 * @access public
 * @return void
function woocommerce_ajax_add_to_cart() {

    global $woocommerce;

    check_ajax_referer( 'add-to-cart', 'security' );

    $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_POST['product_id']);

    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, 1);

    if ($passed_validation && $woocommerce->cart->add_to_cart($product_id, 1)) :
        // Return html fragments
        $data = apply_filters('add_to_cart_fragments', array());
        do_action( 'woocommerce_ajax_added_to_cart', $product_id);
    else :
        // If there was an error adding to the cart, redirect to the product page to show any errors
        $data = array(
            'error' => true,
            'product_url' => get_permalink( $product_id )

    echo json_encode( $data );


add_action('wp_ajax_woocommerce_add_to_cart', 'woocommerce_ajax_add_to_cart');
add_action('wp_ajax_nopriv_woocommerce_add_to_cart', 'woocommerce_ajax_add_to_cart');

Non-AJAX: Requires Page Load

If you are not using AJAX then you are using the woocommerce_add_to_cart_action() from woocommerce-functions.php which runs on the init hook. It is a bit long, so I will just let you read it from the mentioned file. Basically it looks for an add-to-cart` query parameter (which should equal the product ID) and goes from there.

| improve this answer | |
  • Is this the default template used? What if say I'm not using AJAX and the page reloads to add an item to the cart? – Justin Beaudry Feb 28 '13 at 9:20
  • You asked for the javascript event/action. The non-ajax add to cart is handled by the woocommerce_add_to_cart_action() function which fires on the init hook. – helgatheviking Feb 28 '13 at 13:30
  • @helgatheviking I used the following code in my template file $_REQUEST['add-to-cart'] = $proid; $_REQUEST['quantity'] = $qty; global $woocommerce; woocommerce_add_to_cart_action(); to add the products in to the cart. But it not working. what i am doing wrong here? – Vasanthan.R.P May 3 '13 at 6:10
  • 2
    You should really ask your own question. And when you do try to be really specific about what you are doing and where you are failing. "it is not working" doesn't tell anyone anything useful. – helgatheviking May 3 '13 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.