With WooCommerce if a customer orders a virtual product there is no shipping address displayed on the checkout page. This makes sense as there is no physical item being posted to your customer. However, a few of my clients like to have the WooCommerce billing address match the shipping address. Subsequently, I wrote this piece of code to place in your functions.php file that will duplicate the billing to shipping address for orders containing a virtual product in WooCommerce.
The function will fire when a new customer is created on the placing of an order in WooCommerce.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
/** * Add hook to add additional user meta data on the cart page when a user is registered * Use this to copy billing address to shipping for virtual products like memberships */ add_action( 'woocommerce_created_customer', 'es_insert_member_shipping_meta' ); function es_insert_member_shipping_meta( $user_id ) { global $woocommerce; // Default virtual products number $virtual_products = 0; //Check for virtual cart products $products = $woocommerce->cart->get_cart(); // Loop through cart products foreach( $products as $product ) { // Get product ID and '_virtual' post meta $product_id = $product['product_id']; $is_virtual = get_post_meta( $product_id, '_virtual', true ); // Update $has_virtual_product if product is virtual if( $is_virtual == 'yes' ) $virtual_products += 1; } if($virtual_products > 0) { $user_info = get_userdata($user_id); $billing_company = $user_info->billing_company; $address_line_2 = $woocommerce->customer->get_address_2(); //Update all the shipping values, Address line 2 and Company Name is the only field not mandatory update_user_meta( $user_id, 'shipping_country', esc_attr($woocommerce->customer->get_country())); update_user_meta( $user_id, 'shipping_first_name', get_user_meta($user_id, 'first_name', true)); update_user_meta( $user_id, 'shipping_last_name', get_user_meta($user_id, 'last_name', true)); if ($billing_company) update_user_meta( $user_id, 'shipping_company', esc_attr()); update_user_meta( $user_id, 'shipping_address_1', esc_attr($woocommerce->customer->get_address())); if($address_line_2) update_user_meta( $user_id, 'shipping_address_2', esc_attr($woocommerce->customer->get_address_2())); update_user_meta( $user_id, 'shipping_city', esc_attr($woocommerce->customer->get_city())); update_user_meta( $user_id, 'shipping_state', esc_attr($woocommerce->customer->get_state())); update_user_meta( $user_id, 'shipping_postcode', esc_attr($woocommerce->customer->get_postcode())); } } |
Sorry, tried removing virtual products code but it’s not copying shipping to billing. Here’s what I did:
add_action( 'woocommerce_created_customer', 'es_insert_member_shipping_meta' );
function es_insert_member_shipping_meta( $user_id )
{
global $woocommerce;
// Loop through cart products
foreach( $products as $product ) {
//Update all the shipping values, Address line 2 and Company Name is the only field not mandatory
update_user_meta( $user_id, 'shipping_country', esc_attr($woocommerce->customer->get_country()));
update_user_meta( $user_id, 'shipping_first_name', get_user_meta($user_id, 'first_name', true));
update_user_meta( $user_id, 'shipping_last_name', get_user_meta($user_id, 'last_name', true));
if ($billing_company)
update_user_meta( $user_id, 'shipping_company', esc_attr());
update_user_meta( $user_id, 'shipping_address_1', esc_attr($woocommerce->customer->get_address()));
if($address_line_2)
update_user_meta( $user_id, 'shipping_address_2', esc_attr($woocommerce->customer->get_address_2()));
update_user_meta( $user_id, 'shipping_city', esc_attr($woocommerce->customer->get_city()));
update_user_meta( $user_id, 'shipping_state', esc_attr($woocommerce->customer->get_state()));
update_user_meta( $user_id, 'shipping_postcode', esc_attr($woocommerce->customer->get_postcode()));
}
}
Get rid of the “foreach( $products as $product )” loop you don’t need that. You only need the update_user_meta commands.
Nope.. Sorry. Still doesn’t work. Please email me if you have time for this small project. Thank you!