How to autofill WooCommerce Checkout Address Box - wordpress

I know there must be a solution to this I just can’t find it.
I want to have when users go to the Checkout page they can either click a button or select a checkbox for a certain predefined address and it will auto fill the address into the Shipping address box.
So meaning the user clicks <button> and it auto fills form with 123 Street, NY NY, 11111
Anyone have any idea how to do this?

Okay so I solved this (kinda)
On the Cart & Checkout Page: As I thought I didn't know WooCommerce well enough, so I changed it so now I have a Shipping Zone USA and whithin that I have 2 Shipping Types, the Free Shipment and a Flat Rate to anywhere else
On the Checkout Page I set a custom Javascript that when you click the button it auto fills the address in
Inside my functions.php
add_action( 'woocommerce_before_checkout_form', 'wnd_checkout_message', 10 );
function wnd_checkout_message( ) {
echo '<div class="wnd-checkout-message"><h3>Sending to X? Shipping is free!</h3><button onclick="changeShippingAddr();">Click here to ship</button></div><br />
';
And then on my actual Checkout Page I have a JavaScript:
<script>
function changeShippingAddr(){
document.getElementById("shipping_first_name").value = document.getElementById("billing_first_name").value;
document.getElementById("shipping_last_name").value = document.getElementById("billing_last_name").value;
document.getElementById("shipping_company").value = "COMPANY";
document.getElementById("shipping_country").value = "COUNTRY";
jQuery('body').trigger('update_checkout');
document.getElementById("shipping_state").value = "STATE";
jQuery('body').trigger('update_checkout');
document.getElementById("shipping_address_1").value = "ADDR";
document.getElementById("shipping_city").value = "CITY";
document.getElementById("shipping_postcode").value = "ZIP";
jQuery('body').trigger('update_checkout');
document.getElementById("select2-shipping_state-container").innerHTML= "State Name";
}
</script>
Which essentially sets the Name from Billing to Shipping and inserts a default Address so you don't need to type it in.
Thanks to all the helped out!

Related

Add a second featured image and field name, later display it before add to cart button

I'm building a property buying and selling site for agents using wordpress and woocommerce.
I'm using the oceanwp theme.
The targets is to show the agent's photo and name before the add to cart button.
(button has been changed to whatsapp and tel contact).
What I've tried to do :
Add product attributes which contain photo link addresses from each agent.
Adding product attributes containing the name of each agent.
The code snippet I use is as follows :
add_action('woocommerce_before_add_to_cart_button','misha_before_add_to_cart_btn' );
function misha_before_add_to_cart_btn(){
global $product;
$photo_val = $product->get_attribute('pa_photo');
print '<img src='.$photo_val.' style="width: 75px;height: 75px;border-radius: 50%;margin-left: 0px;margin-right: auto;display: block;margin-bottom:10px; box-shadow:0px 0px 3px 0px rgba(9, 8, 8, 0.7);"/>';
$agent_val = $product->get_attribute('pa_agent');
echo "<p style='margin-left:5px;font-size:20px;'>".$agent_val."</p>";
}
The display is exactly what I wanted. (See)
But I noticed this method is less effective because it has to do many steps to achieve it.
So I thought, is there a way to add a custom photo (upload) and name (input) for the agent in the admin panel which is automatically displayed before the add to cart button without using an additional plugin?
Example of the desired view on the admin panel (see)
Example of the desired display on single product page (see)
How can I achieve that?
Any help will be greatly appreciated.
You can use this action hook woocommerce_before_add_to_cart_button to add any content just before add to cart button on a single product page.
Sample code that can go in your functions.php if you are editing a theme:
add_action( 'woocommerce_before_add_to_cart_button', 'content_before_addtocart' );
function content_before_addtocart() {
echo '<img src="https://picsum.photos/150/150?random=1">';
}

Access WooCommerce custom post field in same action as send email

Before this gets flagged as a duplicate of Save custom post fields value before Woocommerce email send I will point out that the answer given to that question is a) not accepted and b) refers to a hook that is no longer in the documentation.
I'm helping out a friend with their WordPress WooCommerce site, and they want to be able to add a tracking number to the email that gets automatically sent out to a customer once the order status is set to "Completed". I've managed to get this all working using the Advanced Custom Fields plugin and some code in functions.php. The problem I have is the user friendliness isn't quite there when it comes to processing the order, it being a double-barrelled operation:
Fill in the tracking number custom field on the Edit Order screen and then click the Update button
Set the status to Completed and the click the Update button again
Ideally, it would be nice to fill in the tracking number text field and change the status dropdown value to Completed, and then hit update. If that is done though, the email doesn't get the custom field populated, presumably because the custom fields get saved after the email gets triggered.
Is there a hook in WooCommerce that allows, on update of the order, saving of custom fields to occur before triggering the email?
For reference, this is my current code in functions.php, where (despite changing the priority of the operation to 1) it doesn't populate the tracking number on the email:
add_action( 'woocommerce_email_before_order_table', 'add_content_specific_email', 1, 4 );
function add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( $email->id == 'customer_completed_order' ) {
$ausPostTrackingNumber = get_post_meta( $order->get_id(),'australia_post_tracking_number',true);
if($ausPostTrackingNumber != '') {
echo '<p class="email-auspost-text">Your Australia Post tracking number is ' . $ausPostTrackingNumber . '</p>';
}
}
}

Woocommerce how to click a button to add to cart and checkout?

