Changing the Purchase Button - Woocommerce - wordpress

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?

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.

Related

WordPress "in_widget_form" does not display basic HTML Code

I have been taking what I thought would be a simple HTML code. I tried to follow this tutorial here:
However, I am not able to get a simple input field to be added to the bottom of any widget. Could someone please tell me what I am missing?
After add below to my functions file, below does not output HTML of any thing withing wp-admin/widgets.php. Help please!
function thmfdn_add_menu_description_option( $widget, $return, $instance ) {
// Are we dealing with a nav menu widget?
if ( 'nav_menu' == $widget->id_base ) {
// Display the description option.
$description = isset( $instance['description'] ) ? $instance['description'] : '';
?>
<p>
<input class="checkbox" type="checkbox" id="<?php echo $widget->get_field_id('description'); ?>" name="<?php echo $widget->get_field_name('description'); ?>" <?php checked( true , $description ); ?> />
<label for="<?php echo $widget->get_field_id('description'); ?>">
<?php _e( 'Show descriptions', 'thmfdn_textdomain' ); ?>
</label>
</p>
<?php
}
}
add_action('in_widget_form', 'thmfdn_add_menu_description_option', 10, 3 );
My goal is to add visibility controls to widgets in order to dynamically display or high them on the frontend based on user logged in or not. To achieve that I want to extend existing widgets by adding extra settings that sets whether a widget should be rendered on frontend based on user status.
Thank you for any correction or solutions.

Entering detail information on the created order

I want to create a field where the customer can write the reason for canceling a created order. The code below appears in other orders as well, due to the customer's own knowledge. I made several attempts to register the order, but I could not get the result I wanted. I thought about "update_post_meta" and "order_id" but I guess I failed. Also, I couldn't decide where it would make sense for this area to appear on the admin page. Thanks.
// Display user custom field
add_action( 'woocommerce_order_details_before_order_table', 'add_user_custom_url_field_to_order' );
function add_user_custom_url_field_to_order( $order ) {
global $current_user;
$custom_url = get_user_meta( $current_user->ID, 'custom_URL', true );
?>
<form method="post">
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="custom_URL"><?php _e( 'URL', 'woocommerce' ); ?></label>
<input type="text" name="custom_URL" id="custom_URL" value="<?php echo $custom_url; ?>" />
</p>
<input type="submit" name="submit-custom_URL" value="<?php _e('RUN', 'woocommerce'); ?>" /><br/>
</form>
<?php
}
// Save the field as custom user data
add_action( 'template_redirect', 'save_user_custom_url_field_from_order' );
function save_user_custom_url_field_from_order() {
global $current_user;
if( isset($_POST['custom_URL']) ){
update_user_meta( $current_user->ID, 'custom_URL', sanitize_url( $_POST['custom_URL'] ) );
wc_add_notice( __("Submitted data has been saved", "woocommerce") );
}
}
Code source: https://stackoverflow.com/a/62777930/14597323

How can I hide the price's inside the Cart for unregisterd users wordpress

Basically I have downloaded a plugin to hide price's for unregistered user's. The problem is if the customer clicks on "Add to Cart" button and they go at the Cart they can see the price. Is there any setting to disable prices on cart for unregisterd users or anything related to that?
Thank's in advance
simple way to do this is overwrite cart template and check if user is login then display. I show you the logic. I hope you know how to overwrite template. still brief I say you here.
This is to overwrite cart.
woocommerce/templates/cart/cart.php to
yourtheme/woocommerce/cart/cart.php
this is for check logic
<?php if(is_user_logged_in()){ ?>
<td class="product-price" data-title="<?php esc_attr_e( 'Price', 'woocommerce' ); ?>">
<?php
echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key ); // PHPCS: XSS ok.
?>
</td>
<?php } ?>
you also need to do same for label.

Woocommerce Replace Product in Cart Upon Place Order in Checkout Page

