26

What i need to do: I want to run some checks on a product before being added to the cart. More exactly: I want to compare the product i am about to add to the cart, with the ones already added, to see if there are some conflicts. An example: Let's say we have a product named "Both shoes", and a product "left shoe". A user adds "left shoe" to the cart. Then he adds "both shoes". I want to print an error instead of adding the "both shoes": Sorry, but you can't add both shoes if you've added left shoe to the cart. If you want to buy "both shoes", please first remove "left shoe".

I've looked at class-wc-cart.php and i found an action hook at line 811, but it's too late! It's after the product has been added

"do_action( 'woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data );"

The add_to_cart method starts at line 705. http://wcdocs.woothemes.com/apidocs/source-class-WC_Cart.html#705

How can my "product conflict manager" function be hooked before line 801, without hacking woocommerce?

Thank you!

Sergio
  • 361
  • 1
  • 3
  • 5
  • 1
    If i were to hack it, i would just add the following lines at line 799: `if(!do_action( 'woocommerce_before_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data )) { return false; }` And use `add_action ('woocommerce_before_add_to_cart','add_to_cart_conflict_manage',10,6);` in my functions.php – Sergio Jan 20 '13 at 19:30
  • ^ or something similar to that – Sergio Jan 20 '13 at 21:37
  • 1
    The hack ended being actually a filter instead of an action `// This is supposed to handle product conflicts and check if user already has access to the product // $product_conflict_error = apply_filters( 'woocommerce_before_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data,$product_data); if (product_conflict_error) { $woocommerce->add_error( __($product_conflict_error, 'woocommerce') ); return false; }` – Sergio Jan 21 '13 at 03:33
  • Have you discovered an answer to this problem? I am having the same issue and haven't had any luck finding the correct hook. – Eric K Oct 23 '13 at 18:55

1 Answers1

40

A bit late, but I think you are looking for add to cart validation, which is filterable. Here's an over simplified version:

function so_validate_add_cart_item( $passed, $product_id, $quantity, $variation_id = '', $variations= '' ) {

    // do your validation, if not met switch $passed to false
    if ( 1 != 2 ){
        $passed = false;
        wc_add_notice( __( 'You can not do that', 'textdomain' ), 'error' );
    }
    return $passed;

}
add_filter( 'woocommerce_add_to_cart_validation', 'so_validate_add_cart_item', 10, 5 );
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Thanks! ^_^ This totally put me on the right path; however, I noticed an erroneous closing bracket on the `add_filter` line, and it needs the number of arguments for the filter set along with the priority (`add_filter( 'woocommerce_add_to_cart_validation', 'so_validate_add_cart_item', 10, 5);`), otherwise it will throw an error. – indextwo Aug 06 '14 at 16:54
  • 1
    How can we make it work when the products are added via ajax? – Learning and sharing May 19 '18 at 17:49
  • 1
    Validation is run even when a product is added via ajax. – helgatheviking May 22 '18 at 04:24