Solution for a user who is not logged in
-
I have a website where a user has to be logged in order to download a free file as well as purchase one. By adapting two files I have created a standard Woocommerce message + login button alerting the user to log in. The Download/Add-to-Cart button has been retained but disabled and greyed out.
I put the two modified files in the child theme folder.MESSAGE
Insert this at line 38 of file /woocommerce/templates/content-single-product.php:<?php if ( !is_user_logged_in() ) { echo '<div class="woocommerce-message"> <div style="margin: 7px; float: right;"><a href="/wp-login.php" class="button wc-forward">Log in</a></div> Please log in first in order to download or purchase a product. </div>'; } ?>
DOWNLOAD/ADD TO CART BUTTON
Replace code between line 39 and 65 of file /woocommerce/templates/single-product/add-to-cart/simple.php with this:
<?php if ( $product->is_in_stock() ) : ?> <?php do_action( 'woocommerce_before_add_to_cart_form' ); ?> <?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_value' => ( isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : 1 ) ) ); } ?> <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 esc_html( $product->single_add_to_cart_text() ); ?></button> <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?> </form> <?php else: ?> <button style="margin-bottom: 30px; opacity: 0.33;" class='button alt'><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button> <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?> <?php endif; ?> <?php do_action( 'woocommerce_after_add_to_cart_form' ); ?> <?php endif; ?>
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Solution for a user who is not logged in’ is closed to new replies.