Author Topic: Custom Fields in Category View?  (Read 38709 times)

ChristerE

  • Beginner
  • *
  • Posts: 10
Custom Fields in Category View?
« on: March 25, 2012, 23:06:52 pm »
I've created a custom field with some color values, it shows at the product details-page, on the module page (featured products) BUT i can't get it to work on my category page :( Any suggestions??? Been at it for 3 hours now and all my testing has failed.

Thanks for any  ideas

John2400

  • Virtuemart - the Future is ever Changing
  • Global Moderator
  • Full Member
  • *
  • Posts: 1568
  • VirtueMart Version: Use VM3.0.12
Re: Custom Fields in Category View?
« Reply #1 on: March 28, 2012, 04:46:07 am »
ChristerE
Hi - just wondering about what you are trying to do-
* You have set up a custom field that shows up correctly on the product details page - " so when I click on  a category " it jumps to that product and you can then see the product and the small options"custom values " show up against your product.
 I then pick a colour and I can buy that product?
Is that correct ?  --

Your custom values will not show up against a category ?  This you cannot do as categories are simply links to products ? - I'm just restating what you have written. I am sorry if I have misquoted your problem.

maybe - show us an example by image .

stuart.prevos

  • Beginner
  • *
  • Posts: 36
Re: Custom Fields in Category View?
« Reply #2 on: May 23, 2012, 10:36:56 am »
I am having the same issue
VM 2.0.6
J2.6
I have some custom fields and want to display the custom fields in the category view but it aint working for me. I have pulled some code from the /views/productdetails/default.php and put it in /views/category/default.php, here is what I have
============
<?php
               if ($this->show_prices == '1') {
                  if( $product->product_unit && VmConfig::get('vm_price_show_packaging_pricelabel')) {
                     echo "<strong>". JText::_('COM_VIRTUEMART_CART_PRICE_PER_UNIT').' ('.$product->product_unit."):</strong>";
                  }
                  if(empty($product->prices) and VmConfig::get('askprice',1) and empty($product->images[0]->file_is_downloadable) ){
                     echo JText::_('COM_VIRTUEMART_PRODUCT_ASKPRICE');
                  }
                  //todo add config settings
                  if( $this->showBasePrice){
                     echo $this->currency->createPriceDiv('basePrice','COM_VIRTUEMART_PRODUCT_BASEPRICE',$product->prices);
                     echo $this->currency->createPriceDiv('basePriceVariant','COM_VIRTUEMART_PRODUCT_BASEPRICE_VARIANT',$product->prices);
                  }
                  echo $this->currency->createPriceDiv('variantModification','COM_VIRTUEMART_PRODUCT_VARIANT_MOD',$product->prices);
                  echo $this->currency->createPriceDiv('basePriceWithTax','COM_VIRTUEMART_PRODUCT_BASEPRICE_WITHTAX',$product->prices);
                  echo $this->currency->createPriceDiv('discountedPriceWithoutTax','COM_VIRTUEMART_PRODUCT_DISCOUNTED_PRICE',$product->prices);
                  echo $this->currency->createPriceDiv('salesPriceWithDiscount','COM_VIRTUEMART_PRODUCT_SALESPRICE_WITH_DISCOUNT',$product->prices);
                  echo $this->currency->createPriceDiv('salesPrice','COM_VIRTUEMART_PRODUCT_SALESPRICE',$product->prices);
                  echo $this->currency->createPriceDiv('priceWithoutTax','COM_VIRTUEMART_PRODUCT_SALESPRICE_WITHOUT_TAX',$product->prices);
                  echo $this->currency->createPriceDiv('discountAmount','COM_VIRTUEMART_PRODUCT_DISCOUNT_AMOUNT',$product->prices);
                  echo $this->currency->createPriceDiv('taxAmount','COM_VIRTUEMART_PRODUCT_TAX_AMOUNT',$product->prices);
               } ?>
               
                <?php
               
                // ********** custom fields *****************//
                     <?php
                         } // Product custom_fields

                         if (!empty($this->product->customfieldsSorted['normal'])) {
                     $this->position = 'normal';
                     echo $this->loadTemplate('customfields');
                      } // Product custom_fields END               
                     
                     
                      // **********end of custom fields *****************//
                   ?>
               
               </div>
==============================
Thanks

John2400

  • Virtuemart - the Future is ever Changing
  • Global Moderator
  • Full Member
  • *
  • Posts: 1568
  • VirtueMart Version: Use VM3.0.12
Re: Custom Fields in Category View?
« Reply #3 on: May 27, 2012, 03:58:40 am »
Hi guys,

I'm not really sure what you are trying to do But I read this today with some beautiful examples

try here and write back in this forum :
http://forum.virtuemart.net/index.php?topic=102918.0

Peter Pillen

  • Jr. Member
  • **
  • Posts: 223
Re: Custom Fields in Category View?
« Reply #4 on: October 07, 2012, 18:34:44 pm »
the link in the last page doesn't really work. But I needed to show the custom field in the browse pages and I've managed to solve it in my case (I did it once before in VM1). In the image below you can see the result. It shows the values (shoesizes in this case) that belong to a specific product.



It requires some knowledge of php and mysql to transform the custom field in the final result you want, but maybe this can help you on the right way. I'm sorry for my messy programming and such... but it works :)

to show the custom fields in your category browse page edit this file /templates/"your template"/html/com_virtuemart/category/default.php  (depending on which template you are using this file location could be different) and place this code where you want to show the custom field. In my case I placed under the product short description.

Code: [Select]
<?php // Product Short Description
     
if(!empty($product->product_s_desc)) { ?>

     <p class="product_s_desc">
     <?php echo shopFunctionsF::limitStringByWord($product->product_s_desc40'...'?>
     </p>
     <?php }
//MY EDIT STARTS HERE
 //make a database connection and find the field record that contains the customfield string
     
$db JFactory::getDBO();
     
//please do check in php myadmin how your database table is called and replace it instead of "VM2_virtuemart_product_customfields"
     
$db->setQuery("SELECT custom_param FROM VM2_virtuemart_product_customfields WHERE virtuemart_product_id=".$product->virtuemart_product_id.";");
     
$db->query();
     
$result $db->loadResult();
 
//$result is the custom field output
// if you just need the custom field, you're done. I'll show how I did it to change the custom field to what I have

//check to see if there is a customfield mentioned
     
if(!empty($result)){
          
//remove all unwanted symbols (waste) from the string
          
$waste=array('{','}','"',':');
          
$clean_result=str_replace($waste,"",$result);
          
          
//detect and prepare the language label ... you can skip this if you don't need it
                    
$lang =& JFactory::getLanguage();
                    if(
$lang->getName()=="Dutch (NL)"){ $my_lang="nl"; }
                    if(
$lang->getName()=="French (fr-FR)"){ $my_lang="fr"; }
                    if(
$lang->getName()=="English (United Kingdom)"){ $my_lang="nl"; }
                    
$ta_nl="Maten: ";
                    
$ta_fr="Pointures: ";
                    
$ta_en="Sizes: ";
          
//language done
          
          //now make an array from the clean result: I have left the komma's in the string. This was not waste and i'm gonna use it now
          
$pieces explode(","$clean_result);
          
// make a p-tag for your custom field
          
echo "<p class=\"product_s_desc\">".${ta_.$my_lang};
          
// now echo all values from the clean result that we exploded into pieces and check if they are numeric... because these are the shoesizes
          
foreach($pieces as $size){
                    
$final_result=explode('[',$size);
                    if(
is_numeric($final_result[0])){
                              echo 
$final_result[0]." ";
                    }
          }
          unset(
$size);
          echo 
"</p>";
   }
?>

I have succesfully placed this code in a few files such as templates/"your template"/html/mod_virtuemart_product/default.php so that the custom fields are also seen on modules such as: latest products, featured products and so on.

I hope this helps

be

  • Beginner
  • *
  • Posts: 36
Re: Custom Fields in Category View?
« Reply #5 on: October 30, 2012, 20:21:15 pm »
Hi,
I have done it like you but on my site there will not puplish but the enclosing span.

Code: [Select]
<?php
//MY EDIT STARTS HERE
//make a database connection and find the field record that contains the customfield string
$db JFactory::getDBO();
//please do check in php myadmin how your database table is called and replace it instead of "VM2_virtuemart_product_customfields"
$db->setQuery('SELECT custom_param FROM mzoex_virtuemart_product_customfields WHERE virtuemart_product_id='.$product->virtuemart_product_id.';');
$db->query();
$result $db->loadResult();
//$result is the custom field output
//check to see if there is a customfield mentioned
echo "<span class=\"product_s_desc\">".$result."</span>";  
?>

Normally it should be enough to call the variable $result to display the table content, or?
Is there another table who called similar like xxx_virtuemart_product_customfields?

BR

PS: Joomla 2.5.7 and Virtuemart 2.0.12f

Peter Pillen

  • Jr. Member
  • **
  • Posts: 223
Re: Custom Fields in Category View?
« Reply #6 on: October 30, 2012, 20:25:59 pm »
Yep. $result should be enough to show the content of the custom parameters of that product. You should get the same value as you see in the database.

Are you sure that your database name is correct and that there is a custom parameter set for the product?

wanderschaft

  • Beginner
  • *
  • Posts: 10
Re: Custom Fields in Category View?
« Reply #7 on: November 02, 2012, 06:58:46 am »
The solution from Peter works great for me! Thanks.

some additional Information that may help. I found this when I tried your solution:

1. in first row of your code edit use "<? php {" instead of "<? php }", but I guess you wont need the brackets at all when you use just the first part

2. instead of hardcoded prefix in Database Name xxxx_virtuemart... use the general way #__virtuemart... (note 2 underscores)
3. I have more than one custom field per product. So maybe someone want to use a specific product custom field
a) find out the custom_field_id (simply by looking in phpMyAdmin in the custom field table or by looking in the Joomla backend in components>virtuemart>product>custom fields>ID Value at the last column) Note also that many custom field (like Strings oder HTML text fields use the custom_value (not param) to store the specific custom field information for a product like in my case a normal string wiht an additional Product ID/EAN Code. So I also changed the Query from custom_param to custom_value
Code: [Select]
             <?php 
             
//MY EDIT STARTS HERE
 //make a database connection and find the field record that contains the customfield string
     
$db JFactory::getDBO();
     
//please do check in php myadmin how your database table is called and replace it instead of "VM2_virtuemart_product_customfields" replace the "6" with your ID of Custom field you want to output
     
$db->setQuery("SELECT custom_value FROM #__virtuemart_product_customfields WHERE virtuemart_product_id=".$product->virtuemart_product_id." and virtuemart_custom_id=6 ;");  
     
$db->query();
     
$result $db->loadResult();
 echo "<span class=\"YourClass\">".$result."</span>";
?>

4. you may also use a div with a class to style the output via css, in this case I left the span, just put this or similar in your template css
Code: [Select]
.YourClass {
color: #ff0000;
}


be

  • Beginner
  • *
  • Posts: 36
Re: Custom Fields in Category View?
« Reply #8 on: November 02, 2012, 15:25:25 pm »
Hi,
a big fat thankyou to you!
Now it works and the best is the part with the itemid this solves my problem.
What is the comparable var for the titleoutput of this itemid?
Code: [Select]
… virtuemart_product_id=".$product->virtuemart_product_id."
Best regard

amorino

  • Jr. Member
  • **
  • Posts: 150
    • Création sites web Tunisie
Re: Custom Fields in Category View?
« Reply #9 on: December 09, 2012, 17:26:41 pm »
Hello,
Thank you very much it solved me so many problem and worked like a charm every where ;)
Best regards
Amorino
Création sites web Tunisie
http://www.idealconception.com

Peter Pillen

  • Jr. Member
  • **
  • Posts: 223
Re: Custom Fields in Category View?
« Reply #10 on: December 09, 2012, 17:45:36 pm »
A very good addition from wanderschaft.  ;)

squat

  • Beginner
  • *
  • Posts: 1
Re: Custom Fields in Category View?
« Reply #11 on: December 19, 2012, 15:35:42 pm »
Code: [Select]
$db = JFactory::getDBO();

$query = $db->getQuery(true);
$query->select('custom_value');
$query->from('#__virtuemart_product_customfields');
$query->where('virtuemart_product_id="' . (int) $db->escape($product->virtuemart_product_id) . '"');
$query->where('virtuemart_custom_id="6"');

$db->setQuery($query);
$result = $db->loadResult();

echo "<span class=\"YourClass\">".$result."</span>";

PRO

  • Global Moderator
  • Super Hero
  • *
  • Posts: 10398
  • VirtueMart Version: 3+
Re: Custom Fields in Category View?
« Reply #12 on: December 19, 2012, 18:47:23 pm »
I wrote a snippet of code to use my "internal notes" field as a category custom field

https://forum.virtuemart.net/index.php?topic=109549.0

J3.9+ VM 3.4.2
Slowest Page Speed Score (88) (Category)
Fastest Page Speed Score (94-96) (productdetails)

Abigail Ritchie

  • Beginner
  • *
  • Posts: 5
Re: Custom Fields in Category View?
« Reply #13 on: February 01, 2013, 12:45:52 pm »
Hi,
I'm brand new to virtuemart and have been trying to get this working for a few days with no joy at all,
really could do with some help please because I've run out of options.

virtuemart.2.0.12b
joomla 2.5.7

This is the code I'm using (same as above)
Code: [Select]
    $db = JFactory::getDBO();
     //please do check in php myadmin how your database table is called and replace it instead of "VM2_virtuemart_product_customfields" replace the "6" with your ID of Custom field you want to output
     $db->setQuery("SELECT custom_value FROM #__virtuemart_product_customfields WHERE virtuemart_product_id=".$product->virtuemart_product_id." and virtuemart_custom_id=17 ;"); 
     $db->query();
     $result = $db->loadResult();
echo "<span class=\"YourClass\">".$result."</span>";
?>

Not sure If I should be adding anything else? I've checked the database out and everything corresponds.

Could it be that this no longer works with my current version of VM?

Abby

Peter Pillen

  • Jr. Member
  • **
  • Posts: 223
Re: Custom Fields in Category View?
« Reply #14 on: February 01, 2013, 13:10:13 pm »
i'm not completely sure how to fix this for you. Your syntax seems correct...

1. are you 100% sure that you want to call the the custom_value and not the custom_param field?
2. i have joomla 2.5.8 and VM 2.018a and it still works