I have been searching on the web, reading docs & stuff but I can't figure out on replacing product in Checkout Page.
For your information, my main product page is in the Home Page and each product that have been selected, will redirected to Checkout Page. Now here, there is a problem. Let me explain....
You see, I have a carousel slider in Checkout Page which user can change/replace their product (which already been added into their cart) before they pay.
form-checkout.php
global $woocommerce;
global $product;
$items = $woocommerce->cart->get_cart();
foreach ($items as &$item){
$id = $item['product_id'];
}
echo $id;
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="false">
<div class="carousel-inner" role="listbox">
<?php
// Querying of product information retrieval
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'orderby' =>'menu_order', 'order' =>'ASC');
$loop = new WP_Query( $args );
// Display each retrieved product
while ( $loop->have_posts() ) :
$loop->the_post();
// WooCommerce global product variable. Refer: https://docs.woothemes.com/document/class-reference/
global $product;
global $woocommerce;
?>
<div class="item <?php if ($product->id == $id) { ?> active <?php } ?>">
<div class="p-big" id="p-custom-color">
<strong><?php the_title(); ?></strong>
</div>
<div class="p-light-black">CANDIDATES</div>
<input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>">
</div>
<?php
endwhile;
wp_reset_query(); // After the loop ended, quit the custom loop and reset back the main loop
?>
</div>
</div>
<!-- Upon form submission -->
if (isset($_POST['woocommerce_checkout_place_order'])){
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
do_shortcode('[add_to_cart id="' . $selectedproduct . '"]'); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
}
<form name="checkout" method="post" class="checkout woocommerce-checkout" action="" enctype="multipart/form-data">
<?php if ( sizeof( $checkout->checkout_fields ) > 0 ) : ?>
<?php do_action( 'woocommerce_checkout_before_customer_details' ); ?>
<?php do_action( 'woocommerce_checkout_billing' ); ?>
<?php do_action( 'woocommerce_checkout_after_customer_details' ); ?>
<?php endif; ?>
<h3 id="order_review_heading"><?php _e( 'Your order', 'woocommerce' ); ?></h3>
<?php do_action( 'woocommerce_checkout_before_order_review' ); ?>
<div id="order_review" class="woocommerce-checkout-review-order">
<!-- Checkout Review -->
<input type="hidden" id="selectedproduct" name="selectedproductid" value="">
<?php do_action( 'woocommerce_checkout_order_review' ); ?>
</div>
<?php do_action( 'woocommerce_checkout_after_order_review' ); ?>
</form>
As you can see, in the carousel, I have included <input type="hidden" id="product" name="productid" value="<?php echo $product->id; ?>"> to get each product ID and with my jQuery (I didn't show here), I took any product ID that the product is currently on the active slide and fill it in the <input type="hidden" id="selectedproduct" name="selectedproductid" value=""> in the form.
By that, I can replace the product that have been added to cart with the selected/chosen product based on the active slide with these code (Located above the form):-
<!-- Upon form submission -->
if (isset($_POST['woocommerce_checkout_place_order'])){
global $woocommerce;
$woocommerce->cart->empty_cart(); // Empty the cart
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
do_shortcode('[add_to_cart id="' . $selectedproduct . '"]'); // Insert the selected product in the the cart
return esc_url( wc_get_checkout_url() ); // Redirect to Payment Gateway Page
}
The problem here is, it failed to replace the old product with the current chosen product and it just redirect to the payment gateway page with the old product.
I want it to replace the product with the new selected ones upon placing order. Is it possible? I hope it is, because I have been playing with WooCommerce for weeks now and I don't want my effort to be futile. Help me guys.....
After few days of figuring this out, with 30+ Chrome tabs, 50+ of purchases test & 10 gallons of coffee, finally I found the answer...
add_action('woocommerce_checkout_process', 'change_product_upon_submission');
function change_product_upon_submission() {
if ( !empty( $_POST['_wpnonce'] ) && !empty($_POST['selectedproductid']) ) {
$selectedproduct = $_POST['selectedproductid']; // Get the selected product
WC()->cart->empty_cart(); //Empty the cart
WC()->cart->add_to_cart( $selectedproduct ); // Insert the selected product in the cart
}
}
The hook required to trigger this function is within the WC_Checkout process_checkout() class in includes/class-wc-checkout.php. This woocommerce_checkout_process doesn't exist WooCommerce template files, we're gonna be thorough. So to do whatever custom stuff before sending data to the payment gateway upon place order submission, we're gonna need to manipulate the woocommerce_checkout_process hook as process_checkout() function processes the checkout after the confirm order button is pressed.
Hope this save someone's lives as I don't have any because I need to sleep after few days of burning midnight oil figuring this abomination.

WordPress WooCommerce: give another button 'add to cart' function

I'd like to give another html element the function of 'add to cart'. Is this possible? I have added some html code into single-product.php and want one of these elements to function as 'add to cart' when you click on them :)
I hope that I've been able to explain myself :P
The following will work as an add to cart button within a product loop, where $product is the post object -
<?php if( $product->is_purchasable() ):?>
<form class="cart" method="post" enctype='multipart/form-data'>
<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
<button type="submit">Add to Cart</button>
<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>
<?php endif; ?>

Resources