Mis-configured Shopper Fields can prevent User Address from loading when a registered user logs in during the checkout phase
Steps
1. Add a product to the cart (should not be logged in at this stage)
2. Display the shopping cart
3. Check under the "Bill to" title, there may be one or more text entries presented below the title (see attachment ShopperFieldMisconfiguration.jpg)
4. Login via the shopping Cart login or site login form (this is an already registered user with valid BT address data )
5. If condition in step 3. exists, the Address fields will be blank except for the email address. (See attachment ShopperFieldMisconfigurationAfterLogin.jpg)
6. Check Account Maintenance; the Bill To information will be blank
While the conditions for the error to occur are likely to be rare, it has occurred to me and thefore, could to happen to others as they configure their shops.
The conditions are as follows:
Must be two or more Shopper Fields with Default values set to some value other than zero. (See attachment ShopperFieldConfiguration.jpg)
Must be a Shopper field that Virtuemart Cart code does not overwrite; these include delimiter and text fields; there maybe others:
delimiter_userinfo (type = delimiter)
delimiter_billto (type = delimiter)
company (type = text)
tax_exemption_number (type = text)
Any other "text" or "delimiter" field created by user that is setup with a default value.
Note: default values in any of the key address fields triggers an error message which alerts the user to some type of misconfiguration however,
the error message does not identify that fact that it is a default value in one of the Shopper Fields that triggered the error message.
The below code framgents show how this quirky error can occur.
public function getUserFieldsFilled($_selection, $_userData = null, $_prefix = ''){ line 738 of administrator/components/com_virtuemart/models/userfields.php
foreach ($_selection as $_fld) {
$_return['fields'][$_fld->name] = array(
'name' => $_prefix . $_fld->name
,'value' => (($_userData == null || !array_key_exists($_fld->name, $_userData)) // Debug (Line 763) this line will populate the value field with the "Default" value of the shopper field.
// The default value is set via the Virtuemart Configuration -> Shopper Fields or could be ported across
// due the Migration process
? $_fld->default
: @html_entity_decode($_userData[$_fld->name],ENT_COMPAT,'UTF-8'))
,'title' => JText::_($_fld->title)
,'type' => $_fld->type
,'required' => $_fld->required
,'hidden' => false
,'formcode' => ''
,'description' => JText::_($_fld->description)
);
function prepareAddressDataInCart($type='BT',$new = false){ Line 1406 of components/com_virtuemart/helpers/cart.php
if(empty($this->$type) and $type=='BT'){ // Debug (line 1430) $this->BT is populated here only if it is empty.
$tmp =&$this->$type ;
$tmp = array();
foreach($address['fields'] as $k =>$field){
//vmdebug('prepareAddressDataInCart',$k,$field);
if($k=='virtuemart_country_id'){
if(isset($address['fields'][$k]['virtuemart_country_id']) and !isset($tmp['virtuemart_country_id'])){
$tmp['virtuemart_country_id'] = $address['fields'][$k]['virtuemart_country_id'];
}
} else if($k=='virtuemart_state_id') {
if(isset($address['fields'][$k]['virtuemart_state_id']) and !isset($tmp['virtuemart_state_id'])){
$tmp['virtuemart_state_id'] = $address['fields'][$k]['virtuemart_state_id'];
}
} else if (!empty($address['fields'][$k]['value'])){ // Debug (line 1443) these lines are populating shopper fields (specifically the fields with type = delimiter or text)
// where the value is not empty. This occurs on first entry to this function where $address is not empty.
if(!isset($tmp[$k])){
$tmp[$k] = $address['fields'][$k]['value'];
}
}
}
function setPreferred() { line 154 of components/com_virtuemart/helpers/cart.php
$userModel = VmModel::getModel('user');
$user = $userModel->getCurrentUser();
if (empty($this->BT) || (!empty($this->BT) && count($this->BT) <=1) ) { // Debug (line 159) $this->BT may not be empty, if it has two or more entries the test fails and hence,
// the BT Address fields are never populated in the Shopping cart for Registered users who login via checkout page.
foreach ($user->userInfo as $address) {
if ($address->address_type == 'BT') {
$this->saveAddressInCart((array) $address, $address->address_type,false);
}
}
}
Perhaps some sort of warning message that the BT address fields will not be populated due to default values being set in the Shopper Fields or simply overwrite any existing data anyway.
Or better still, since these Shopper Fields are not likely to be important for Virtuemart itself to function, should there be a case where the default values are preserved and the BT address fields populated anyway.
[attachment cleanup by admin]