0

I wanted to add the action hook below only if we are not in a specific product ID. So i tried something like this in my functions.php

global $product;
$productId = $product->get_id();
if ($productId != 1345){
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );
}

But, it's impossible to call the $product variable out of a function.

So i tried to do this

function woocommerce_add_new_add_to_cart(){
global $product;
$productId = $product->get_id();
if ($productId != 1345){
        add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );
    }

}
add_action('woocommerce_single_product_summary', 'woocommerce_add_new_add_to_cart', 10);

And it didn't work as well.

So then i found this post : Is it possible to create an action hook using do_action() within add_action()?

And i tried this

function woocommerce_add_new_add_to_cart(){
    global $product;
    $productId = $product->get_id();

    if ($productId != 1345){
        add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 10 );
    }

}
add_action('woocommerce_single_product_summary', function(){do_action('woocommerce_add_new_add_to_cart');});

And it doesn't work as well. What's wrong ? How can i do this ? Any idea ? :/

Thanks

2
  • When in the product phase do you want to add the function when an item is added to the cart? Or when they go to the product page? What is the ultimate goal?
    – Orlando P.
    May 2, 2018 at 23:12
  • while there might be a non WC specific solution, the only valid answer right now is "do not use globals in context in which you are not 1000% sure they are properly set" which is not a great answer, but the best that can be given without diving into WC internals, therefor at its current form it is off topic. If you think you can rewrite it to not be so WC specific, go for it May 3, 2018 at 5:08

1 Answer 1

1

Woocommerce adds the action hook at priority order 30.

add_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30);

So, You may have done

remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30);

to remove the action from the hook. To remove an action the priority must match the priority with with the function was originally added.

Now you can do

function add_cart_button(){
  global $product;
  $id = $product->get_id();
  if($id != 1345 ):
  add_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30); // You should note this should trigger later tested at 20, 30, 35 working
  endif;
}
add_action('woocommerce_single_product_summary','add_cart_button'); // This triggers at 10 by default

To add the button again.

You should check the order of execution of functions in a hook.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.