Forums / Templates, Stylesheets, Page Layout / zen_products_lookup: How do I "return any field"?

zen_products_lookup: How do I "return any field"?

Locked

Views: 1,471

Results 1 to 3 of 3
This thread is locked. New replies are disabled.
22 Jun 2008, 7:39 PM
#1
molywerks avatar

molywerks

Zen Follower

Join Date:
Aug 2006
Posts:
136
Plugin Contributions:
0

zen_products_lookup: How do I "return any field"?

I found the code below in functions_lookup.php. Methinks I should be able to use it to great advantage to pass any field from the products or products_description tables to a module or tpl file. But how? is the question, yes!

/*
 * Return any field from products or products_description table
 * Example: zen_products_lookup('3', 'products_date_added');
 */
  function zen_products_lookup($product_id, $what_field = 'products_name', $language = '') {
    global $db;

    if (empty($language)) $language = $_SESSION['languages_id'];

    $product_lookup = $db->Execute("select " . $what_field . " as lookup_field
                              from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
                              where p.products_id ='" . (int)$product_id . "'
                              and pd.language_id = '" . (int)$language . "'");

    $return_field = $product_lookup->fields['lookup_field'];

    return $return_field;
  }

Let's say for a link's text I wanted to pass products_model to the also_purchased_products.php module instead of or in addition to passing the products_name. I tried substituting zen_products_lookup($product['id'], 'products_model') for $also_purchased_products->fields['products_name'] (see detail below) but that did not work. I looked at the various threads like How Do I Add the Model Number to My Cart? threads and found the inline method for templates but I'm having trouble translating this code to module use.

What am I doing wrong?

'text' => (($also_purchased_products->fields['products_image'] == '' and PRODUCTS_IMAGE_NO_IMAGE_STATUS == 0) ? '' : '<a href="' . zen_href_link(zen_get_info_page($also_purchased_products->fields['products_id']), 'products_id=' . $also_purchased_products->fields['products_id']) . '">' . zen_image(DIR_WS_IMAGES . $also_purchased_products->fields['products_image'], $also_purchased_products->fields['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br />') . '<a href="' . zen_href_link(zen_get_info_page($also_purchased_products->fields['products_id']), 'products_id=' . $also_purchased_products->fields['products_id']) . '">' . zen_products_lookup($product['id'], 'products_model') . '</a>');

22 Jun 2008, 8:03 PM
#2
justmetoo avatar

justmetoo

Zen Follower

Join Date:
Jun 2006
Posts:
31
Plugin Contributions:
0

Re: zen_products_lookup: How do I "return any field"?

If you wanted to know the products_model from a particular product, such as one from the also purchased, first you have to make sure that the products_id is available.

In this case, it is available as $also_purchased_products->fields['products_id']

To look up something with the function zen_products_lookup, this can be called by sending the function the products_id, or in this case, $also_purchased_products->fields['products_id'] and the name of the field you want returned, in this case, products_model.

The function is written as, function zen_products_lookup($product_id, $what_field = 'products_name', $language = '')

This means I need to pass it the values I want that either exist already or that I can get from some other function or by adapting the code.

echo 'What is the model? ' . zen_products_lookup($also_purchased_products->fields['products_id'], 'products_model');

would return to me the contents of the products_model for the products_id that is contained in the $also_purchased_products->fields['products_id'] value.

In another file, that value for the products_id might be written as $listing->fields['products_id'] so the same thing can be done by changing how the function is used, in this case

echo 'What is the model? ' . zen_products_lookup($listing->fields['products_id'] 'products_model');
22 Jun 2008, 8:32 PM
#3
molywerks avatar

molywerks

Zen Follower

Join Date:
Aug 2006
Posts:
136
Plugin Contributions:
0

Re: zen_products_lookup: How do I "return any field"?

Thank you for the best reply I've received in ages!

+1 to the Nth. :wink:

Your answer explained with examples of how to pass and send and helps me understand so much more about how to take advantage of Zen Cart and not just in this area. It was the big hurdle I needed help with to be able to do so very much more with Zen Cart. And I'll probably now go hog wild writing functions, too. I've already done one!

Again, thank you.

:clap: