最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to remove the woocommerce_checkout_process action hook in woocommerce if particular project in cart

programmeradmin8浏览0评论
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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论