I know i can limit the available countries by managing the address field.

However, I am wanting to use a rule to set only one country to be available in the address form, depending upon a geo lookup.

I already have a country lookup that is happening - I use context and rules to set a context for that country. Now, I want to only display the one country and no other country options?

Even just setting a dynamic default country would help.... but my rule to set the default value of the form element (customer_profile_billing[commerce_customer_address][und][0][country]) to US is not working.

any ideas?

Comments

rszrama’s picture

Status: Active » Fixed

To accomplish this through Rules, you'd have to actually create the customer profile and save it to the order when the shopping cart order was created. This isn't impossible, but it's not a trivial Rules configuration either. The simplest thing to do would be to alter the checkout form so only the option you wanted was available (ensuring the form was rebuilt for the target country at the same time). However, if you want to pursue the Rules option, I'd check around on #drupal-commerce in IRC to see if someone is around to walk you through it.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

rudy.barrett’s picture

I was able to do this by modifying the default customer profile fields provided by Commerce:
admin/commerce/customer-profiles/types/billing/fields/commerce_customer_address
/admin/commerce/customer-profiles/types/shipping/fields/commerce_customer_address

Hope this helps someone in the future, it took me a minute to find.

pog21’s picture

Issue summary: View changes

I don't have any "customer profiles" section within Commerce. Is there another way to access these field options?

Doh! Easily fixed: Enable Customer UI module.

Jurgen8en’s picture

I found a solution to set the countries programmatically.

/**
 * Set the right available countries for checkout
TODO Where to call this function?
 */
function custom_set_available_countries(){
  new ShippingRules; // This will load ShippingRules class automatically.
  
  //Shipping
  $instance = _custom_country_get_addressfield_instance('shipping');
  $instance['widget']['settings']['available_countries'] = ShippingRules::$countries;
  field_update_instance($instance);
  
  //Billing
  $instance = _custom_country_get_addressfield_instance('billing');
  $instance['widget']['settings']['available_countries'] = ShippingRules::$countries;
  field_update_instance($instance);
  
}

/**
 * Helper function to get the addressfield instance.
 */
function _custom_country_get_addressfield_instance($type) {
  $entity_type = 'commerce_customer_profile';
  $bundle_name = variable_get('commerce_'.$type.'_country_customer_bundle', $type);
  $field_name = variable_get('commerce_'.$type.'_country_customer_addressfield', 'commerce_customer_address');

  return field_info_instance($entity_type, $field_name, $bundle_name);
}
Jurgen8en’s picture

Much easier with the latest Address Field 7.x-1.0
To set default country and available countries

function custom_addressfield_default_values_alter(&$default_values, $context) {
  if (empty($default_values['country'])) {
    $default_values['country'] = custom_get_user_country_code();
  }
  
  new ShippingRules; // This will load ShippingRules class automatically.
  $context['instance']['widget']['settings']['available_countries'] = ShippingRules::$countries;
  field_update_instance($context['instance']);
}
ptmkenny’s picture

Status: Closed (fixed) » Needs work

@jurgen8en: With Addressfield 1.2, I am getting the following error:

"Fatal error: Class 'ShippingRules' not found in /srv/bindings/3e6587f55074463ea17292eda4e1ac67/code/sites/all/modules/mycustommodule"

Is there an alternate way to load the class?

rszrama’s picture

Status: Needs work » Closed (fixed)

That looks unrelated to Drupal Commerce or anything else that we maintain and rather an issue with your custom module. Please refer to general Drupal API documentation for autoloading classes or post your question on https://drupal.stackexchange.com.

schiavone’s picture

#4 worked for me. Enabled commerce_customer_ui

Go to /admin/commerce/customer-profiles/types/billing/fields

Where you can select what countries to list and a default country

AmolB’s picture

Comment #9 from schiavone worked for me.

webengr’s picture

#3 worked, thank you Rudy for sharing!