Objective:
I would like the customer to click on a button, add an item with quantity = 1 to cart, and route to checkout page automatically.
What I did:
I'm using Elementor to add a button with a href value of:
https://fakeurl.com/checkout/?add-to-cart=59
Problem:
Once I click the button, it will route to the checkout page, however it will add 2 quantity instead of one to the cart.
What I've tried:
Explicitly specify the quantity count in the href:
https://fakeurl.com/checkout?add-to-cart=59&quantity=1
But I'm getting the same results.
My checkout page is just simple page with 2 shortcodes namely woocommerce_cart & woocommerce_checkout:
Any idea why? Do I need to empty the cart before the aforementioned button is pressed?
Use your link structure as you already do > ?add-to-cart=59&quantity=1 and add below code in functions.php in your theme to just do checking
the only thing this peace of code do is to loop your cart to see if this product is already there .. and if it is - it sets $valid var on false
function is_product_in_cart( $valid, $product_id, $quantity) {
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0) return true;
foreach ( $woocommerce->cart->get_cart() as $key => $values ) {
$_product = $values['data'];
$id = $_product->id ;
if( $product_id == $id ) $valid = false;
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_in_cart', 10, 3 );
Woocommerce default flow is that it will add the quantity to the cart whenever you add an item that is already in the cart.
Empty your cart whenever a new product is added to the cart so that only one remains in the cart.
In most cases the quantity is doubled because you are being redirected.
So the quantity is added and after the redirect the quantity is added again.
The reasons for this could be a couple of the things.
Theme or wordpress permalinks setting are adding or removing // in the links
Your checkout page is not set in woocommerce settings (woocommerce->advanced settings->Page settings)
How to check if you are being redirected (chrome)
At the top of Chrome's inspector (in the Network tab) is a checkbox which says Preserve log. Enable this option. Now it doesn't matter at all how the page navigates, the inspector will keep all log history -- including the redirect response.
(found here: See full redirect path and HTTP status code in Chrome)
Possible solutions
“Enable AJAX add to cart buttons on archives” (WooCommerce –> Settings –> Products -> General)
found here (https://www.businessbloomer.com/woocommerce-custom-add-cart-urls-ultimate-guide/)
This sometimes help when having custom pages for cart and checkout.
Disable "Redirect to the cart page after successful addition" under WooCommerce > Settings > Products.Because you are linking to the checkout page, this wil redirect you again to the cart page.

How to reset all fields when adding to WooCommerce Cart

I'm developing a site with WooCommerce and have used the plugin Product Addons to add extra fields of text to the item being purchased (name, email address, phone number, etc). When someone clicks on "Add to Cart", the site currently adds the product to the cart, refreshes the page, but the previous data remains in the fields. I want to reset all the fields and make them empty after the product has been added.
I tried using this function:
( function($) {
$( document.body ).on( 'added_to_cart', function() {
$('input').val(''); }
);
} ) ( jQuery );
Any suggestions?
If the page literally refreshes itself then it is not at all standard. It was done just to update the mini cart at the top right corner of your menu and products are being added through ajax. In this case you can't empty all fields on some event because the page is being refreshed, what you have to do is write you code under document.ready function, perform $.each function on the common class or input and empty the input fields.
Try this action hook in your plugin or theme functions.php file
add_filter( 'woocommerce_checkout_get_value', 'misha_clear_checkout_fields_vals' );
function misha_clear_checkout_fields_vals($input){
return '';
}

Using Woocommerce Checkout for Quotes Only

How would go about using woocommerce without pricing but keeping the cart functionality? Id like the ability to add products to cart (changing button to to say add to quote) and then when checking out instead of going to payment gateway it would instead submit all the items in the cart for a quote. Once I receive the order I would contact the customer with a quote. You can edit an order in the admin so technically I could change the 0 cost items to the quoted prices and then notify the customer that their order/quote has been updated.
Could I just make all items have 0 cost and hide the prices on the front end ?
Set a price of '0' so the 'add to cart' buttons still show, but in your templates hide the price fields, and change the 'add to cart' labels to whatever is appropriate, so 'add to quote' or 'add to shortlist' or whatever you're using the cart for.
If you still want the checkout function disable all payment options except for 'cash on delivery', and change the title of this payment option to 'No Payment Required For Quotes' or similar. No payment is then required, yet the customer can create orders without paying for them
It takes some digging to find all the right filters in WooCommerce to make this happen but once you find them, it's pretty straightforward. These are the various snippets I'm using to change "Cart" language to "Quote" and streamline the checkout process:
add_filter('woocommerce_product_single_add_to_cart_text', 'rental_single_product_add_to_cart',10,2);
add_filter('woocommerce_product_add_to_cart_text', 'rental_single_product_add_to_cart',10,2);
function rental_single_product_add_to_cart( $title,$product ) {
return 'Add to Quote';
}
add_action('woocommerce_widget_shopping_cart_before_buttons', 'rental_before_mini_cart_checkout',10);
function rental_before_mini_cart_checkout(){
//change buttons in the flyout cart
echo '
<p class="buttons" style="display: block;">
Submit for Quote
</p>
';
}
add_filter('woocommerce_billing_fields','rental_billing_fields',10,1);
function rental_billing_fields($fields){
unset($fields['billing_country']);
unset($fields['billing_address_1']);
unset($fields['billing_address_2']);
unset($fields['billing_city']);
unset($fields['billing_state']);
unset($fields['billing_postcode']);
return $fields;
}
add_filter('woocommerce_checkout_fields','rental_checkout_fields',10,1);
function rental_checkout_fields($fields){
//change comment field labels
$fields['order']['order_comments']['label'] = 'Notes';
return $fields;
}
add_filter('woocommerce_order_button_text','rental_order_button_text',10,1);
function rental_order_button_text($text){
return 'Submit to Request Confirmed Quote';
}
This plus the suggestion from /u/crdunst regarding payment methods should make switching to a Quote submission a breeze!

Resources