Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI need to remove checkout page field validation if a particular product is in the cart.
Plugin code :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['developer_name'] )
wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}
I need to remove this action hook my_custom_checkout_field_process
only if the customer added the product_id (19) to the cart. Else there's no need to remove the add_action
.
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 6 years ago.
Improve this questionI need to remove checkout page field validation if a particular product is in the cart.
Plugin code :
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['developer_name'] )
wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}
I need to remove this action hook my_custom_checkout_field_process
only if the customer added the product_id (19) to the cart. Else there's no need to remove the add_action
.
2 Answers
Reset to default 2try this below code in your function.php file or in your plugin
add_action("init", function () {
// removing the woocommerce hook
foreach( WC()->cart->get_cart() as $cart_item ){
$product_id = $cart_item['product_id'];
if($product_id!='19')
{
remove_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
}
}
});
WordPress compiles a list of actions quite early in the process, possibly before the product_id
is known. So you probably (I don't know where WooCommerce executes this action) cannot execute this action conditionally.
However, what you can do is remove the action completely and define a new action that includes the condition. Also you must make sure this is done before the old action is executed. Like this:
add_action ('woocommerce_checkout_process', 'wpse333234_change_hook', 1); // early priority
function wpse333234_change_hook () {
remove_action ('woocommerce_checkout_process', 'my_custom_checkout_field_process'); // remove old hooked function
add_action ('woocommerce_checkout_process', 'wpse333234_new_hook', 10); // define new hooked function with later priority
}
function wpse333234_new_hook () {
// Check if set, if its not set add an error.
if ( ! $_POST['developer_name'] && !$product_id==19)
wc_add_notice( __( 'Please fill in your name.' ), 'error' );
}
Beware that the latter function will give an error initially, because $product_id
is not defined in the function. I don't know how this is defined in WooCommerce. You'll need a way to access this (global?) variable in some way.