News:

Support the VirtueMart project and become a member

Main Menu

guide | how to add a new field for products in virtuemart 3x

Started by mfarooqi, May 06, 2017, 21:18:46 PM

Previous topic - Next topic

mfarooqi

Although this is not required because "custom fields" does the same job. but.. still if somebody needs to add very custom script or custom kind of view he can using following method.


I am required to add new fields to show some youtube videos related to products.

what i did was.

1. New Fields in Database Table

Alter the database table where we need to add new field. What I had to do is ... I need some custome information for some products like YouTube videos, or Some other downloadable content etc. so created a field product_youtube_link | varchar=128

in table

abcd_virtuemart_products (here abcd is my pre-title for tables)

Showing Table Field in VM Admin's Front End


in the following page (because I wanted to show this field in backend/Admin view under product->custom fields)

  website/administrator/components/com_virtuemart/views/product/tmpl/product_edit_custom.php
i added following line of code.

<fieldset style="background-color:#b9298b;">
    <legend>Installation Guide</legend>
    YouTube URL like this: http://www.youtube.com/watch?v=nwgLvK830xI
    <div style="width: auto;">
        <input type="text" size="40" name="product_youtube_link" id="product_youtube_link" value="<?php echo $this->product->product_youtube_link?>" />
    </div>
</fieldset>


This will show an input filed under product->custom fields - in Virtuemart 3.x.x

So far now.. it doesn't show any values from database (I added manually something in phpMyAdmin for one specific product)

To make this field -readable- and -editable- or -deletabel- we have to do a lot of work.  :-\

Don't be scared. That is the simplest work...  ;)

In the following file.

website/administrator/components/com_virtuemart/helpers/shopfunctions.php

and found a function getValidProductFilterArray() ... on line 980 . and added database field title at the end of $filterArray's values.

This array should end now ...

before

.....'pc.ordering');

after

.....'pc.ordering', 'product_youtube_link');

... now refreshed the product view in VM administrator panel. and wow... :D ;D ;D my database values are there.. (what i manually added)

to test if that value is actually working.. I changed this value on the front end in product view in VM Administrator panel. saved that product.. and THANKS ALLAH.. ;D :) that value updated. I opened phpMyAdmin, and that was updated.

how to show this value on front end of website.

I need to show this under product details.

because i'm using template overrides. so under

website/templates/mytemplate/html/com_virtuemart/productdetails/default.php


or if using no overrides then in default template details page for vm..

website/components/com_virtuemart/views/productdetails/tmpl/default.php

I added following lines

<?
if (!empty($this->product->product_youtube_link)) {
    echo $this->product->product_youtube_link;
  }
?>


And that data is appearing on front end.


I hope this could help others. I searched googled for a couple of hours. but couldn't find. so finally thought to do it myself.  :)

K&K media production

This are core hacks and not recommended, because you will lost all with your next update.

There is a simple custom field plugin to add youtube videos:
http://extensions.virtuemart.net/products/youtube-detail

GJC Web Design

you can also just add a string custom field for the youtube etc id and do all the heavy lifting in the template
GJC Web Design
VirtueMart and Joomla Developers - php developers https://www.gjcwebdesign.com
VM4 AusPost Shipping Plugin - e-go Shipping Plugin - VM4 Postcode Shipping Plugin - Radius Shipping Plugin - VM4 NZ Post Shipping Plugin - AusPost Estimator
Samport Payment Plugin - EcomMerchant Payment Plugin - ccBill payment Plugin
VM2 Product Lock Extension - VM2 Preconfig Adresses Extension - TaxCloud USA Taxes Plugin - Virtuemart  Product Review Component
https://extensions.joomla.org/profile/profile/details/67210
Contact for any VirtueMart or Joomla development & customisation

Milbo

K&K media production you are completly right.

But his "manual" is very nice for developers. He shows exactly the system which I developed and how easy it is to extend the core with new features.

Another way is to add the new field just as "vars to push param" in the product table file.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

mfarooqi

Quote from: K&K media production on May 06, 2017, 21:40:13 PM
This are core hacks and not recommended, because you will lost all with your next update.

There is a simple custom field plugin to add youtube videos:
http://extensions.virtuemart.net/products/youtube-detail


Quote from: Milbo on May 07, 2017, 08:47:10 AM
K&K media production you are completly right.

But his "manual" is very nice for developers. He shows exactly the system which I developed and how easy it is to extend the core with new features.

Another way is to add the new field just as "vars to push param" in the product table file.


Quote from: GJC Web Design on May 06, 2017, 22:28:42 PM
you can also just add a string custom field for the youtube etc id and do all the heavy lifting in the template



... YOU GUYS  are Absolutely right. I updated the top line after doing that all.
I'm now a days working on an upgrade from 1.5 to 3x. There were a number of custom fields I had created in 1.5. And I didn't know if this is now a built-in function. When I had finished this workout.. I just thought to give a try to these "pre defined" custom fields feature...

oh that was the same...   ;) ... 

Now i'm creating queries to just copy all Custom fields' data from 1.5 to 3x

AH

Based on Milbo's comment

I always thought it would be nice to have a few "user" fields in the product table that could then be used for whatever a customer decided and would be much easier to edit than using customfields.

Note: Customfields is very powerful and should be your first port of call :)

However, if like me, you wish to throw caution to the wind - read on

The only thing that gets wiped out during an update in the following example is the stuff you added to administrator\components\com_virtuemart\tables\products.php  - so just one file to manage/adjust after each update.  The rest is done using simple overrides

Add the new field to the database

In products table  #__virtuemart_products

Add your new field:-   `quorvia_use_me_for_anything` VARCHAR(40) NULL DEFAULT NULL,

Make the new field available whenever you display a product in BE and FE

Add the field name to the product table data in:-

administrator\components\com_virtuemart\tables\products.php


var $layout = '';
       /** @var int published or unpublished */
var $published = 1;

    /** my new variables */
    var $quorvia_use_me_for_anything = null;



Show the new Field in a VM Admin page

A nice empty page is - product_edit_status.php

Create an override to this page (I use ISIS in admin):-

administrator\templates\isis\html\com_virtuemart\product\possible-product_edit_status.php

Add the code to display the field for editing:-

<tr>
            <td >My new variable</td>
            <td>
                <input type="text" class="inputbox" name="quorvia_use_me_for_anything" id="quorvia_use_me_for_anything" value="<?php echo $this->product->quorvia_use_me_for_anything?>" size="40" maxlength="40" />
            </td>
        </tr>




That is all you need for the backend and you now have the ability to add and maintain data in this new field

To display this new field in any product page


Create an override to the template and use this code:

if (!empty($this->product->quorvia_use_me_for_anything)) {

echo $this->product->quorvia_use_me_for_anything;
}



For those that are interested in displaying a youtube video details are shown below:

I would only store the youtube embed code in the product admin  ( e.g. IuOhL2YqJwY  )  ignoring the rest of the link they show you.


Create a sublayout for the youtube display
This keeps things nice and clean so you know where your stuff is

\templates\mytemplate\html\com_virtuemart\sublayouts\quorvia_youtube.php

Here is the complete code for it. (I just realised how long ago I did this haha)

<?php
/**
 * Created by Quorvia.
 * User: office
 * Date: 06/05/15
 * Time: 10:42
 * Display a youtube video.
 */
defined '_JEXEC' ) or die ( 'Restricted access' );

$product $viewData['product'];

//Quorvia shows a video at this point
if (!empty($product->quorvia_use_me_for_anything)) {
    echo 
"<div class='tube-heading'>Product video</div>";
    echo 
"<div class='youtube'>";
    echo  
"<iframe class='q-product-vid' src='//www.youtube.com/embed/"$product->quorvia_use_me_for_anything ."?feature=player_detailpage &fs=0 &iv_load_policy=3&rel=0'></iframe>";
    echo 
"</div>";
}
?>





Show this on a product page

Put this code wherever you want the youtube video to display:-


<?php
    
//Quorvia shows a video at this point
    
echo shopFunctionsF::renderVmSubLayout('quorvia_youtube',array('product'=>$this->product));
    
?>





Again I must restate that using product customfields is probably the best route for most things of this nature




Regards
A

Joomla 4.4.5
php 8.1

yaniv1

Hi,

I manage to implement what you suggest about the new field in product edit, the field call "Tags". and i was able to save the data and echo it on the front page.
But i need to insert a select box with multiple values, and that i didn't succeed to implement because when i save the product, only 1 value from the selection was saved to DB.
so i need to be able to save an array value, i add to the select name [] (name="Tags[]") but only 1 value was saved in the DB.
Is there any suggestion how to save the new field with multiple value?

Thanks
yaniv