Replace "Add to cart" for guests with "Log in or Register to buy" Woocommerce

Viewed 397

I'm trying to replace "Add to cart button" for GUESTS with "Log in or Register to buy" button with link to register/login page.

This Replace add to cart button for unlogged users in WooCommerce (part 1) answer code is something very similar to what I need.

I replaced

// $button_link = get_permalink( wc_get_page_id( 'myaccount' ) ); // Login Url

With

// $button_link = get_permalink( wc_get_page_id( 'mans-konts' ) ); // Login Url

But:

  • It's not working for me, maybe I'am editing wrong fields (edited only login url)? Which fields should be edited?

  • I need it to work only for certain category not all shop, is it possible?

1 Answers

Try this code, first method removes the add to cart button from product loops, second method adds a new add to cart button

function remove_loop_button(){
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
}
add_action('init','remove_loop_button');

add_action('woocommerce_after_shop_loop_item','replace_add_to_cart');
function replace_add_to_cart() {
    global $product;
    $link = $product->get_permalink();

    echo do_shortcode('<a  href="'.$link.'" class="button add_to_cart_button">'.$product->add_to_cart_text().'</a>');
}
Related