Custom field doesn't work when type is string and value is 0

Started by McKillo, March 06, 2019, 07:13:23 AM

Previous topic - Next topic

McKillo

For VM 3.4.3.10014 on JL 3.9.3.

Simulate issue:
1.   Define new customfield, string with name 'Size'.
2.   Add this new customfield to some product and with value 0, in this case, the value Size is 0.
3.   Add this new customfield to some product and with value 2, in this case, the value Size is 2.
3.   Save product.

Error:
If you see the product modified, in the Size atribute only show 2 option into combo: " " (first option empty) and 2 (second option). Don't see 0 in the first option.

Debug:
This value 0 (as string) isn't saving into table _virtuemart_product_customfields and  its _customfield_value field is empty.

Solution:
You have to modify the code to correct this data management and change code in 3 files:

FIRST: In administrator part to save data of custom field:
administrator/components/com_virtuemart/helpers/vmtable.php line 510

Change these lines:

// Bind the source value, excluding the ignored fields.
foreach ($this->getProperties() as $k => $v)
{
   // Only process fields not in the ignore array.
   if (!in_array($k, $ignore))
   {
      if (isset($src[$k]))
      {
         $this->$k = $src[$k];
      }
   }
}


To others one:

// Bind the source value, excluding the ignored fields.      
foreach ($this->getProperties() as $k => $v)
{
        // Only process fields not in the ignore array.
        if($k=="customfield_value"){
            if (!in_array($k, $ignore, true)) {
                  if (isset($src[$k])) {
                        $this->$k = $src[$k];
                       }
                   }
        }else {
                   if (!in_array($k, $ignore)) {
                       if (isset($src[$k])) {
                              $this->$k = $src[$k];
                       }   
                   }
        }
}


At least, check type 'in_array' when $k is 'customfield_value'.
So, save the value of customfield.
I don't know if this change will have to apply in functions in this same file(possibly):
     checkDataContainsTableFields
     bindTo
It's a task for VM support/dev.


SECOND: In administrator part to read data of custom field:
administrator/components/com_virtuemart/models/customfields.php line 338

Change these lines:

$field->customfield_value = empty($field->customfield_value) ? $field->custom_value : $field->customfield_value;

To others one:

if($field->field_type=="S"){
$field->customfield_value = isset($field->customfield_value) ? $field->customfield_value : $field->custom_value;
}else {
          $field->customfield_value = empty($field->customfield_value) ? $field->custom_value : $field->customfield_value;
}


So, read the value of customfield.

THIRD: In component part to show data of custom field (product detaild view to user):
components/com_virtuemart/sublayouts/customfield.php line 56

Change these lines

$customfield->customfield_value = empty($customfield->customfield_value) ? $customfield->custom_value : $customfield->customfield_value;

To others one:

if($customfield->field_type=="S"){
$customfield->customfield_value = isset($customfield->customfield_value) ? $customfield->customfield_value : $customfield->custom_value ;
}else {
        $customfield->customfield_value = empty($customfield->customfield_value) ? $customfield->custom_value : $customfield->customfield_value;
}


So, user can see the value of customfield into productdetails view.

REQUEST:
I do not know if it is the best solution but it works, and I would like development to implement the best solution for this issue in the next subreleased VM.

Dev/Real: VM 3.4.3.x + JL 3.9.3 + PHP 7.2.15 VM Silver Certified

d0ublezer0

Biggest request for VM developers: please, fix this issue. This is great problem in my case. We actively use zero-values in custom fields, but cannot work correctly with them.
Also, you already can see possibly solution in this thread

lostmail

I have the same problem.

Type is string, value is empty (I need only the Title of the custom field - no value.)

When I add the custom field to a product (value = empty) than on the product page the field is not visible.
It is only visible in product page when i add an empty space or another value.

In Cart the field title is visible..

UPDATE: When I add a default value (i.e. empty space) than the field is visible in product view.
So far it solves my problem.

Joomla 3.x | VirtueMart 4.0.12 107771 | HORME3 PRO 1.9.6 / 2.0
VirtueMart 4.2.4 | Joomla 4.4.1 | PHP 8.1 | Vp_neoteric 1.3

Milbo

Thank you to bring this up again. McKillo nice to read from you. I will take an another look on that problem. I wonder about the sideeffects enabling that.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Milbo

I replaced it now with


$field->customfield_value = (!isset($field->customfield_value) or $field->customfield_value==='') ? $field->custom_value : $field->customfield_value;

and

$customfield->customfield_value = (!isset($customfield->customfield_value) or $customfield->customfield_value==='') ? $customfield->custom_value : $customfield->customfield_value;


That should work now also with 0 and the empty String "" can be done with " " (as explained by lostmail) just with an extra Space. But the "" is still overriden by the prototype. When the prototype has also "", then even an empty string "" should work.

The first change of McKillo was imho not needed.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/