0

Hi I have faced a problem on my code. I wanted to run add_action for the certain page. add_action is working individually when I Put this on the functions.php but when I tried to run this in wp_head action then its not working.

add_action( 'wp_head', 'remove_my_action', 0);

function remove_my_action($post) {
    global $post;
    $post_id = $post->ID;
    if (512 == $post_id) {
        echo "Page Found";
        remove_action( 'woocommerce_add_to_cart', 'add_product_to_cart');
    }else{
        add_action('woocommerce_add_to_cart', 'add_product_to_cart');
    }
}
Kane
  • 605
  • 2
  • 8
  • 22
  • What do you mean with "its not working"? Have you checked if $post_id has an actual value? did u check if global $post is set? if not you can move the action further down into the wp action call stack https://profbrown.org/p/wp_hooks/hook – Diego Jan 19 '22 at 10:51
  • when I run the `remove_my_action` or `add_action` in if statement then its not worked. – Kane Jan 19 '22 at 11:23
  • I guess i don't have enough info to help you. Are you sure the action "add_product_to_cart" actually exists? i can't find it in the default woocommerce codebase. Where does it comes from? what adds it? can you find it in the code? please give us some more info – Diego Jan 19 '22 at 13:13

0 Answers0