php - woocommerce remove action based on product id -


everyone, trying stop plugin function executing if product id in cart. have tried , written following code , put them in theme functions.php, doesn't seem work. instead, whole page become blank. can me out?

$found = false; global $woocommerce; foreach($woocommerce->cart->get_cart() $cart_item_key => $values ) {     $_product = $values['data'];     if( $_product->id == 1097 ) {         $found = true;     } } if ($found){     remove_action('woocommerce_before_order_notes', array($wdt, 'show_field'), 20); } 

by itself, remove_action('woocommerce_before_order_notes', array($wdt, 'show_field'), 20); works. once tried link work base on product id, whole website cannot load. thank you!

the variable $wdt not seam initialized. should ether use class name(string) or instance of class here.

an other guess code runs before action added. make sure run code on later hook in execution see reference: http://codex.wordpress.org/plugin_api/action_reference#actions_run_during_a_typical_request

so if code add hook runs @ init, can try run code @ wp_loaded. this:

add_action('wp_loaded','my_remove_order_notes');  function my_remove_order_notes() {     //your code } 

also, make sure priority parameter matches code action hook added. default priority 10.

edit: ok, should not remove filter. filters supposed used manipulating data , return new data back. this:

add_filter( 'woocommerce_checkout_fields' , 'custom_remove_order_comments', 99 );  function custom_remove_order_comments( $fields ) {     global $woocommerce;          foreach($woocommerce->cart->get_cart() $cart_item_key => $values ) {              $_product = $values['data'];              if( $_product->id == 1097 ) {            unset($fields['order']['order_comments']); //remove order comments field     }     return $fields; //return data woocommerce } 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

python - Django-cities exits with "killed" -

python - How to get a widget position inside it's layout in Kivy? -