Woocommerce copy company name from billing to shipping

504 Views Asked by At

I've bumped into this issue: I want to check if the shipping company name field is empty (usually is) and populate it with the company name from billing. But I can't get it right. And it's creating issues with the shipping company. I manage to do it with a javascript but if the user doesn't check the "deliver to different address" that fails.

Here are my tries to populate that field:

function onboarding_update_fields( $fields = array() ) {
     
     if( empty($fields['shipping']['shipping_company']['value']))   
     {
         if( !empty($fields['billing']['billing_company']['value'])   )
         {
        $fields['shipping']['shipping_company']['value'] = $fields['billing']['billing_company']['value'];
         }
     
     
     }
      return $fields;

}

and the simple one

 function onboarding_update_fields( $fields = array() ) {
    if ( isset($_POST['smartbill_billing_company_name']) && ( empty( $POST['shipping_company'] ) && !empty( $_POST['smartbill_billing_company_name'] )) ) {
           $_POST['shipping_company'] = $_POST['smartbill_billing_company_name'];
       }
      return $fields;
 }

the hooking


add_filter( 'woocommerce_checkout_fields', 'onboarding_update_fields' );

Hopefully, somebody can spot my error! Thank you!

1

There are 1 best solutions below

1
On

Why not use jQuery to set the Shipping Billing Address? Something like this:

if ($('#shipping_company').is(':empty')) {
    var billingCompany = $('#billing_company').val();
    $('#shipping_company').val(billingCompany);
}