Mis-configured Shopper Fields can prevent User Address from loading

Started by mbarry, January 24, 2014, 03:22:46 AM

Previous topic - Next topic

mbarry

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]

jshoeks

Hi,

i have this problem too on a page...

has tested alot and found out, that does this works.

in components/com_virtuemart/helpers/cart.php
public function setPreferred() {

$usermodel = VmModel::getModel('user');
$user = $usermodel->getCurrentUser();

//if (empty($this->BT) || (!empty($this->BT) && count($this->BT) <=1) ) {
foreach ($user->userInfo as $address) {
if ($address->address_type == 'BT') {
$this->saveAddressInCart((array) $address, $address->address_type,false);
}
}
//}

See the two lines i have set // infront of.

I know this is not a good sollution to hack the core files, but its alot better, than the clients complain and its not working.
Please fix this asap in virtuemart

Milbo

I am sorry, I dont understand the error.

If you have a user which is registered on joomla, and there is not vm address data, it is exactly meant that it is prefilled with the default. If you dont want this behaviour, then do not enter a default value.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

mbarry

The issue has nothing to do with the registered user.

The problem occurs for a very specific case when shopper fields are created or modified in the backend.
  Must be two or more Shopper Fields with Default values set to some value other than zero.
  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)

1. Select an existing a shopper field (or create a new one) that is of type 'delimiter' or 'text'. (Use delimiter_userinfo for this test)
2. Add a value to the default attribute of the shopper field and save the changes.
3. Repeat steps 1 and 2 for another shopper field. (Use company for this test)
4. From the front end add a product to the cart without being logged in.
5. Log into the shop.
6. The error will occur in the cart where the users address does not get displayed.
7. Select Account Maintenance; you will find all the address fields are blank. If shopper field company is displayed, it will display whatever default data was entered in the back end.

Note: I see no particular reason why any of these fields would have a default value however, if two or more fields do (and can happen), then this error will occur.

I have highlighted the code fragments that relates to this problem.

Commenting out the line identified by jswebpilots is exactly the line that causes this problem due to the condition identified above.

For jswebpilots - check that your shopper fields in the backend do not have data in the default attribute. If they do, clear it out and that should fix the problem.

Milbo

I changed this logic anyway a bit in vm2.5.2, please test this version.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

mbarry

Hi Milbo,
where do I find vm2.5.2?
I can find vm2.5.1 released for test on 25 Feb 2014.

AH

Regards
A

Joomla 3.10.11
php 8.0

jshoeks

Quote from: Milbo on March 02, 2014, 21:22:11 PM
I changed this logic anyway a bit in vm2.5.2, please test this version.

not fixed in the latest version still....

still makes same error

Ubie

Quote from: jshoeks on October 05, 2014, 20:19:59 PM
Quote from: Milbo on March 02, 2014, 21:22:11 PM
I read this top notch Trenorol review and changed this logic anyway a bit in vm2.5.2, please test this version.

not fixed in the latest version still....

still makes same error

This is fixed now though right jshoeks in the latest version? I still need to upgrade.

Milbo

I assume it is fixed, very likely in vm2.6.x and also in vm3.x
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/