summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnthony Lindsay2012-09-15 20:02:28 (GMT)
committerAnthony Lindsay2012-09-15 20:02:28 (GMT)
commit153c5544caa30ef0b2a4314b25e5d8b9a92ad6cd (patch)
treede2b29bb068435a205995d8ddbfb477a9b2d6426
parent2c291182f0ead3ec8bc02945b637aba2047ef10d (diff)
Made popular products function save to a variable on checkout completion and block view function retrieve it from the variable.
-rw-r--r--commerce_popular_products.module58
1 files changed, 39 insertions, 19 deletions
diff --git a/commerce_popular_products.module b/commerce_popular_products.module
index ee95e0d..8c9c7ab 100644
--- a/commerce_popular_products.module
+++ b/commerce_popular_products.module
@@ -6,13 +6,19 @@
*/
/**
+ * 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,7 +137,7 @@ 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;
}
@@ -153,6 +145,34 @@ function commerce_popular_products_block_view($delta = '') {
}
/**
+ * 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().
*/
function commerce_popular_products_menu() {