Where is $product_data Declared

I have been poking around the php and tpl files in CS-Cart because I want to add several custom fields to our products. So basically when you click Catalog->Products->Add Product. Then from the General page I want my custom field to be a little textbox. I have figured out how to add this textbox to this page.



Now I am at the point where I need to store this data in the database when I click “Save” at the bottom of the general page. I am not sure, but I think I have figured out that my field needs to be added to the variable $product_data in the function fn_update_products in the products.php file in the "public_html\controllers\admin" folder.



So in this function it seems that the $product_data variable holds all of the fields that will be added to the database when I click the “Save” button. I am trying to track down where I can actually add my field to the $product_data field… I am not having much luck. Does anyone know if I am taking the correct approach here? And if so, where do I add my field??



Thanks for any help.

possibly in



schemas\revisions\descriptions.php



where the “product” array() is defined?? think i’m gettin’ closer? Somebody definitely knows this.



(and yes, I know I need to add my fields to the DB once I have added them in the php / tpl code, but I figure let’s get the hard part out of the way first.) :wink:

I have done this for a couple of things, but for this example I added the Cost of the product.



Here is what I did:



Add a language variable in your admin:



Variable: cost

Value: Cost



In your mySQL do:


ALTER TABLE `cscart_products` ADD `cost` decimal(12,2) COLLATE utf8_general_ci NOT NULL Default (0.00) AFTER `price` ;



Then in:



skins/basic/admin/views/products/update.tpl



After:


```php



{$lang.price} ({$currencies.$primary_currency.symbol}) :


```

Add:

```php

{$lang.cost} ({$currencies.$primary_currency.symbol}) :

```

That should be it. If you want to add something different this should at least give you something to start with.

I hope this helps,

Brandon

Brandon,



Thank you for your reply. Very helpful. We have the field showing in our admin page by adding it to the update.tpl, and we have changed the database as well. Now will the field’s value be saved to the database when the product is saved/updated? Or is there another step? I am assuming I need to edit a query somewhere in the uc.sql file correct?



I was also wondering about the spreadsheet upload feature for product addition/updating? Will we need to change some things to get this to work too?



It seems like also I should edit the code in \schemas\revisions\descriptions.php here:



'product_descriptions' => array (
'product' => array(),
'shortname' => array(),
'short_description' => array(),
'full_description' => array(),
'meta_keywords' => array(),
'meta_description' => array(),
'page_title' => array(),
'age_warning_message' => array(),
'search_words' => array(),
[B][COLOR="Red"]'my_field' => array(),[/COLOR][/B]
),

The uc.sql file contains the SQL statements to be executed during an upgrade so you won’t modify that.



For import/export, you will need to modify /schemas/exim/products.php



I do not think /schemas/revisions/descriptions.php is the correct file - I think those files are to support the workflow/revisions feature that will be coming eventually.



I would look in either /core/fn.catalog.php or /controllers/admin/products.php



Bob

Ok,



Man, you two are awesome! I see you guys reply to all the really tough threads with great answers. Thanks Bob and Brandon.



I am going to get to work on this.



When I get done doing this I am going to post exactly how we did it. Once its done, I kind of want to make an add-on that allows people to just add their own fields to this page and modifies all the necessary code/tpl files. That would be nice. I know people would want that if they knew it was easy to do! :wink:

To make it so you can export a field you would change:



schemas/exim/products.php



To keep with my example:



After:


'Price' => array (
'#table' => 'product_prices',
'#db_field' => 'price',
),




Add:


'Cost' => array (
'#db_field' => 'cost'
),




Now you should be able to export and import your new field in a CSV



Brandon

Ok, so I have this working now. Thanks.



Now I am wondering how this is all working. So when I add to the update.tpl, what PHP files are called to set all of these values? For instance the “Full Description” field in the update.tpl is declared like this:


```php


{$lang.full_description}:
{$product_data.full_description}

{include file="common_templates/wysiwyg.tpl" id="product_full_descr"}



```

When I put some text value in the "Full Description" which is assigned to the {$product_data.full_description} variable in the update.tpl, where is the PHP file that sets this to the correct field in the database? And then a follow up, how would I go about learning this kind of thing. Basically what line or lines in the update.tpl would lead me to finding out which PHP file is setting all of these values.

I realize its probably something like controllers/admin/products.php ... but what would lead me to find this by looking at the update.tpl file?

Sorry I am new to Smarty, but I hope to get better as time goes on...

Thanks for any help!

If anyone is still reading this, I’ll just share my findings. The update.tpl file is in fact controlled by the file:



controllers\admin\products.php



On line 38-39 of this file (for me at least) are the lines of code



if ($mode == 'add') {
if (!empty($_REQUEST['product_data']['product']) && !empty($_REQUEST['product_data']['main_category'])) {




$_REQUEST[‘product_data’][‘product’] and $_REQUEST[‘product_data’][‘main_category’] refer to the values of the fields for “Name:” and “Category:” on the Catalog->Products->General page from the admin panel, which are controlled by the update.tpl. This is just basically checking if these mandatory fields are not empty so it can go ahead and add the product to the database. And that’s the very next step in the code:



$product_id = fn_update_product($_REQUEST['product_data']);




This fn_update_product is a function that goes through all the fields for a product that you entered on the Catalog->Products->General and other pages and makes sure to add them to the database. Soooo, if I am looking to modify what the user has entered from the admin panel after they have entered it in, I should do it either before the line of code above, or inside the fn_update_product function() … I am think this is right.



Please anyone correct me if I am wrong. I want to make sure I do this right.