• Resolved peorthyr

    (@peorthyr)


    Good day all.
    I’ve added this function to the theme:

    function western_custom_buy_buttons(){
    
       $product = get_product();
    
       if ( has_term( 'exclusive', 'product_cat') ){
    
           // removing the purchase buttons
    
           remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' ); 
    
           remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
    
           remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
    
           remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
    
           remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
    
           remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
    
       }
    
    }
    
    add_action( 'wp', 'western_custom_buy_buttons' );

    but instead of just removing the button, I’d like to add a simple text/link, so for some categories, the add to cart button is not shown, and a different link si shown instead.

    does anyone has a clue?

    https://www.ads-software.com/plugins/woocommerce/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    You’re missing a 3rd arg https://codex.www.ads-software.com/Function_Reference/has_term

    Pass in global $post;

    global $post;
    
    if ( is_single() && has_term( 'exclusive', 'product_cat', $post ) ){
    Thread Starter peorthyr

    (@peorthyr)

    oh, you are right. Thanks, well, it was an optional parameter, I thought it wasn’t an issue.
    by the way, this has no impact on the possibility to add something different instead of the add to cart button, am I wrong?

    Plugin Contributor Mike Jolley (a11n)

    (@mikejolley)

    This removes buttons but you have no added code to add a new button yet.

    Thread Starter peorthyr

    (@peorthyr)

    yes, and that was actually my question ;D

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    So with every remove_action, you will need and add_action as well for the same hook. Then just create a simple function that outputs your simple text/link.

    Something like this:

    function western_custom_buy_buttons(){
      global $post;
    
      if ( is_single() && has_term( 'exclusive', 'product_cat', $post ) ){
          // removing the purchase buttons
          remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
          add_action( 'woocommerce_after_shop_loop_item', 'wc_ninja_custom_add_to_cart' );
    
          remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
          add_action( 'woocommerce_single_product_summary', 'wc_ninja_custom_add_to_cart', 30 );
    
          remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
          add_action( 'woocommerce_simple_add_to_cart', 'wc_ninja_custom_add_to_cart', 30 );
    
          remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
          add_action( 'woocommerce_grouped_add_to_cart', 'wc_ninja_custom_add_to_cart', 30 );
    
          remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
          add_action( 'woocommerce_variable_add_to_cart', 'wc_ninja_custom_add_to_cart', 30 );
    
          remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
          add_action( 'woocommerce_external_add_to_cart', 'wc_ninja_custom_add_to_cart', 30 );
       }
    }
    add_action( 'wp', 'western_custom_buy_buttons' );
    
    function wc_ninja_custom_add_to_cart() {
      echo '<a href="#" class="button">Button</a>';
    }
    Thread Starter peorthyr

    (@peorthyr)

    wow…simple and effective, maybe I had to make sure the function is fired only once, but I’ll test it soon, so I should handle this issue if it presents.

    well, this is very usefull, thanks a lot Caleb.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘change the "add to cart" into another link, only in some categories’ is closed to new replies.