admin管理员组

文章数量:1025477

Closed. This question is off-topic. It is not currently accepting answers.

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 question

I 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.

Closed. This question is off-topic. It is not currently accepting answers.

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 question

I 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.

Share Improve this question edited Apr 2, 2019 at 14:12 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Apr 2, 2019 at 12:26 KanewilliamKanewilliam 611 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

try 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.

Closed. This question is off-topic. It is not currently accepting answers.

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 question

I 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.

Closed. This question is off-topic. It is not currently accepting answers.

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 question

I 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.

Share Improve this question edited Apr 2, 2019 at 14:12 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Apr 2, 2019 at 12:26 KanewilliamKanewilliam 611 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

try 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.

本文标签: