Drupal 7 Commerce: Programatically adding the Billing and Shipping informations to Order

In Drupal, commerce order contains both Billing and shipping information on normal checkout process. If we are creating the commerce order programmatically then we have to add this information in manually. The below is the example of code for adding the Billing and shipping address to the order.

Syntax: 

$order = commerce_order_load(ORDER_ID); // Load commerce Order.
$orderProfile = commerce_customer_profile_new(TYPE); // Load the prile type billing (or) shipping
$orderProfile->FIELD_1 = VALUE_1;
$orderProfile->FIELD_1 = VALUE_1;
// Saving Billing or Shipping Profile Id in Order.
$order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'] = $orderProfile->profile_id; (or) $order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'] = $orderProfile->profile_id; 
commerce_order_save($order);

Example:

1. Saving Billing Information: 

$order = commerce_order_load(12345); // Load commerce Order.
$profileBill = commerce_customer_profile_new('billing'); // Create new Billing profile.
// Saving user name.
$profileBill->field_name_to_print[LANGUAGE_NONE][0]['value'] = 'Arunkumar';
// Saving user Country.
$profileBill->commerce_customer_address[LANGUAGE_NONE][0]['country'] = 'US';
// Saving zipcode.
$profileBill->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] = 3412;
commerce_customer_profile_save($profileBill); // Saving new profile.
 
$order->commerce_customer_billing[LANGUAGE_NONE][0]['profile_id'] = $profileBill->profile_id; // Saving Billing Profile Id in Order.
commerce_order_save($order);

2. Saving Shipping Information: 

$order = commerce_order_load(12345); // Load commerce Order.
$profileShip = commerce_customer_profile_new('shipping'); // Create new Shipping profile.
// Saving user name.
$profileShip->field_name_to_print[LANGUAGE_NONE][0]['value'] = 'Arunkumar';
// Saving user Country.
$profileShip->commerce_customer_address[LANGUAGE_NONE][0]['country'] = 'US';
// Saving zipcode.
$profileShip->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] = 3412;
commerce_customer_profile_save($profileShip); // Saving new profile.
 
$order->commerce_customer_shipping[LANGUAGE_NONE][0]['profile_id'] = $profileShip->profile_id; // Saving Shipping Id in Order.
commerce_order_save($order);
Category: