Woocommerce Set Billing Address Optional - wordpress

I redesigned my Woo checkout page to only show the billing address fields when the Stripe gateway is selected, otherwise if the cheque gateway is selected I do not want to collect their billing address. I have the template finished and the jQuery calls work like a charm. However, I'm having problems making the billing address fields optional and the shipping address fields required.
Here's what I have so far...I added the following filter to make both address fields not-required:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields');
function custom_override_default_address_fields( $fields ) {
// add or remove billing fields you do not want
$keys = array(
'first_name',
'last_name',
'company',
'address_1',
'address_2',
'city',
'postcode',
'state',
'country'
);
foreach( $keys as $key ) {
$fields[$key]['required'] = false;
}
return $fields;}
I remove this filter when the Stripe gateway is selected using jQuery so that the billing address fields show as required (and re-apply if cheque is selected), this behavior works for the billing address fields.
I now need to make the shipping address fields to show as required, so I tried adding the following filter:
add_filter( 'woocommerce_checkout_fields', 'set_shipping_required_fields',9999 );
function set_shipping_required_fields( $fields ) {
// add or remove billing fields you do not want
$shipping_keys = array(
'first_name',
'last_name',
'address_1',
'city',
'postcode',
'state',
);
foreach( $shipping_keys as $key ) {
$fields['shipping']['shipping_'.$key]['required'] = true;
}
return $fields;
}
This successfully will display the shipping_first_name and shipping_last_name as required, however, the rest of the shipping fields initially render as required, but a split second later will revert back to being optional. Does anyone have any tips what I'm doing wrong to make the billing address fields optional and shipping address fields required? Is there a better way?

ou should need to use woocommerce_default_address_fields filter hook instead as explained here.
/ Billing and shipping addresses fields
add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
function filter_default_address_fields( $address_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $address_fields;
// All field keys in this array
$key_fields = array('country','first_name','last_name','company','address_1','address_2','city','state','postcode');
// Loop through each address fields (billing and shipping)
foreach( $key_fields as $key_field )
$address_fields[$key_field]['required'] = false;
return $address_fields;
}
As billing email and phone are already required by default, if you want them to be not required, you should need this additional code:
// For billing email and phone - Make them not required
add_filter( 'woocommerce_billing_fields', 'filter_billing_fields', 20, 1 );
function filter_billing_fields( $billing_fields ) {
// Only on checkout page
if( ! is_checkout() ) return $billing_fields;
$billing_fields['billing_phone']['required'] = false;
$billing_fields['billing_email']['required'] = false;
return $billing_fields;
}
All code goes in function.php file of your active child theme (or active theme). Tested and works.

Related

Disable WooCommerce new order email notification when shipping method is NOT "Local Pickup"

I am trying to send the Woocommerce New Order email ONLY when the customers has selected "Local Pickup" as shipping method.
In order to achieve that i disabled the new order email notification in the WooCommerce settings while trying to enabled the new order email with a filter if the condition shipping method = local pickup is met.
So far i was unable to pull it off. Here is my filter:
add_filter( 'woocommerce_email_enabled_new_order', 'lds_only_send_mail_when_shipping_is_pickup', 10, 2 );
function lds_only_send_mail_when_shipping_is_pickup($order) {
if (!empty($order) && $order->get_shipping_method() === 'local_pickup') {
return true;
}
}
Not sure what I am doing wrong?
As the first parameter of woocommerce_email_enabled_{id} is optional, i thought it would be enough to just return true when the condition is met.
No need to make any changes to WooCommerce settings
To disable WooCommerce new order email notification if order shipping method is NOT equal to "Local Pickup", you can use the woocommerce_email_recipient_new_order hook
So you get:
function filter_woocommerce_email_recipient_new_order( $recipient, $order = false ) {
if ( ! $order || ! is_a( $order, 'WC_Order' ) ) return $recipient;
// Get shipping method
$shipping_method = $order->get_shipping_method();
// NOT equal (Note: this should be adjusted to the shipping method in your site language)
// Such as: 'Afhalen' for dutch, etc...
if ( $shipping_method != 'Local Pickup' ) {
$recipient = '';
}
return $recipient;
}
add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient_new_order', 10, 2 );

WooCommerce - get shipping country for non registered user

