3

Does anyone know of a filter or hook that can be applied to insert a function before an item is inserted into a cart using WooCommerce? I have a similar issue as this post:

Wordpress. Woocommerce. Action hook BEFORE adding to cart

But that OP's comments don't seem to work or are so vague I can't get them to work and I can find no documentation regarding woocommerce_before_add_to_cart.

What I'm trying to do is just display an error, I'll add logic once I can hook into the right action:

function checkProd(){
global $woocommerce;
$woocommerce->add_error( __('ERROR', 'woocommerce') );
return;
}
add_action( 'woocommerce_variable_add_to_cart', 'checkProd');
Community
  • 1
  • 1
RyGuy
  • 508
  • 6
  • 18

2 Answers2

3

The best resource that I've found for woocommerce hooks is actually the repository itself, they have incredibly well commented code that is very readable.

https://github.com/woocommerce/woocommerce/blob/master/includes/wc-template-hooks.php

I'm sure a solution exists for the problem you're trying to solve in one of these sections:

/**
 * Product Add to cart
 *
 * @see woocommerce_template_single_add_to_cart()
 * @see woocommerce_simple_add_to_cart()
 * @see woocommerce_grouped_add_to_cart()
 * @see woocommerce_variable_add_to_cart()
 * @see woocommerce_external_add_to_cart()
 */
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
add_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
add_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
add_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );


/**
* Cart Actions
*
* @see woocommerce_update_cart_action()
* @see woocommerce_add_to_cart_action()
* @see woocommerce_load_persistent_cart()
*/
add_action( 'init', 'woocommerce_update_cart_action' );
add_action( 'init', 'woocommerce_add_to_cart_action' );
add_action( 'wp_login', 'woocommerce_load_persistent_cart', 1, 2 );

If you continue to have issues leveraging the above action hooks feel free to throw me a few more details and I can try to walk you through. Good luck!

timbillstrom
  • 1,176
  • 16
  • 33
Nick Klufas
  • 218
  • 3
  • 9
  • 1
    Thanks for the response Nick. I've tried to use the above actions but so far no luck. The overall goal of what I'm trying to do is if someone clicks to add a variable product to the cart, a function checks whether or not a specific product has already been added. If the required products are not already in the cart, then an error is thrown and the variable product is not added to the cart. I can work on the logic later at this point all I'm trying to do is get the action to call a function and display an error message to show I hooked the right action. – RyGuy Apr 16 '13 at 00:33
  • Do you mind posting some code so I may gander on how exactly your implementing a solution? add_action('woocommerce_simple_add_to_cart','do_something_foo'); function do_something_foo() { } – Nick Klufas Apr 16 '13 at 13:16
  • 1
    The current link is now https://github.com/woothemes/woocommerce/blob/master/includes/wc-template-hooks.php – Ben Feb 18 '14 at 11:47
0

I needed to do some actions before add to cart, so I did it this way

add_action('init', function(){
    //if user clicked http://example.com/shop/?add-to-cart=42
    if(!is_admin() && isset($_REQUEST['add-to-cart'])){
        //do something
    }
});

$_REQUEST used to process both GET and POST requests.

Maxime Ashurov
  • 559
  • 4
  • 11