I use the following hook
function dvpi_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id, $variations ) { return $passed; } add_filter( 'woocommerce_add_to_cart_validation', 'dvpi_add_to_cart_validation', 10, 5 ); For a product of the 'variable' type, this works
The problem is that if the type of the product is 'single', this hook is also executed but expected 3 params, otherwise I get this error message:
function dvpi_add_to_cart_validation( $passed, $product_id, $quantity ) { return $passed; } add_filter( 'woocommerce_add_to_cart_validation', 'dvpi_add_to_cart_validation', 10, 3 ); Fatal error: Uncaught ArgumentCountError: Too few arguments to function dvpi_add_to_cart_validation(), 3 passed in /data/sites/web/.../wp-includes/class-wp-hook.php on line 288 and exactly 5 expected in...
And if condition in the function where I am going to check on the product type 'comes too late'.
How can I solve this?
1 Answer
You can change your function parameter to optional like below & check for both single & variation products if it works or not.
function dvpi_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = 0, $variations = null) { return $passed; } add_filter( 'woocommerce_add_to_cart_validation', 'dvpi_add_to_cart_validation', 10, 5 );