Displaying new music product field and data on product pages
I've successfully added a new field (format) to my Product - Music product type. The new field displays on my product creation pages and is listed in my Admin>Extras drop-down menu. I can edit the list of values for media formats there (LP, CD, DVD, etc.).
The problem I'm having now is getting the information to display on the storefront's product pages. I've edited the tpl_product_music_info_display.php file to match the same way it handles the artist field (which displays on the product pages in the productDetailsList). I've looked at other files handling the artist field to make sure the format field is always handled the same way. But no matter where I look or modify, I can't produce the field output on the product page.
I've looked in my templates and everywhere else that handles music product files (and forum threads that deal with general products doing the same). But can't even find something that alters the existing displayed fields (Model number and Artist) to indicate I'm on the right track. What very fundamental step am I missing to get the information displayed on the store's product pages? Thank you.
:frusty:
Re: Displaying new music product field and data on product pages
Okay, apparently this is activated under Admin>Catalog>Product Types (click on product type selection) in the Product Type Info Page Layout options :: Product - Music
I can see the Artist, Genre, and Record Company... but not for the new Format field.
Does this have to be registered in Admin>Admins>Admin Page Registration?
Re: Displaying new music product field and data on product pages
Take a look in the folder includes/modules/pages/product_music.
You should see at least two files, header_php.php and main_template_vars.php (I think is the name). This second file generally produces the variables that you are seeking.
Keeping your modifications independent of existing "core" files, there are two ways that generally are used to get your additional data. Either add an additional file (variable file or an additional header) or take advantage of notifiers that are observed to push your new data.
There are pros and cons to both, but that's basically where to look.
Also to get a little more "appreciation" of how/where to put things so that they are loaded where/when desired, recommend looking through index.php found at the root of the site to see generally what file(s) are loaded in what sequence.
Re: Displaying new music product field and data on product pages
In includes/modules/pages/product_music_info I have the header_php.php, main_template_vars.php, and main_template_vars_product_type.php files.
header_php.php has almost nothing in it. It's a very small file with a bit of code at the bottom.
main_template_vars.php, and main_template_vars_product_type.php, I have altered extensively to mimic the Artist field:
main_template_vars.php:
Code:
$flag_show_product_music_info_artist = zen_get_show_product_switch($_GET['products_id'], 'artist');
$flag_show_product_music_info_format = zen_get_show_product_switch($_GET['products_id'], 'format');
main_template_vars_product_type.php:
Code:
$music_extras = $db->Execute($sql);
$sql = "select * from " . TABLE_RECORD_ARTISTS . "
where artists_id = '" . $music_extras->fields['artists_id'] . "'";
$artist = $db->Execute($sql);
$sql = "select * from " . TABLE_RECORD_ARTISTS_INFO . "
where artists_id = '" . $music_extras->fields['artists_id'] . "'
and languages_id = '" . (int)$_SESSION['languages_id'] . "'";
$artist_info = $db->Execute($sql);
$sql = "select * from " . TABLE_MEDIA_FORMAT . "
where format_id = '" . $music_extras->fields['format_id'] . "'";
$format = $db->Execute($sql);
$sql = "select * from " . TABLE_MEDIA_FORMAT_INFO . "
where format_id = '" . $music_extras->fields['format_id'] . "'
and languages_id = '" . (int)$_SESSION['languages_id'] . "'";
and
Code:
* extract info from queries for use as template-variables:
*/
$products_artist_name = $artist->fields['artists_name'];
$products_artist_url = $artist_info->fields['artists_url'];
$products_format_name = $format->fields['format_name'];
$products_format_url = $format_info->fields['format_url'];
$products_record_company_name = $record_company->fields['record_company_name'];
$products_record_company_url = $record_company_info->fields['record_company_url'];
$products_music_genre_name = $music_genre->fields['music_genre_name'];
if (!empty($products_artist_url)) $products_artist_name = '<a href="' . zen_href_link(FILENAME_REDIRECT, 'action=music_artist&artists_id=' . zen_output_string_protected($music_extras->fields['artists_id']), 'NONSSL', true, false) . '" target="_BLANK">'.$products_artist_name.'</a>';
if (!empty($products_format_url)) $products_format_name = '<a href="' . zen_href_link(FILENAME_REDIRECT, 'action=music_format&format_id=' . zen_output_string_protected($music_extras->fields['format_id']), 'NONSSL', true, false) . '" target="_BLANK">'.$products_format_name.'</a>';
if (!empty($products_record_company_url)) $products_record_company_name = '<a href="' . zen_href_link(FILENAME_REDIRECT, 'action=music_record_company&record_company_id=' . zen_output_string_protected($music_extras->fields['record_company_id']), 'NONSSL', true, false) . '" target="_BLANK">'.$products_record_company_name.'</a>';
// This should be last line of the script:
$zco_notifier->notify('NOTIFY_PRODUCT_TYPE_VARS_END_PRODUCT_MUSIC_INFO');
I've tried to copy as accurately as possible, so not sure where I may have gone wrong.
Re: Displaying new music product field and data on product pages
lets see:
- you have added fields/tables into the database
- you are able to update them on the admin side
- you are now having difficulty displaying them on the product info page
does that accurately describe your current situation?
if so, you have a couple of areas to look at to resolve this problem:
- are you sure you are editing the correct template file (tpl_product_music_info_display.php)? you can add a constant into that file to see if it displays when you test. i'm sure you can figure that out. its very easy to be updating the wrong file.
- if you are editing the correct template file, you then have to ensure that you correctly retrieved the data from the database. you can do that by adding:
Code:
print_r($format);
print_r($format_info);
if you can see the data on the page from the print_r command you should be able to then display the fields. if you can not see the data, you will have to go back and look at where you constructed those db objects and correct them.
best.
Re: Displaying new music product field and data on product pages
If you get through carlwhat's list and still have problems...
Check out Twitch Hidden Searchable Model Number to find a file-map of the files you'll need to change to statically add one field from admin -> database -> product page:
https://www.zen-cart.com/downloads.php?do=file&id=1794
You'll see the modules/pages/product_info/main_template_vars.php file is preparing the data for output on the product page - storefront.
$products_model_spoon = $product_info->fields['products_model_spoon'];
Although not in the package (hiding the model is the point of that module) to output the data collected from the database on your product page simply add to includes/templates/YOUR_TEMPLATE/templates/tpl_product_info_display.php:
<h1 id="productName" class="productGeneral"><?php echo $products_name; ?><?php echo $products_model_spoon ; ?></h1>
or anywhere you would like to display the new data.
Also note: Be certain to edit the correct product type files in admin as there is a collect_info.php page for each type.
Re: Displaying new music product field and data on product pages
Quote:
Originally Posted by
carlwhat
lets see:
- you have added fields/tables into the database
- you are able to update them on the admin side
- you are now having difficulty displaying them on the product info page
does that accurately describe your current situation?
if so, you have a couple of areas to look at to resolve this problem:
- are you sure you are editing the correct template file (tpl_product_music_info_display.php)? you can add a constant into that file to see if it displays when you test. i'm sure you can figure that out. its very easy to be updating the wrong file.
- if you are editing the correct template file, you then have to ensure that you correctly retrieved the data from the database. you can do that by adding:
Code:
print_r($format);
print_r($format_info);
if you can see the data on the page from the print_r command you should be able to then display the fields. if you can not see the data, you will have to go back and look at where you constructed those db objects and correct them.
best.
I do have the Table: media_format in the database, and I can write to it. Though it doesn't appear to have the Edit Copy Delete options in front of each row like other tables do. Still, I can edit them through Admin>Extras>Media Format, and I can media format values into individual products through the product creation page.
My override was not set up well and I did not see it in my Admin>Tools>Template Selection. I fixed that and now see the select option in my admin and have it set to that. Prior to that tpl_product_music_info_display.php in the template default was handling the page. After adding the print commands
Code:
print_r($format);
print_r($format_info);
I get a real mess that looks like this (I've deleted anything that looks like specific server information):
Quote:
queryFactoryResult Object ( [EOF] => [cursor] => 0 [fields] => Array ( [format_id] => 4 [format_name] => 7" vinyl [format_image] => [date_added] => [last_modified] => ) [is_cached] => [result] => Array ( ) [result_random] => [limit] => [resource] => mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => Array ( [0] => 1 [1] => 8 [2] => 0 [3] => 0 [4] => 0 ) [num_rows] => 1 [type] => 0 ) [link] => mysqli Object ( [affected_rows] => 1 [client_info] => DELETED [client_version] => 100134 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 1 [host_info] => Localhost via UNIX socket [info] => [insert_id] => 0 [server_info] => DELETED [server_version] => 100131 [stat] => Uptime: 569284 Threads: 17 Questions: 313305399 Slow queries: 0 Opens: 85935 Flush tables: 1 Open tables: 26083 Queries per second avg: 550.349 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 4359777 [warning_count] => 0 ) [sql_query] => select * from media_format where format_id = '4' ) queryFactoryResult Object ( [EOF] => 1 [cursor] => 0 [fields] => [is_cached] => [result] => Array ( ) [result_random] => [limit] => [resource] => mysqli_result Object ( [current_field] => 0 [field_count] => 5 [lengths] => [num_rows] => 0 [type] => 0 ) [link] => mysqli Object ( [affected_rows] => -1 [client_info] => DELETED [client_version] => 100134 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 1 [host_info] => Localhost via UNIX socket [info] => [insert_id] => 0 [server_info] => DELETED [server_version] => 100131 [stat] => Uptime: 569284 Threads: 17 Questions: 313305399 Slow queries: 0 Opens: 85935 Flush tables: 1 Open tables: 26083 Queries per second avg: 550.349 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 4359777 [warning_count] => 0 ) [sql_query] => select * from media_format_info where format_id = '4' and languages_id = '1' )
I'm not getting any error logs, but not exactly getting the field value I was hoping for either.
Also, when working correctly I should be able to control this field display through the admin under Admin>Catalog>Product Types (click on product type selection) in the Product Type Info Page Layout options :: Product - Music. I believe there is a table in the database that lists those admin options. But my last attempt to fix that too didn't go very well.
Re: Displaying new music product field and data on product pages
What that means is that is the value returned from your query isn't being handled the way expected.
Can't seem to be able to tell which variable was posted, whether $format or $format_info, but whichever it is was basically set like:
$var = $db->Execute("SELECT * FROM " . TABLE_MEDIA_FORMAT_INFO . " WHERE format_id = '" . $some_selected_value . "' AND languages_id = '" . (int)$_SESSION['languages_id'] . "'");
In order to get the specific value of one of the five fields of the table: ([format_id] => 4 [format_name] => 7" vinyl [format_image] => [date_added] => [last_modified] => ), need to reference the field more directly:
$format_name = $var->fields['format_name'];
As to the setting options in the product type, yes there are a few things that need to be brought together to add those and to be sure that the naming convention works with other built in software.
Re: Displaying new music product field and data on product pages
Quote:
Originally Posted by
mc12345678
What that means is that is the value returned from your query isn't being handled the way expected.
Can't seem to be able to tell which variable was posted, whether $format or $format_info, but whichever it is was basically set like:
$var = $db->Execute("SELECT * FROM " . TABLE_MEDIA_FORMAT_INFO . " WHERE format_id = '" . $some_selected_value . "' AND languages_id = '" . (int)$_SESSION['languages_id'] . "'");
In order to get the specific value of one of the five fields of the table: ([format_id] => 4 [format_name] => 7" vinyl [format_image] => [date_added] => [last_modified] => ), need to reference the field more directly:
$format_name = $var->fields['format_name'];
Could this be caused by confusing the fields? That is, ZC refers to the artist field in the plural (record_artists, artists_id), sometimes singular (TEXT_PRODUCT_ARTIST, $artist). But I simply replaced all instances of "record" with "media," and "artist/artists" with "format," thinking they would still remain unique. "TEXT_PRODUCT_FORMAT" and "media_format." Could that cause a problem like this? Except for the Product Type Info Page Layout options, all seems to be working on the admin side.
I'm also having a very hard time overriding includes/modules/pages/product_music_info/main_template_vars_product_type.php (which appears to determine the display on music products). I was trying to avoid altering the ZC issued file, but as of now that's all I seem to be able to work with.
Re: Displaying new music product field and data on product pages
joe,
i would like to stay on point, which goes back to post #5: you have updated on the admin side, but you cannot display it on the customer side. i then quoted 2 possibilities, which is you are either not modifying the correct template file or you are not getting the data correctly from the database.
based on the information you have posted, you are modifying the correct template file. so we can eliminate that from your problem. in addition, in post #4, you have the following code:
Code:
$products_format_name = $format->fields['format_name'];
$products_format_url = $format_info->fields['format_url'];
based on the information provided, $products_format_name is correctly populated, and can be used on the template page. your title and OP says you are having difficulty displaying new product field data on product pages. and yet your code suggests that by echoing that var, you can safely use that element, which would say "vinyl". have you tried using that variable on the product info page? is that the correct value for that variable for this product?
your other variable is not being correctly populated as the sql statement you have constructed returns nothing:
Code:
select * from media_format_info where format_id = '4' and languages_id = '1'
so you have incorrectly populated that table or incorrectly constructed the above sql statement.
in addition, the populating of the first table seems to be missing some fields as the date modified and date added have no values.
with regards to not modifying core ZC files, good luck with that! (said sarcastically....) as i have previously stated, i do not think what you are trying to do is easy. and it is not handled easily in other shopping cart systems that provide overrides and field creation and the like.... my opinion previously stated is to put as much as possible in your own extra_functions file and then call needed functions, most likely from the template file to preserve as much of the ZC core as possible. and if you truly want to have an audit trail of what you are doing, i would put all source under a source version control system, ie git, so that you can see what you did. (mind you, learning git is a major job in and/of itself; but well worth the value in the long run...)
with regards to the problem and your question raised in #9 above, frankly i'm at a loss. i do not agree w MCs statement that "..the value returned from your query isn't being handled the way expected." i provided you some tools to see what is available in the database objects you constructed, named $format and $format_info. they provided exactly the information i expected/needed to help you with your problem. if you look closely at the "real mess" object that you provided, perhaps you can better understand what those objects are and now to use them.
best.
Re: Displaying new music product field and data on product pages
Quote:
Originally Posted by
carlwhat
joe,
i would like to stay on point, which goes back to post #5: you have updated on the admin side, but you cannot display it on the customer side. i then quoted 2 possibilities, which is you are either not modifying the correct template file or you are not getting the data correctly from the database.
I think the problem was a bit of both. I ran into a couple of problems with the database that I still don't fully understand being new to working with phpMyAdmin and databases. Not sure if I had to set a field as a primary, or index, or what that means exactly... but whatever I did got the edit, copy and delete options to appear next to the fields on the table I created like other tables in the DB appear. Then I had a table populating problem; discovered when I tried to add a new format field value. The database wouldn't accept an id value because it wasn't unique. And of course, I also had to populate some products with values for the new field so they could ultimately show in the store.
Then in the scripts, there was a lot of confusion. It took me a bit of time to realize that the the admin handles the store's back end, and the includes basically deal with the way the store looks (yes ZC 101 but I still wasn't there yet).
I used that print command you gave me above to verify I was working with the right template, and $products_format_name was the right variable. It wasn't mimicking the way the artist field was displaying values, but at least I now knew what code could produce the value I wanted and where it was getting placed. Experimenting with the code I knew was responsible for handling the artist field, and applying it to the format field ultimately worked. I apparently wasn't calling the values correctly, so they were either producing output I didn't want (like the "mess" I described earlier) or nothing at all. One attempt produced an all caps name from the DB next to the field value I wanted (so on the right track). I determined it was a confusion of using "media_format" vs. "format" in some of the values called in the script. The working combination was finally this in the tpl_product_music_info_display.php file:
Code:
<?php echo (($flag_show_product_music_info_media_format == 1 and !empty($products_format_name)) ? '<li>' . TEXT_PRODUCT_FORMAT . $products_format_name . '</li>' : '') . "\n";
?>
I had the same problem getting the Record Company field to appear (which is a field included in ZC but appears to be handled differently). The same process worked for that.
Quote:
based on the information provided, $products_format_name is correctly populated, and can be used on the template page. your title and OP says you are having difficulty displaying new product field data on product pages. and yet your code suggests that by echoing that var, you can safely use that element, which would say "vinyl". have you tried using that variable on the product info page? is that the correct value for that variable for this product?
That comment and your two questions were a major help because I'm still developing a concept of how ZC works.
Quote:
so you have incorrectly populated that table or incorrectly constructed the above sql statement.
in addition, the populating of the first table seems to be missing some fields as the date modified and date added have no values.
I think it was all of the above.
Quote:
with regards to not modifying core ZC files, good luck with that! (said sarcastically....) as i have previously stated, i do not think what you are trying to do is easy.
Especially as a beginner's lesson!
Quote:
and it is not handled easily in other shopping cart systems that provide overrides and field creation and the like.... my opinion previously stated is to put as much as possible in your own extra_functions file and then call needed functions, most likely from the template file to preserve as much of the ZC core as possible. and if you truly want to have an audit trail of what you are doing, i would put all source under a source version control system, ie git, so that you can see what you did. (mind you, learning git is a major job in and/of itself; but well worth the value in the long run...)
I've only had any real success with template overrides so far.
After straightening out the problem with calling the information to the page, the addition/correction of a configuration_title and configuration_key in the DB Table: product_type_layout fixed the problem I was having with layout controls (turning on /off).
I believe this mod has the full functionality I was looking for. I can enter the new field values when creating a new product. The field value shows up on the store page, handled the same way the artist field is, and is linkable/searchable. The Admin's layout controllers handle inclusion/exclusion as expected.
Thanks to you, mc12345678, and everyone else who offered suggestions in this and other threads. You really helped a beginner get through a brutal introductory lesson in modifying ZC.