0

My problem is simple, I just don't want user to shop unless and until user is not logged in. I also want to show specific message "Please Log In First" instead of price or add to cart button on the single product page.

I can hide price and add to cart functionality by using...

if (!is_user_logged_in()) {
        //Remove single product add to cart            
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30);

        //Remove single product price
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);

        //Remove loop add to cart
        remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');

        //Remove loop price
        remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
    }

How can I show message "Please log in first" on single product page instead of add to cart button?

closed as off-topic by Pieter Goosen, Mark Kaplun, TheDeadMedic, Nicolai, Howdy_McGee Dec 24 '15 at 21:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

0

Just put some conditionals where you render your button. You'll also need to figure out which page you're on.

if($is_single_product_page) {

    if( ! is_user_logged_in()) { ?>

        <div class="message">Please log in first</div>

    <?php } else { // end is_user_logged_in/false 
    ?>

        <button class="btn">add to cart</button>

    <?php } // end is_user_logged_in/true
}
  • The student has become the teacher – jgraup Dec 23 '15 at 7:56
0

I would do this first: In your site's admin go to WooCommerce>Settings>Checkout ex: WooCommerce require registration to buy

Then I would add what @jgraup suggested.

0

It was so easy. I was looking solution like this...

add_action('woocommerce_single_product_summary', 'wl8ShowCustomMsgToGuest', 10);

function wl8ShowCustomMsgToGuest(){
        //
        if (!is_user_logged_in()) {
            $wc_ac_url = get_permalink(get_option('woocommerce_myaccount_page_id'));
            $msg = __('Please log in to see the price.', 'wdmacspc');
            echo "<a href='$wc_ac_url' class='button alt wl8-custom-btn'>$msg</a>";
        }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.