VirtueMart Forum

VirtueMart 2 + 3 + 4 => Templating & Layouts => Topic started by: ste-jackson on May 31, 2016, 15:17:01 PM

Title: Insert a single custom filed sting into template
Post by: ste-jackson on May 31, 2016, 15:17:01 PM
Hi, thanks in advance for anyone who can help.

I am trying to update a VM template, I need to call a single Virtuemart Custom Field in this example Theme.  I only need to be a able to call this single customfield into the template not the whole array I only really need the variable.  I am completely stumped. :'(

What I am trying to achieve is to use the value of the custom field string in the PHP code to call up different artwork for the page type e.g. a custom field String: "Theme"which has a value of "Jungle" and then echo the variable "jungle" into my code.

VirtueMart 3.0.16

Regards

Steve Jackson
Title: Re: Insert a single custom filed sting into template
Post by: PRO on May 31, 2016, 17:20:50 PM
                <?php   
           $value='';
          if   (!empty($product->customfieldsSorted['my-position']))   {   
   foreach ($product->customfieldsSorted['my-position'] as $field) {
   $value =$field->customfield_value;
      } }
            if ($value=='Jungle'){
       your code goes here

}


?>



if its not   $field->customfield_value

then it is $field->customfield_params
Title: Re: Insert a single custom filed sting into template
Post by: ste-jackson on May 31, 2016, 17:56:22 PM
Hi Pro

Thanks for your reply, but it doesn't seem to be working.  Nothing is display, I have put my custom string in a position of my-position.  I have tried both variations of  $field->customfield_value and $field->customfield_params.

<?php   
           $producttheme='';
          if   (!empty($product->customfieldsSorted['my-position']))   {   
   foreach ($product->customfieldsSorted['my-position'] as $field) {
   $value =$field->customfield_params;
      } }
            if ($producttheme=='jungle'){
      echo "Hello world!";

}
echo $producttheme ;

?>
Title: Re: Insert a single custom filed sting into template
Post by: PRO on May 31, 2016, 18:04:59 PM
you did not change

$value =$field->customfield_params;
Title: Re: Insert a single custom filed sting into template
Post by: ste-jackson on May 31, 2016, 18:50:08 PM
Hi Pro

I have changed that it has made no difference.  I have tried both of the below:
<?php   
           $producttheme='';
            if   (!empty($product->customfieldsSorted['my-position']))   {   
               foreach ($product->customfieldsSorted['my-position'] as $field) {
                $producttheme =$field->customfield_value;
            } }
            if ($producttheme=='jungle'){
            echo "Hello world!";

         }
echo $producttheme ;

?>
   
   
   <?php   
           $value='';
          if   (!empty($product->customfieldsSorted['my-position']))   {   
   foreach ($product->customfieldsSorted['my-position'] as $field) {
   $value =$field->customfield_value;
      } }
            if ($value=='Jungle'){
       echo "your code goes here";

}
?>

If I set the variable manually it works $producttheme='jungle';

Please forgive my ignorance, my PHP is rusty.

Regards

Steve
Title: Re: Insert a single custom filed sting into template
Post by: PRO on May 31, 2016, 22:10:52 PM
what happens when you echo this

echo $producttheme ;

what shows up?



Title: Re: Insert a single custom filed sting into template
Post by: ste-jackson on June 01, 2016, 09:43:46 AM
Good Morning Pro

I have tried echo $producttheme; and echo $value ; respectively outputs nothing, it is like the if statement is not referencing the customfields correctly, am I supposed to do anything with the custom field string other than assign it a layout location e.g. in the template etc?

I have attached a screenshot of the custom string properties.  The custom field is a string, Title is "Theme" Layout position is "my-position" it is set to show title and is published and works the custom field displays if I leave the layout blank and is called with the other custom fields using the following code:
echo shopFunctionsF::renderVmSubLayout('customfields',array('product'=>$this->product,'position'=>'normal'));

I have also tried changing $product->customfieldsSorted['my-position'] to $product->customfields['my-position']

I have also used a echo '<pre>',print_r($producttheme,1),'</pre>'; function to see if any variable is being held and it just comes up blank.

I have tried it on both a converted VM2 and the standard VM templates.

Regards

Steve

Title: Re: Insert a single custom filed sting into template
Post by: Ghost on June 01, 2016, 10:41:23 AM
Do you see anything with this code:
echo shopFunctionsF::renderVmSubLayout('customfields',array('product'=>$this->product,'position'=>'my-position'));
?

And where are you adding the codes? To product details layout default.php?
Title: Re: Insert a single custom filed sting into template
Post by: ste-jackson on June 01, 2016, 10:47:12 AM
Yes that works, that shows the custom string of Theme Jungle

Yes this is the location I am adding it to
Title: Re: Insert a single custom filed sting into template
Post by: ste-jackson on June 01, 2016, 16:45:41 PM
That works but I still need to get the value outputted as a variable so I can use it.
Title: Re: Insert a single custom filed sting into template
Post by: PRO on June 01, 2016, 19:46:58 PM
Quote from: PRO on May 31, 2016, 17:20:50 PM
                <?php   
           $value='';
          if   (!empty($product->customfieldsSorted['my-position']))   {   
   foreach ($product->customfieldsSorted['my-position'] as $field) {
   $value =$field->customfield_value;
      } }
            if ($value=='Jungle'){
       your code goes here

}


?>



if its not   $field->customfield_value

then it is $field->customfield_params


^^^
Title: Re: Insert a single custom filed sting into template
Post by: Ghost on June 02, 2016, 07:16:16 AM
Change $product to $this->product.

http://forum.virtuemart.net/index.php?topic=100696.0
Title: Re: Insert a single custom filed sting into template
Post by: ste-jackson on June 02, 2016, 19:22:30 PM
Thanks so much Ghost, that has worked.   ;D

For reference to anyone else looking this is my final code:

           $producttheme='';
if   (!empty($this->product->customfieldsSorted['my-position']))   {   
foreach ($this->product->customfieldsSorted['my-position'] as $field) {
$producttheme =$field->customfield_value;
} };