VirtueMart Forum

VirtueMart 2 + 3 => Virtuemart Development and bug reports => Topic started by: McKillo on March 06, 2019, 07:13:23 am

Title: Custom field doesn't work when type is string and value is 0
Post by: McKillo on March 06, 2019, 07:13:23 am
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.

Title: Re: Custom field doesn't work when type is string and value is 0
Post by: d0ublezer0 on July 03, 2019, 09:54:45 am
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
Title: [SOLVED] Re: Custom field doesn't work when type is string and value is 0
Post by: lostmail on November 30, 2019, 21:17:21 pm
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.

Title: Re: Custom field doesn't work when type is string and value is 0
Post by: Milbo on December 03, 2019, 18:10:18 pm
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.
Title: Re: Custom field doesn't work when type is string and value is 0
Post by: Milbo on December 03, 2019, 18:28:07 pm
I replaced it now with

Code: [Select]
$field->customfield_value = (!isset($field->customfield_value) or $field->customfield_value==='') ? $field->custom_value : $field->customfield_value;
and
Code: [Select]
$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.