Author Topic: Variable initialization in saveAddressInCart  (Read 5860 times)

anantmaks

  • Beginner
  • *
  • Posts: 43
  • Indian Software Engineer
Variable initialization in saveAddressInCart
« 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?
Anant Garg
Ghaziabad, India

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4728
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Variable initialization in saveAddressInCart
« Reply #1 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.

anantmaks

  • Beginner
  • *
  • Posts: 43
  • Indian Software Engineer
Re: Variable initialization in saveAddressInCart
« Reply #2 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
Anant Garg
Ghaziabad, India

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10663
  • VM4.0.232 Eagle Owl
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 4 on joomla 3
Re: Variable initialization in saveAddressInCart
« Reply #3 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
Code: [Select]
$jUser = JFactory::getUser();
if(!empty($jUser->email)) $data['email'] = $jUser->email;
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

anantmaks

  • Beginner
  • *
  • Posts: 43
  • Indian Software Engineer
Re: Variable initialization in saveAddressInCart
« Reply #4 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!
Anant Garg
Ghaziabad, India