been using this code to hide prices..
add_filter('woocommerce_get_price_html','members_only_price');
function members_only_price($price){
if(is_user_logged_in() ){
return $price;
}
else return 'Login or Register to see price!';
}
tried modifying it to use for hiding add to cart as well..but no avail..
anyone?
Extending the above code (thanks Ewout), the following code will get rid of all prices and 'add to cart' buttons on all woocommerce products, as well as provide an explanation as to why. I needed the code for a website that offers direct selling products and to comply with their rules, I cannot show prices to the general public.
Add the filter to your theme's functions.php file.
add_filter('woocommerce_get_price_html','members_only_price');
function members_only_price($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_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Only Registered Users are able to view pricing.';
}
}
Have you tried something like this? You would set woocommerce to only show prices when user is logged in.
add_filter('catalog_visibility_alternate_price_html', 'my_alternate_price_text', 10, 1);
function my_alternate_price_text($content) {
return 'Login or Register to see price!';
}
Reference: http://docs.woothemes.com/document/catalog-visibility-options/
EDIT:
The reference material has the cart visibility reference
add_filter('catalog_visibility_alternate_add_to_cart_button', 'my_alternate_button', 10, 1);
function my_alternate_button($content) {
return 'Login or Register to see cart!';
}
What about CSS?
button.add-to-cart {
display: none;
}
body.logged-in button.add-to-cart {
display: block;
}
We can do this easily through the woocommerce_is_purchasable and woocommerce_get_price_html hooks.
Code:
// 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 'Login to see prices';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'm3wc_woocommerce_get_price_html', 10, 2 );
And the result:
Source: WooCommerce - Disable purchase for non-logged-in users. (Remove add-to-cart button).
Related
For non logged in users I use the php snippet below. Visitors see a "Where to buy" button and a "Ask a demo" button instead of the Woocommerce Add to cart button. Logged in users / customers with a specific role gets the "Add to cart button" instead of the 2 buttons for not logged in customers. There is also a viewonly role who has a login and can download materials but don't have an Add to cart button on single product pages. However with the code below the viewonly users can still see the regular Add to cart button. Any idea how i can fix this?
add_action( 'init', 'disable_add_to_cart' );
function disable_add_to_cart() {
if ( !is_user_logged_in() OR $user_role == 'viewonly' ) {
add_filter( 'woocommerce_is_purchasable', '__return_false');
}
function hide_price( $price ) {
if ( !is_user_logged_in() ) {
$price = '';
$user_role = 'viewonly';
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'hide_price' );
add_filter( 'woocommerce_cart_item_price', 'hide_price' );
add_filter( 'woocommerce_cart_item_subtotal', 'hide_price' );
add_filter( 'woocommerce_order_formatted_line_subtotal', 'hide_price' );
add_filter( 'woocommerce_cart_subtotal', 'hide_price' );
add_filter( 'woocommerce_cart_totals_order_total_html', 'hide_price' );
}
add_action( 'init', 'hide_price_add_cart_not_logged_in' );
function print_login_to_see() {
echo '<div class="var-prod-container"><a class="btn-where-to-buy" href="/where-to-buy">' . __('Where to buy', 'company') . '</a>';
echo '<a class="btn-ask-a-demo" href="/#uagb-tabs__tab1">' . __('Ask a demo', 'company') . '</a>';
echo '</div>';
}
function hide_price_add_cart_not_logged_in() {
if ( ! is_user_logged_in() ) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
add_action( 'woocommerce_single_product_summary', 'print_login_to_see', 31 );
}
}```
To replace the buttons, all you have to do is use the actions you removed and place your button function into it. I am using the author role for testing purposes, but this will check if the user is not logged in and what their role is. Then, remove the add to cart button from the shop page loop and also the single product page. Once they are removed, we add what we want into where we just removed the button from.
If you need to, just change the conditions within the if statement and that will change who sees which set of buttons.
Now all you have to do is style your buttons the way you'd like them!
$user_role = wp_get_current_user(
function print_login_to_see() {
echo '<div class="var-prod-container"><a class="btn-where-to-buy" href="/where-to-buy">' . __('Where to buy', 'company') . '</a>';
echo '<a class="btn-ask-a-demo" href="/#uagb-tabs__tab1">' . __('Ask a demo', 'company') . '</a>';
echo '</div>';
}
if ( current_user_can( 'viewonly' ) || !is_user_logged_in() ) { // Set the user role to the specific role you want to replace the buttons for.
// Remove button from products in the loop and single product page
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 );
// Add the actions you just removed, but insert your button function
add_action( 'woocommerce_after_shop_loop_item', 'print_login_to_see');
add_action( 'woocommerce_single_product_summary', 'print_login_to_see', 30 );
}
I am trying to display sale prices to users that enter the website with the following link www.example.com?sale and show regular prices to the rest of users. The code below does the job for single products, sale price is shown only to users with this url www.example.com?sale, but on car it switches back to regular price. And only works for single products. I'm pretty new with woocommerce and php, hope someone can give me a hand with this. Thanks so much for your help!
function custom_wc_get_sale_price( $sale_price, $product ) {
if (isset($_GET['sale'])) {
return $sale_price;
} else {
return $product->get_regular_price();
}
}
add_filter( 'woocommerce_product_get_sale_price', 'custom_wc_get_sale_price', 50, 2 );
add_filter( 'woocommerce_product_get_price', 'custom_wc_get_sale_price', 50, 2 );
You need to set this url variable to WC session to avoid loosing it when Url change:
// Early enable guest customer WC_Session
add_action( 'init', 'wc_session_enabler' );
function wc_session_enabler() {
if ( is_user_logged_in() || is_admin() )
return;
if ( isset(WC()->session) && ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
}
// Get "sale" url variable and set custom WC session variable
add_action( 'init', 'enable_sale_to_wc_session' );
function enable_sale_to_wc_session() {
if ( isset($_GET['sale']) ) {
WC()->session->set('has_sale', '1');
}
}
// Enable sale price if it's set on products when 'sale' is as an URL variable
add_filter( 'woocommerce_product_get_price', 'enabling_sale_price', 1000, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'enabling_sale_price', 1000, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'enabling_sale_price', 1000, 2 );
add_filter( 'woocommerce_product_variation_get_sale_price', 'enabling_sale_price', 1000, 2 );
function enabling_sale_price( $price, $product ) {
if ( isset($_GET['sale']) || WC()->session->get('has_sale') ) {
return $price;
} else {
return $product->get_regular_price();
}
}
// Remove custom WC session variable on thankyou page
add_action( 'woocommerce_thankyou', 'remove_enable_sale_from_wc_session' );
function remove_enable_sale_from_wc_session() {
if ( WC()->session->get('has_sale') ) {
WC()->session->__unset('has_sale');
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
In Woocommerce, I have a function that replace add to cart button by a linked button to the product in shop and archive pages:
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! current_user_can('customer') ) {
$link = get_permalink($product_id);
$button_text = __( "View product", "woocommerce" );
$html = ''.$button_text.'';
}
return $html;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );
I would like to remove the add to cart button on all pages if a user is not logged in as a customer.
Can anyone help please?
Instead of your actual code, try the following that will do everything everywhere and will remove add to cart button when user is not logged in:
add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
if ( ! is_user_logged_in() )
$purchasable = false;
return $purchasable;
}
Code goes in function.php file of your active child theme (or active theme).
I have some code that is blocking the price from being shown on all products, if the user is not logged in.
My issue is that I have 1 product that is free, and I need the price to be shown, if the user is not logged in.
Can someone help me target that single product by his id and show this particular price even if the user is not logged in.
Here is my original php code in funcions.php which blocks the price from being shown, when a user is not logged in:
// Hide prices on public woocommerce (not logged in)
add_action('after_setup_theme','activate_filter') ;
function activate_filter(){
add_filter('woocommerce_get_price_html', 'show_price_logged');
}
function show_price_logged($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_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
return 'Call for pricing';
}
}
Thanks.
Cleaned up a bit and abstracted your conditional test to a custom function called so_42075748_hide_price() which tests if the user is logged in and tests if the product's price is greater than zero. I've also filtered woocommerce_is_purchasable to make these products completely not purchasable to someone who may know that you can simply do ?add-to-cart=99 to add a product to the cart.
// Switch the Price HTML
add_filter('woocommerce_get_price_html', 'so_42075748_hide_price_logged', 10, 2 );
function so_42075748_hide_price_logged( $price, $product ){
if( so_42075748_hide_price( $product ) ){
// Not the ideal permalink in my opinion, but copying from original question.
$price = '' . __( 'Call for pricing', 'your-textdomain' ) . '';
}
return $price;
}
// Make products completely unpurchasable
add_filter( 'woocommerce_is_purchasable', 'so_42075748_is_purchasable', 10, 2 );
function so_42075748_is_purchasable( $purchasable, $product ){
if( so_42075748_hide_price( $product ) ){
$purchasable = false;
}
return $purchasable;
}
// Hide add to cart buttons
add_action( 'woocommerce_before_shop_loop_item', 'so_42075748_hide_prices' );
add_action( 'woocommerce_before_single_product', 'so_42075748_hide_prices' );
function so_42075748_hide_prices(){
global $product;
if( so_42075748_hide_price( $product ) ){
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 );
}
}
Edited: Made a more complete conditional test that also tests if the price is null.
// Your condition for hiding products
function so_42075748_hide_price( $product = null ){
if( ! is_object( $product ) ){
return true;
}
if( ! is_user_logged_in() ){
$price = $product->get_price();
if( ! is_null( $price ) && $price > 0 ){
return true;
}
}
return false;
}
I want to hide the button on my shop pages, but I would like to show it on other posts and pages.
I've found this code to hide the add to cart button on my whole website:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
How can I tweak it, so it only removes the button on woocommerce shop and catagory pages?
You can use the Woocommerce conditional tags to check:
http://docs.woothemes.com/document/conditional-tags/
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
This could also be done with CSS by targeting the relevant classes:
.cart{display:none;}
.avia_cart_buttons{display:none;}
In my case there is that avia because i use Enfold Theme. With inspect element find out your class where the buton is located. and declare it invisible.
Another example is:
.woocommerce .products .shop-column.product-hover-style-2 .product-content
.product-add-to-cart-btn{
display:none !important;
}
add_action('wp','only_add_bierkoerier_in_cart', 'woocommerce_before_cart');
function only_add_bierkoerier_in_cart() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$bierkoerier_in_cart = false;
$categories = get_categories();
if ( has_term( 'bierkoerier', 'product_cat', $cart_item['product_id'] ) ) {
$bierkoerier_in_cart = true;
break;
}
}
if($bierkoerier_in_cart) {
wc_print_notice( 'omdat u bierkoerier producten in uw winkelwagen heeft
kunt u geen winkelitems toevoegen', 'notice' );
if(is_shop() || is_product() || is_product_category()) {
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 );
}
}
}
To remove the "Add to cart" button
You need to use hook which not affect other code-
add_action( 'woocommerce_after_shop_loop_item', 'remove_loop_button', 1 );
function remove_loop_button()
{
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
}
}
this will remove add to cart button from shop/category pages .
Here you can get WooCommerce Action and Filter Hook
-https://docs.woothemes.com/wc-apidocs/hook-docs.html
Here is a plugin you can use to remove hide disable add to cart button https://wordpress.org/plugins/woo-options/
That's quite simple as i have gone through several tutorials when i was trying to fix it . You have to just put this code in woocommerce.php to hide add to cart button for shop page.
function WpBlog() {
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');
return WooCommerce::instance();
}
Hope that would work for you, if not let me know i will guide you
To remove add to cart buttons from shop, product category, and single product pages, use below steps:
Locate the functions.php in child theme. Child theme prevents changes been overwritten by WP updates.
https://www.dreamhost.com/wordpress/create-woocommerce-child-theme/
Put below code in functions.php:
add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
function remove_add_to_cart_buttons() {
if( is_product_category() || is_shop()) {
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
}
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_before_cart' );
function woocommerce_before_cart() {
if( is_product()) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart',30);
}
}