Tuesday, 12 November 2019

Hide Add to cart for non logged user in WooCommerce

You can hide add to cart for no logged users in Woocommerce. Kindly add the below code in function.php file in your theme.

add_filter('woocommerce_get_price_html','login_before_addtocart');

function login_before_addtocart($price){

if(is_user_logged_in() ){
return $price;
}



else {
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 );

$response .= $price;
$response .= '<br> <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> to add product into the cart';

return $response;
  }

}

No comments:

Post a Comment