user3696585
user3696585

Reputation: 49

Conditionally removing add-to-cart button and hiding prices for product categories

In WooCommerce, I am trying to remove Add-to-cart button from my homepage slider based on certain product categories and hiding the prices.

I have tried to use that code:

/* Code for authorisation */

function show_price_logged($price){
if(! is_user_logged_in()) {

        if(has_term( 'teamine', 'product_cat' )){  /* sub categories need not authorisation, but thier parent category needs authorisation */
            return $price;
        }
        if(has_term( 'epicuren', 'product_cat' ) || has_term( 'revision', 'product_cat' ) || has_term( 'image-2', 'product_cat' )){  /* categories needs authorisation */
            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_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );    
            remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
            return '<a class="amount authorizelink" href="/how-to-get-authorized-its-easy">Authorization needed to purchase <span>Click Here</span></a>';
    }
        if(has_term( 'skinceuticals', 'product_cat' ) || has_term( 'lira', 'product_cat' )){
        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_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );    
            remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
            return '<a href="www.radiantskincarespa.com">Please contact the Spa to arrange a consultation and to purchase this product <span></span></a>';
    }
    else{
        return $price;
    }
}
else return $price;
}

But it's not working as expected and add-to-cart buttons are not removed when conditions are matched.

Any help will be appreciated. How can I achieve this?

Thanks.

UPDATED

screenshot of error

Upvotes: 2

Views: 1529

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253919

As you don't provide any hook for your function usage, to test it and make it work I have used here woocommerce_price_html and woocommerce_sale_price_html filters hooks (for simple products).

I have changed a little bit your code and it is working successfully now:

add_filter('woocommerce_price_html','show_price_logged', 10, 2 );
add_filter('woocommerce_sale_price_html','show_price_logged', 10, 2 );

function show_price_logged( $price, $product ){

    if( !is_user_logged_in() && !has_term( 'teamine', 'product_cat', $product->id ) ){

        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_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
        remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );

        if( has_term( 'epicuren', 'product_cat', $product->id ) || has_term( 'revision', 'product_cat', $product->id ) || has_term( 'image-2', 'product_cat', $product->id ) )
        {
            $price = '<a class="amount authorizelink" href="/how-to-get-authorized-its-easy">Authorization needed to purchase <span>Click Here</span></a>';
        }
        elseif( has_term( 'skinceuticals', 'product_cat', $product->id ) || has_term( 'lira', 'product_cat', $product->id ) )
        {
            $price = '<a href="www.radiantskincarespa.com">Please contact the Spa to arrange a consultation and to purchase this product <span></span></a>';
        }
        else // set here another text + link in case of…
        {
            $price = '<a href="#">LINK </a><span>NON LOGGED USER / NO TARGETED CATEGORY</span>';
        }
    }

    return $price;
}

I have add a final condition (in case of) that will need a custom text + link…

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works, it should also work for you.


Update: See the result here on a test server:

SHOP PAGE (OR ARCHIVES):

PRODUCTS ON SHOP PAGE

PRODUCT PAGES:

PRODUCT PAGES

This just works perfectly…

Related to your error in the comments:
Warning: Missing argument 2 for show_price_logged() …
:

This means that you have forget the $product argument in four function. you need to have this:

function show_price_logged( $price, $product ){

Instead of this:

function show_price_logged( $price ){

As you are declaring 2 arguments at the end of your add_filter() hooks…

Upvotes: 4

Related Questions