Skip to content

Instantly share code, notes, and snippets.

@m3esma
Created November 30, 2019 15:08
Show Gist options
  • Save m3esma/189d37444ec49bdff4f45550d770d06f to your computer and use it in GitHub Desktop.
Save m3esma/189d37444ec49bdff4f45550d770d06f to your computer and use it in GitHub Desktop.
WooCommerce - Disable purchase for non-logged-in users. (Remove add-to-cart button).
<?php
// Do NOT include the opening php tag.
// Disable purchase for non-logged-in users. (Remove add-to-cart button).
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
if ( ! is_user_logged_in() ) {
return false;
}
return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );
// Show "Login to see prices" instead of price for non-logged in users.
function m3wc_woocommerce_get_price_html( $price ) {
if ( ! is_user_logged_in() ) {
return '<a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Login</a> to see prices';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'm3wc_woocommerce_get_price_html', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment