Newer
Older
* Block content based upon popular products this month
*/
/**
* Build up a query based upon settings and retrieve products.
*/
function commerce_popular_products_get_product() {
$output = '';
$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');
Anthony Lindsay
committed
if (isset($products)) {
// Gather information on all the Commerce product reference fields in use.
Anthony Lindsay
committed
$fields = field_info_fields();
$product_references = array();
foreach ($fields as $key => $field) {
if ($field['type'] == 'commerce_product_reference') {
if ($key != 'commerce_product') {
$product_references[] = $key;
}
Anthony Lindsay
committed
// Turn the time period into something we can use with FROM_UNIXTIME().
$time_period = '';
Anthony Lindsay
committed
$time_period = 'D';
Anthony Lindsay
committed
$time_period = 'u';
Anthony Lindsay
committed
$time_period = 'M';
Anthony Lindsay
committed
case 'year':
$time_period = 'Y';
break;
Anthony Lindsay
committed
// Create an array to hold all our results for different product types.
Anthony Lindsay
committed
foreach ($product_references as $table) {
// Build query to find the most bought product for the current month.
$query = db_select('commerce_order', 'co');
$query->join('commerce_line_item', 'cli', 'cli.order_id = co.order_id');
$query->join('commerce_product', 'cp', 'cp.sku = cli.line_item_label');
// Add in joins to field data tables for all the product reference fields.
$query->rightJoin('field_data_' . $table, $table, $table . '.' . $table . '_product_id = cp.product_id');
$query->addField($table, 'entity_id', 'entity_id');
// Continue building the query.
$query->addExpression(
'FROM_UNIXTIME(co.changed, :M1)',
'order_time',
Anthony Lindsay
committed
$query->fields('cp', array('product_id', 'title'));
$query->addExpression('SUM(cli.quantity)', 'sum');
Anthony Lindsay
committed
array('pending', 'completed', 'checkout_complete'),
Anthony Lindsay
committed
$product_types = array();
foreach ($products as $key => $product) {
$product_types[] = $product;
}
$query->condition('cli.type', $product_types);
// Deal with the different time periods.
Anthony Lindsay
committed
switch ($time) {
case 'day':
$query->where("co.changed > (unix_timestamp(now()) - 86400) ");
Anthony Lindsay
committed
break;
Anthony Lindsay
committed
case 'week':
$query->where("co.changed > (unix_timestamp(now()) - 604800) ");
Anthony Lindsay
committed
break;
Anthony Lindsay
committed
case 'month':
$query->where("co.changed > (unix_timestamp(now()) - 2629743) ");
Anthony Lindsay
committed
break;
Anthony Lindsay
committed
$query->groupBy('order_time');
$query->groupBy('cp.product_id');
$query->groupBy('cp.title');
$result = $query->execute();
foreach ($result as $record) {
$product = $record->entity_id;
$count = $record->sum;
$all_results[] = array('id' => $product, 'count' => $count);
Anthony Lindsay
committed
$count = array();
foreach ($all_results as $key => $row) {
$count[$key] = $row['count'];
}
array_multisort($count, SORT_DESC, $all_results);
Anthony Lindsay
committed
$i = 0;
Anthony Lindsay
committed
$view_count = 0;
Anthony Lindsay
committed
while ($i < $number) {
// For each returned row, get the product's ID and go fetch the View.
Anthony Lindsay
committed
if (isset($all_results[$i])) {
$product = $all_results[$i]['id'];
$view = views_embed_view('popular_commerce_products', 'popular_products_block', $product);
$output .= $view;
Anthony Lindsay
committed
}
Anthony Lindsay
committed
$i++;
}
Anthony Lindsay
committed
if ($view_count == 0) {
$output = '';
}
Anthony Lindsay
committed
return $output;
}
}
/**
* Implements hook_block_info().
*/
function commerce_popular_products_block_info() {
// A block To display a popular product.
$blocks['popular-product-block'] = array(
Anthony Lindsay
committed
'info' => t('Commerce Popular Products display block'),
'weight' => '-50',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function commerce_popular_products_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'popular-product-block':
$title = variable_get('commerce_popular_products_title');
$title = check_plain($title);
$block['subject'] = t($title);
$block['content'] = commerce_popular_products_get_product();
}
return $block;
}
/**
* Implements hook_menu().
*/
function commerce_popular_products_menu() {
$items['admin/commerce/config/popular-products'] = array(
'title' => 'Popular Commerce Products settings',
'description' => 'Manage settings for defining a popular products block.',
'page callback' => 'commerce_popular_products_settings',
'access arguments' => array('Configure store settings '),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Menu callback to produce the Settings Page.
*/
function commerce_popular_products_settings() {
$output = t('<p>Here you can define which product types are ranked for popularity & the period over which they are ranked.</p>');
$form = drupal_get_form('commerce_popular_products_settings_form');
return $output;
}
/**
* Implements hook_form().
* Produces the Settings Page form.
*/
function commerce_popular_products_settings_form($form, &$form_state) {
$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');
$title = variable_get('commerce_popular_products_title', 'Popular Products This Month');
Anthony Lindsay
committed
$product_types = commerce_popular_products_get_product_types();
$form['block_title'] = array(
'#type' => 'textfield',
'#title' => t('Block title.'),
'#description' => t('The title of the "popular products" block as shown to users.'),
'#default_value' => $title,
Anthony Lindsay
committed
if (isset($product_types)) {
Anthony Lindsay
committed
$form['product_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Product Types.'),
'#description' => t('Product nodes to include in the popularity check.'),
);
}
else {
$form['product_types'] = array(
'#title' => t('Product Types.'),
Anthony Lindsay
committed
'#markup' => t('<h4>Product types</h4><p>You have not defined any product types. You need to <a href = "/admin/commerce/products/types/add">define some</a> first.</p>'),
Anthony Lindsay
committed
);
}
Anthony Lindsay
committed
Anthony Lindsay
committed
$options = array();
foreach ($product_types as $type) {
$options[$type] = $type;
}
Anthony Lindsay
committed
if (empty($products)) {
$products = array();
foreach ($product_types as $type) {
$products[$type] = 0;
}
}
$form['product_types']['#default_value'] = $products;
$form['time_period'] = array(
'#type' => 'radios',
'#title' => t('Timeframe.'),
'#description' => t('The timeframe within which to calculate popularity.'),
'#options' => array(
'day' => t('Day'),
'week' => t('Week'),
'month' => t('Month'),
'year' => t('Year'),
),
'#default_value' => $time,
$form['number_display'] = array(
'#type' => 'select',
'#title' => t('Display.'),
'#description' => t('How may ranked popular products would you like included in the display?'),
'#options' => array(
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
),
'#default_value' => $number,
Anthony Lindsay
committed
$form['submit'] = array('#type' => 'submit', '#value' => t('Save'));;
return $form;
}
/**
* Custom submit handler for settings form: saves values to Variable table.
*/
function commerce_popular_products_settings_form_submit($form, &$form_state) {
$values = $form_state['values'];
$products = $values['product_types'];
$time = $values['time_period'];
$number = $values['number_display'];
$title = $values['block_title'];
variable_set('commerce_popular_products_title', $title);
variable_set('commerce_popular_products_time', $time);
variable_set('commerce_popular_products_number', $number);
variable_set('commerce_popular_products_products', $products);
drupal_set_message(t('Your settings have been saved.'));
}
/**
* A simple query to return all the Commerce product types in the database.
*/
function commerce_popular_products_get_product_types() {
Anthony Lindsay
committed
$names = array();
Anthony Lindsay
committed
$products = commerce_product_types();
foreach ($products as $item) {
$names[] = $item['name'];
}
return $names;
}
/**
* Implements hook_views_api().
*
* Makes hooking into Views possible.
*/
function commerce_popular_products_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'commerce_popular_products'),
);