1

I already change the add to cart text in global product

remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
add_action( 'woocommerce_after_shop_loop_item', 'add_cart_button_replace', 10);

function add_cart_button_replace() {
    global $product;
    $link = $product->get_permalink();
    echo do_shortcode('<a href="'.$link.'" class="button addtocartbutton">+show OFFER</a>');



}

but, in a certain product category page like here, where I have a free-products term in product-category custom taxonomy and I want to change the add to cart text into +choose me with add to cart link.

closed as off-topic by cybmeta, Jack Johansson, fuxia Jul 6 '17 at 1:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

You can use the has_term function to check for the desired term.

function add_cart_button_replace() {
    global $product;
    $link = $product->get_permalink();

    $button_text = __('+show OFFER', 'woocommerce');

   // check if the current product has a "product-category" of "free-products"
    if(has_term('free-products', 'product_cat', $product->get_id()))
        $button_text = __('+choose me', 'woocommerce');

    echo do_shortcode('<a href="'.$link.'" class="button addtocartbutton">' . $button_text . '</a>');

}

You can also learn more about has_term function.

Update: change product-category taxonomy to product_cat as per your comment.

  • thanks for the reply, but still not working.. – Marvin Desoyo Jul 6 '17 at 4:34
  • I just update the code you provide ` function add_cart_button_replace() { global $product; $link = $product->get_permalink(); $cap = __( '+show OFFER', 'woocommerce' ); echo do_shortcode('<a href="'.$link.'" class="button addtocartbutton">' . $cap . '</a>'); if( has_term( 'free-products', 'product_cat', $product->get_id() ) ){ $text = __( '+choose me', 'woocommerce' ); echo do_shortcode('<a href="'.$link.'" class="button addtocartbutton">' . $text . '</a>'); } return $text; } ` visit link – Marvin Desoyo Jul 6 '17 at 6:10

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