Good morning everybody.
I need to implement a method to subtract shipping costs from the total in case the shipping destination is included in a specific array of values.
This is not a case of free shipping, because this costs will be added later for other reasons.
I cannot base my decision on the user country, for two reasons:
user can be not registered
user country and shipping country can be different.
I see that WooCommerce reload the order totals when I change billing/shipping country. I believe I need to intercept this kind of change an trigger an action to insert a new cart fee (a negative one, of course).
Well, how can I do that?
This is a part of my code
function delayShippingCosts(){
global $woocommerce;
$EUcountries = ['IT','AT','BE','BG','CY','HR','DK','EE','FI','FR','DE','GR','IE','LV','LT','LU','MT','NE','PL','PT','CZ','RO','SK','SI','ES','SE','HU'];
return in_array( $woocommerce->customer->get_country() , $EUcountries);
}
add_action( 'woocommerce_cart_calculate_fees', 'scc_detract_shipping_costs' );
function scc_detract_shipping_costs(){
global $woocommerce;
if(delayShippingCosts()){
$shippingCosts = WC()->cart->get_shipping_total() * -1;
if(current_user_can('administrator')) {
$woocommerce->cart->add_fee( 'Delayed shipping costs', $shippingCosts, true, 'standard' );
}
}
}
The problem is that now I'm looking to my customer data, and these are not dynamic (and void for unregisterd / unlogged users).
Any suggestions?
Thanks!!
EDIT
Almost OK
I managed to retrieve the shipping country from "woocommerce_checkout_update_order_review" hook, like that:
function action_woocommerce_checkout_update_order_review($posted_data) {
global $shipTo;
$data = array();
$vars = explode('&', $posted_data);
foreach ($vars as $k => $value){
$v = explode('=', urldecode($value));
$data[$v[0]] = $v[1];
}
WC()->cart->calculate_shipping();
$shipTo = $data['shipping_country'] ? $data['shipping_country'] : $data['billing_country'];
// REMOVE ALL NOTICES, IF PRESENTS...
wc_clear_notices();
}
add_action('woocommerce_checkout_update_order_review', 'action_woocommerce_checkout_update_order_review', 10, 1);
add_action( 'woocommerce_cart_calculate_fees', 'scc_detract_shipping_costs' );
function scc_detract_shipping_costs(){
global $woocommerce;
... something ...
if(condition) {
wc_add_notice("info message", "error");
}
}
My problem is that the notice is not removed when "condition" in false.
I tried to call wc_remove_notices() both in woocommerce_cart_calculate_fees and woocommerce_checkout_update_order_review. No big difference! :(
Any hint?
The delayShippingCosts() function is not needed. You can get the list of European countries via the get_european_union_countries method of the WC_Countries class (unless you want to customize the list).
Also you are getting the country with the get_country() method of the WC_Customer class.
The WC_Customer::get_country function is deprecated since version 3.0.
You can get the country like this:
WC()->customer->get_shipping_country() the country of the shipping address
WC()->customer->get_billing_country() the country of the billing address
Finally, note that to apply the standard tax class for the fee you have to set the 4th parameter as an empty string instead of 'standard'. See here for more information.
TO GET THE COUNTRY FIELD FROM A USER NOT LOGGED IN WITH THE woocommerce_cart_calculate_fees HOOK
You can send an AJAX call to send the billing and shipping country value when the respective fields change in the checkout.
To make sure that the AJAX function is executed before the woocommerce_cart_calculate_fees hook it is necessary to remove the update_totals_on_change class from the billing and shipping country fields (to avoid the AJAX call being made to update the checkout) and update the checkout only after the call AJAX has been completed.
This method may take a few extra milliseconds/second to update the checkout because you have to wait for the AJAX call to create the option to complete.
See this answer for more details on how to submit an AJAX call in Wordpress.
Add the following code inside your active theme's functions.php:
// enqueue the script for the AJAX call
add_action('wp_enqueue_scripts', 'add_js_scripts');
function add_js_scripts(){
wp_enqueue_script( 'ajax-script', get_stylesheet_directory_uri().'/js/script.js', array('jquery'), '1.0', true );
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
// update options with checkout country values
add_action( 'wp_ajax_nopriv_set_option_country', 'set_option_country' );
add_action( 'wp_ajax_set_option_country', 'set_option_country' );
function set_option_country() {
if ( isset( $_POST ) ) {
// get the countries valued in the checkout by the user (guest or logged in)
$countries = $_POST['countries'];
$billing_country = $countries['billing_country'];
$shipping_country = $countries['shipping_country'];
// update options
update_option( 'guest_billing_country', $billing_country );
update_option( 'guest_shipping_country', $shipping_country );
// returns the output as a response to the AJAX call
echo 'success';
}
// always die in functions echoing AJAX content
die();
}
Create a script.js file and add it inside your child theme (because I used get_stylesheet_directory_uri() instead of get_template_directory_uri()) in the directory: /child-theme/js/script.js:
jQuery(function($){
// disable AJAX update
$('#billing_country_field').removeClass('update_totals_on_change');
$('#shipping_country_field').removeClass('update_totals_on_change');
// when the country fields change
$('#billing_country, #shipping_country').change(function(){
var countries = {
'billing_country': $('#billing_country').val(),
'shipping_country': $('#shipping_country').val(),
};
$.ajax({
url: ajax_object.ajaxurl,
type : 'post',
data: {
'action': 'set_option_country',
'countries': countries
},
complete: function(){
// update checkout via AJAX
$(document.body).trigger('update_checkout');
},
success:function(data) {
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
});
The code has been tested and works.
So, the correct scc_detract_shipping_costs function will be:
add_action( 'woocommerce_cart_calculate_fees', 'scc_detract_shipping_costs' );
function scc_detract_shipping_costs(){
$countries = new WC_Countries();
// get the list of countries of the european union
$eu_countries = $countries->get_european_union_countries();
// get countries from checkout
$billing_country = get_option( 'guest_billing_country' );
$shipping_country = get_option( 'guest_shipping_country' );
// if the shipping country is part of the European Union
if ( in_array( $shipping_country, $eu_countries ) ) {
$shippingCosts = WC()->cart->get_shipping_total() * -1;
if ( current_user_can('administrator') ) {
WC()->cart->add_fee( 'Delayed shipping costs', $shippingCosts, true, '' );
}
}
}
The code has been tested and works. Add it to your active theme's functions.php.
TO GET THE COUNTRY FIELD FROM A USER NOT LOGGED IN WITH THE woocommerce_calculate_totals HOOK
add_action( 'woocommerce_calculate_totals', 'get_post_checkout_data' );
function get_post_checkout_data( $cart ) {
// get post data
if ( isset( $_POST['post_data'] ) ) {
parse_str( $_POST['post_data'], $post_data );
} else {
$post_data = $_POST;
}
if ( ! empty( $post_data ) ) {
$billing_country = $post_data['billing_country'];
$shipping_country = $post_data['shipping_country'];
// ...
}
}
Add the code in your active theme's functions.php.

Woocommerce Get Vendor Information with Product ID

I am using Woo Commerce with WC Vendor and WC Booking plugin. I want to send booking notification to vendor. Currently it sends notification to Customer and Administrator and when admin changes product status to processing & completed, then it sends notification to vendor. However, I want to send vendor notification along with admin notification.
I tried this hook:
add_action( 'woocommerce_new_booking', 'new_order_email_to_vendor', 10, 4 );
function new_order_email_to_vendor( $order ){
$emails = WC()->mailer()->get_emails();
if ( ! empty( $emails ) ) {
$emails['WC_Product_Vendors_Order_Email_To_Vendor']->trigger( $order );
}
}
But it throws an error:
Fatal error: Call to a member function get_date_created() on boolean in /wp-content/plugins/woocommerce-product-vendors/includes/emails/class-wc-product-vendors-order-email-to-vendor.php on line 56
So, now I am trying to hook vendor email directly along with customer and administrator email in this line:
$this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ), 'WILL ADD VENDOR EMAIL HERE' );
in file:
wp-content/plugins/woocommerce-bookings/includes/emails/class-wc-email-new-booking.php
At this stage, I have bookable product id available and I am trying to pull vendor information using product id but I don't found any information.
I tried:
get_post()
get_post_meta()
get_post_meta_by_id()
The Question is: How to get vendor information (specifically email) using product id?
I don't have and I have never used WC Vendors plugin as this is a non official commercial plugin (not made by automatic).
To get the vendor ID (after searching a bit) you can get it this way from a product ID:
$vendor_id = get_post_field( 'post_author', $product_id );
Now you can get the vendor email this way:
$vendor_id = get_post_field( 'post_author', $product_id );
$vendor = get_userdata( $vendor_id );
$email = $vendor->user_email;
May be a good turn around:
You can use the dedicated filter hook woocommerce_email_recipient_{$this->id} where $this->id is the ID of the notification type, for new_booking email ID (and also for testing new_order email ID too).
This will allow you to add additional email recipients.
In email notifications hooks, the Order object is nearly always defined. As In an order you can have many items (different products rom different vendors), you will need to get the vendor ID from each.
In the code below I add to the recipients the vendors emails for new_booking and new_order email notifications:
add_filter( 'woocommerce_email_recipient_new_booking', 'additional_customer_email_recipient', 10, 2 );
add_filter( 'woocommerce_email_recipient_new_order', 'additional_customer_email_recipient', 10, 2 ); // Optional (testing)
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
$additional_recipients = array(); // Initializing…
// Iterating though each order item
foreach( $order->get_items() as $item_id => $line_item ){
// Get the vendor ID
$vendor_id = get_post_field( 'post_author', $line_item->get_product_id());
$vendor = get_userdata( $vendor_id );
$email = $vendor->user_email;
// Avoiding duplicates (if many items with many emails)
// or an existing email in the recipient
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
// Convert the array in a coma separated string
$additional_recipients = implode( ',', $additional_recipients);
// If an additional recipient exist, we add it
if( count($additional_recipients) > 0 )
$recipient .= ','.$additional_recipients;
return $recipient;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This should work without errors.

Woocommerce skip checkout for free products

I'm building webpage on WP and Woocommerce - I would like to skip cart and also checkout page for free products (or products which ID-s I can specify). These products are free and virtual (no payment needed, no shipping needed). The webpage is only used by registered users - so all the customer info is present.
The result I would like to have is that if you press ORDER button on product page - the order is done and customer is redirected to Thank-You page.
BR,
Kaspar
I applied the same concept, but found a major issue when processing order on the checkout; fields were still required.
The primary issue was processing the order via AJAX ( was using is_ajax() ), and even though it was on the checkout page, it wasn't returning as true. It's possible there was a recent change, or it could be the site's environment (theme).
Here are some of the conditional tags: https://docs.woocommerce.com/document/conditional-tags/
Seeing how things change, the answer can be edited here, but the original concept is located at: https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/
function wc_free_checkout_fields() {
// Bail we're not at checkout, or if we're at checkout OR AJAX (payment process) but payment is needed.
if ( function_exists( 'is_checkout' ) && ( ! ( is_checkout() || is_ajax() ) || ( ( is_checkout() || is_ajax() ) && WC()->cart->needs_payment() ) ) ) {
return;
}
// Remove coupon forms since it's irrelevant with a free cart?
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
// Remove the "Additional Info" order notes.
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
// Unset the fields we don't want in a free checkout.
function wc_unset_unwanted_checkout_fields( $fields ) {
// Add or remove billing fields you do not want.
// #link http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
$billing_keys = array(
'billing_company',
'billing_phone',
'billing_address_1',
'billing_address_2',
'billing_city',
'billing_postcode',
'billing_country',
'billing_state',
);
// For each unwanted billing key, unset.
foreach( $billing_keys as $key ) {
unset( $fields['billing'][$key] );
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'wc_unset_unwanted_checkout_fields' );
// A tiny CSS tweak for the account fields; this is optional.
function wc_print_custom_css() {
?>
<style>
.create-account {
margin-top: 6em;
}
</style>
<?php
}
add_action( 'wp_head', 'wc_print_custom_css' );
}
add_action( 'wp', 'wc_free_checkout_fields' );
Check if the checkout has no cost with the WC()->cart->needs_payment() check.
see this for more info:
https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/

Adding extra info to order

I am programmatically adding a product to the cart. Besides this, somehow, I want to store some extra info (an array) to the order. When the client finishes the order, I want to access that info through some WordPress actions. I'll have to do this immediately after adding the product to the cart, because the info may change after that, if the user doesn't finish the order right away. Is there any way I can do it, without putting the database to work?
You should probably use the WooCommerce Cart Item Meta API and the WooCommerce Order Item Meta API.
You use them like this:
// Add to cart item
// This is triggered on add to cart
add_filter('woocommerce_add_cart_item_data', 'my_add_cart_item_data', 10, 2);
function my_add_cart_item_data( $cart_item_meta, $product_id ) {
//Here we can easily filter what values should be added to what products using the $product_id
$cart_item_meta['my_meta_key'] = 'meta value';
return $cart_item_meta;
}
// Add to order item when the cart is converted to an order
// This is triggered when the order is created
add_action('woocommerce_add_order_item_meta', 'my_order_item_meta'), 10, 2);
function my_order_item_meta( $item_id, $values, $cart_item_key ) {
// The value stored in cart above is accessable in $values here
woocommerce_add_order_item_meta( $item_id, 'meta_key', $values['my_meta_key'] );
//Or add what ever you want
$meta_value = 'value';
woocommerce_add_order_item_meta( $item_id, 'meta_key', $meta_value );
}
I hope that helps.

Resources