Commit 153c5544 authored by anthonylindsay's avatar anthonylindsay

Made popular products function save to a variable on checkout completion and...

Made popular products function save to a variable on checkout completion and block view function retrieve it from the variable.
parent 2c291182
......@@ -5,14 +5,20 @@
* Block content based upon popular products this month
*/
/**
* Implements hook_commerce_checkout_complete()
*/
function commerce_popular_products_commerce_checkout_complete($order) {
// Update our cache of popular products.
commerce_popular_products_get_product();
}
/**
* Build up a query based upon settings and retrieve products.
*/
function commerce_popular_products_get_product() {
$output = '';
// Fetch settings for use.
$time = variable_get('commerce_popular_products_time', $default = 'month');
$number = variable_get('commerce_popular_products_number', $default = '1');
$products = variable_get('commerce_popular_products_products');
if (isset($products)) {
// Gather information on all the Commerce product reference fields in use.
......@@ -104,22 +110,8 @@ function commerce_popular_products_get_product() {
}
array_multisort($count, SORT_DESC, $all_results);
// Call the views to display N items.
$i = 0;
$view_count = 0;
while ($i < $number) {
// For each returned row, get the product's ID and go fetch the View.
if (isset($all_results[$i])) {
$product = $all_results[$i]['id'];
$view = views_embed_view('popular_commerce_products', 'popular_products_block', $product);
$output .= $view;
$view_count++;
}
$i++;
}
if ($view_count == 0) {
$output = '';
}
return $output;
variable_set('commerce_popular_products_popular_products', $all_results);
return $all_results;
}
}
......@@ -145,13 +137,41 @@ function commerce_popular_products_block_view($delta = '') {
$title = variable_get('commerce_popular_products_title');
$title = check_plain($title);
$block['subject'] = t($title);
$block['content'] = commerce_popular_products_get_product();
$block['content'] = commerce_popular_products_get_content();
break;
}
return $block;
}
/**
* Helper function to produce block content.
*/
function commerce_popular_products_get_content() {
$output = '';
$number = variable_get('commerce_popular_products_number', $default = '1');
$all_results = variable_get('commerce_popular_products_popular_products');
if ($all_results == null) {
$all_results = commerce_popular_products_get_product();
}
$i = 0;
$view_count = 0;
while ($i < $number) {
// For each returned row, get the product's ID and go fetch the View.
if (isset($all_results[$i])) {
$product = $all_results[$i]['id'];
$view = views_embed_view('popular_commerce_products', 'popular_products_block', $product);
$output .= $view;
$view_count++;
}
$i++;
}
if ($view_count == 0) {
$output = '';
}
return $output;
}
/**
* Implements hook_menu().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment