Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1025477
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.
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.
本文标签:
版权声明:本文标题:plugins - How to remove the woocommerce_checkout_process action hook in woocommerce if particular project in cart 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745632578a2160250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论