VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Coding Central => Topic started by: anantmaks on December 23, 2017, 09:05:27 AM

Title: Variable initialization in saveAddressInCart
Post by: anantmaks on December 23, 2017, 09:05:27 AM
VirtueMart Version: 3.2.10
Currently working on cart part I found that a variable $address is being incorrectly handled, correct me if I am wrong. In saveAddressInCart method under 'cart' helper file, within a condition an index of address is filled as $address['email'] = $jUser->email; and after the end of that condition, address is then being initialized as an array $address = array(); . Isn't it wrong or I am missing something?
Title: Re: Variable initialization in saveAddressInCart
Post by: Studio 42 on December 23, 2017, 14:51:58 PM
$jUser->email is an object member (string) that fill the $address['email'] (string)
All 2 are strings, so it's right.
If you do $jUser->email = $address['email']  it's valid too because all are strings.
Title: Re: Variable initialization in saveAddressInCart
Post by: anantmaks on December 26, 2017, 09:36:49 AM
Thanks for following up @studio42, though my question was not that. I am seeking for an answer on why $address is initialized as an array "$address = array();" after the actual assignment "$address['email'] = $jUser->email;". And what is the purpose of assignment then, if it is being initialized afterwards. Shouldn't it be initialized right in the beginning
Title: Re: Variable initialization in saveAddressInCart
Post by: Milbo on December 29, 2017, 11:39:58 AM
Hello anantmaks, you are right.

This case is only for already registered joomla users, but not registered vm users (more for old migrations and such). So it did not hurt. I checked the svn for the whole year 2017 and the error seems to be there all the time.

I suggest this fix

$jUser = JFactory::getUser();
if(!empty($jUser->email)) $data['email'] = $jUser->email;
Title: Re: Variable initialization in saveAddressInCart
Post by: anantmaks on December 30, 2017, 13:08:27 PM
Thanks Max for the reply. So, if it doesn't hurt so does it mean to be use that way only? I am not getting if it is important to be used in that way only, initializing something after assigning a value!