5 replies [Last post]
Sansui's picture
Offline
Joined: 06/05/2008
Juice: 154
Was this information Helpful?

What I want to do is have the special offers block display one product, randomly chosen. I've made some initial modifications below to put in my template.php file, and so far so good. First problem is, I know that "order by rand" is a terrible thing to put in SQL queries, but I have no idea how to do it the 'correct' way.

Second problem is that I don't want this product to display the add to cart button (which is designated in the tapir table for uc_catalog_products) - is there any way to add new tapir tables that I could then reference in this function for theming? Or alternatively a different an easy way to disable fields without changing the global tapir settings for that table?

Thanks in advance!

<?php
/**
* New Theme function for special offer products list
*/
function phptemplate_uc_marketing_special_products() {
 
$product_types = module_invoke_all('product_types');
 
$gridwidth = variable_get('uc_catalog_grid_display_width', 3);
  if (
variable_get('uc_catalog_grid_display', false)) {
   
$gridlines = variable_get('uc_marketing_special_block_lines', 1);
   
$gridwidth = $gridwidth * $gridlines;
  }

 

$products = array();
 
$sql = "SELECT DISTINCT(n.nid), n.title, p.sell_price
    FROM {node} n
      INNER JOIN {uc_products} AS p ON n.nid = p.nid
    WHERE n.status = 1
      AND n.sticky = 1
      AND n.type IN ('"
. implode("','", $product_types) ."')
    ORDER BY RAND()"
;

 

$result = db_query_range($sql, 0, '1');
  while (
$node = db_fetch_object($result)) {
   
$products[] = $node->nid;
  }

  if (

count($products)) {
   
$output .= '<h2 class="title special">' . variable_get('uc_marketing_special_block_title', t('Special Offers')) . '</h2>';
   
$output .= theme('uc_catalog_products', $products);
    if (
variable_get('uc_marketing_special_block_help_allowed', true)) {
     
$output .= '<p class="special description">' . variable_get('uc_marketing_special_block_help', t("We're always looking for new ways to bring our products to the broadest audience. Here are our special offers for this month.")) . '</p>';
    }
  }
  return
$output;
}
?>
Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Creating override for Special Offers function and ordering r

To be honest, I think I'd do this through a custom View if I were you. Then you can easily make it a block displayed in a normal Views format or in a grid format using the Views Bonus Pack module. I know it's not quite the answer you were looking for, it's just the way I would go about it.

Sansui's picture
Offline
Joined: 06/05/2008
Juice: 154
Re: Re: Creating override for Special Offers function and orderi

Ah, sure, I think I'll try and give that a whirl. Just seemed convenient to use the existing stuff, but maybe not Smiling

Sansui's picture
Offline
Joined: 06/05/2008
Juice: 154
Re: Re: Re: Creating override for Special Offers function and or

Views worked beautifully... not sure why I didn't think to do that in the first place! Thanks for the suggestion Smiling

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Re: Re: Creating override for Special Offers function an

Awesome. Cool

Sansui's picture
Offline
Joined: 06/05/2008
Juice: 154
Re: Re: Re: Re: Re: Creating override for Special Offers functio

Trying to create a block view for bestsellers as well, but I don't see any sort criteria for number of item sold. Is that one not something that can be recreated in views?