new_user
new_user

Reputation: 1

Changing the Purchase Button - Woocommerce

I have to hide the possibility to buy for not logged in users (whole domain or store)

Ex: If a user is NOT logged in (don't have an account) then the cart button etc will not show. Sees only a link "See store locator list".

Only if user logged in will they show prices and button "Buy" and they will have the opportunity to purchase.

example code if(is_user_logged_in() Buy

if(is_user_NOT_logged_in() Show "See store locator list" with page URL.

How to do it? Please could someone help?

I found this example: Function Reference/is user logged in Display different output depending on whether the user is logged in or not.

<?php
if ( is_user_logged_in() ) {
    echo 'Welcome, registered user!';
} else {
    echo 'Welcome, visitor!';
}
?>

Did it change in the "/WooCommerce/Templates/Loop/add-to-cart.php" enough?

But still need help :( How to put this code into "buy" button?

Upvotes: 0

Views: 181

Answers (1)

Aibrean
Aibrean

Reputation: 6422

Your code isn't exactly set up for your needs. It would be more like

<?php if (is_user_logged_in()) {?>
    <form class="cart" method="post" enctype='multipart/form-data'>
    <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>

    <?php
        if ( ! $product->is_sold_individually() )
            woocommerce_quantity_input( array(
                'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
                'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )
            ) );
    ?>

    <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />

    <button type="submit" class="single_add_to_cart_button button alt"><?php echo $product->single_add_to_cart_text(); ?></button>

    <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php } else { ?>
<div>Store Location</div>
<?php } ?>

Inside the div/form (doesn't need to be a div, but I just put there as an example) you can put whatever code you need.

I put in the form code for displaying the simple button. You would likely need this in all your add-to-cart templates. I'm also not entirely positive what is in the before/after add to cart button so those locations might need to be tweaked.

Upvotes: 1

Related Questions