Skip to content

Commit

Permalink
Merge pull request #1144 from briansandall/featured_efficient
Browse files Browse the repository at this point in the history
Resolves issue #976
  • Loading branch information
abrookbanks committed Sep 20, 2016
2 parents 42cc9db + 3c4a7dc commit 9b5ce31
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 66 deletions.
3 changes: 2 additions & 1 deletion admin/skins/default/templates/products.index.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@
<div><label for="product_code">{$LANG.catalogue.product_code}</label><span><input name="product_code" id="product_code" class="textbox" type="text" value="{$PRODUCT.product_code}"></span></div>
<div><label for="product_code_auto">{$LANG.catalogue.product_code_auto}</label><span><input name="product_code_auto" id="product_code_auto" type="hidden" class="toggle" {$PRODUCT.auto_code_checked}> <input name="product_code_old" id="product_code_old" type="hidden" value=""> </span></div>
<div><label for="product_weight">{$LANG.catalogue.product_weight}</label><span><input name="product_weight" id="product_weight" class="textbox number" type="text" value="{$PRODUCT.product_weight}"></span></div>
<div><label for="product_featured">{$LANG.catalogue.product_latest}</label><span><input type="hidden" name="featured" id="product_featured" class="toggle" value="{$PRODUCT.featured}"></span></div>
<div><label for="product_featured">{$LANG.catalogue.product_featured}</label><span><input type="hidden" name="featured" id="product_featured" class="toggle" value="{$PRODUCT.featured}"></span></div>
<div><label for="product_latest">{$LANG.catalogue.product_latest}</label><span><input type="hidden" name="latest" id="product_latest" class="toggle" value="{$PRODUCT.latest}"></span></div>
<div><label for="available">{$LANG.catalogue.available_for_purchase}</label><span><input type="hidden" name="available" id="available" class="toggle" value="{if isset($PRODUCT.available)}{$PRODUCT.available}{else}1{/if}"></span></div>
</fieldset>
<fieldset>
Expand Down
4 changes: 2 additions & 2 deletions admin/sources/products.export.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ function download_parts($format = 'cubecart', $no_rows, $per_page) {

if ($results = $GLOBALS['db']->query($query, $per_page, $page)) {

$header_fields = array('Product Name', 'Status', 'Include in latest products', 'Product Code', 'Weight', 'Description', 'Short Description', 'Price', 'Sale Price', 'Cost Price', 'Tax Class', 'Tax Inclusive', 'Main Image', 'Stock Level', 'Use Stock Level', 'Stock Level Warning', 'Master Category ID', 'Manufacturer', 'UPC Code', 'EAN Code', 'JAN Code', 'ISBN Code', 'Brand', 'MPN Code', 'GTIN Code', 'Meta Title', 'Meta Keywords', 'Meta Description', 'Condition', 'Digital', 'Digital Path (Legacy)');
$fields = array('name', 'status', 'featured', 'product_code', 'product_weight', 'description', 'description_short', 'price', 'sale_price', 'cost_price', 'tax_type', 'tax_inclusive', 'image', 'stock_level', 'use_stock_level', 'stock_warning', 'cat_id', 'manufacturer', 'upc', 'ean', 'jan', 'isbn', 'brand', 'mpn', 'gtin', 'seo_meta_title', 'seo_meta_description', 'seo_meta_keywords', 'condition', 'digital', 'digital_path');
$header_fields = array('Product Name', 'Status', 'Include in featured products', 'Include in latest products', 'Product Code', 'Weight', 'Description', 'Short Description', 'Price', 'Sale Price', 'Cost Price', 'Tax Class', 'Tax Inclusive', 'Main Image', 'Stock Level', 'Use Stock Level', 'Stock Level Warning', 'Master Category ID', 'Manufacturer', 'UPC Code', 'EAN Code', 'JAN Code', 'ISBN Code', 'Brand', 'MPN Code', 'GTIN Code', 'Meta Title', 'Meta Keywords', 'Meta Description', 'Condition', 'Digital', 'Digital Path (Legacy)');
$fields = array('name', 'status', 'featured', 'latest', 'product_code', 'product_weight', 'description', 'description_short', 'price', 'sale_price', 'cost_price', 'tax_type', 'tax_inclusive', 'image', 'stock_level', 'use_stock_level', 'stock_warning', 'cat_id', 'manufacturer', 'upc', 'ean', 'jan', 'isbn', 'brand', 'mpn', 'gtin', 'seo_meta_title', 'seo_meta_description', 'seo_meta_keywords', 'condition', 'digital', 'digital_path');
$delimiter = ',';
$extension = 'csv';
$glue = "\n";
Expand Down
7 changes: 4 additions & 3 deletions admin/sources/products.import.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@
if(!isset($product_record['product_code']) || empty($product_record['product_code'])) {
$product_record['product_code'] = generate_product_code($product_record['name']);
}
if(!isset($product_record['featured']) || empty($product_record['featured'])) {
$product_record['featured'] = 1;
if(!isset($product_record['latest']) || empty($product_record['latest'])) {
$product_record['latest'] = 1;
}
// If no stock level is set we assume no stock control is used
if(isset($product_record['use_stock_level']) && ($product_record['use_stock_level']==1 || strtolower($product_record['use_stock_level'])=='true')) {
Expand Down Expand Up @@ -311,7 +311,8 @@
'digital_path' => $lang['catalogue']['file_path'],
'tax_type' => $lang['catalogue']['tax_class'],
'tax_inclusive' => $lang['catalogue']['tax_inclusive'],
'featured' => $lang['catalogue']['product_latest'],
'featured' => $lang['catalogue']['product_featured'],
'latest' => $lang['catalogue']['product_latest'],
'seo_path' => $lang['settings']['seo_path'],
'seo_meta_title' => $lang['settings']['seo_meta_title'],
'seo_meta_keywords' => $lang['settings']['seo_meta_keywords'],
Expand Down
3 changes: 2 additions & 1 deletion admin/sources/products.index.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,8 @@
// Breadcrumb
$GLOBALS['gui']->addBreadcrumb($lang['catalogue']['product_add'], $_GET);
$result[0] = array(
'featured' => 1,
'featured' => 0,
'latest' => 1,
'tax_inclusive' => 0,
'use_stock_level' => 1,
);
Expand Down
2 changes: 1 addition & 1 deletion classes/cubecart.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function displayHomePage() {

$products = array();

$where = $GLOBALS['catalogue']->outOfStockWhere(array('status' => '1', 'featured' => '1'));
$where = $GLOBALS['catalogue']->outOfStockWhere(array('status' => '1', 'latest' => '1'));

if ($GLOBALS['config']->get('config', 'catalogue_latest_products')) {
$latestProducts = $GLOBALS['db']->select('CubeCart_inventory', false, $where, array('date_added' => 'DESC', 'product_id' => 'DESC'), (int)$GLOBALS['config']->get('config', 'catalogue_latest_products_count'));
Expand Down
112 changes: 55 additions & 57 deletions classes/gui.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1099,77 +1099,75 @@ private function _displayPopularProducts() {
}

/**
* Display random products box
* Display featured products box
*/
private function _displayRandomProduct($p=0) {
private function _displayRandomProduct() {

if(!$GLOBALS['smarty']->templateExists('templates/box.featured.php')) return false;

foreach ($GLOBALS['hooks']->load('class.gui.display_random_product_pre') as $hook) include $hook;

$where = $GLOBALS['catalogue']->outOfStockWhere(array('status' => '1'));
// SQL below used to replace RAND which is a monster on resources over 100 products
$query = 'SELECT * FROM `'.$GLOBALS['config']->get('config', 'dbprefix').'CubeCart_inventory` JOIN (SELECT CEIL(RAND() * (SELECT MAX(`product_id`) FROM `'.$GLOBALS['config']->get('config', 'dbprefix').'CubeCart_inventory`)) AS `product_id`) AS `r` USING (`product_id`) WHERE '.$where;
$random_product = $GLOBALS['db']->misc($query, false);

if ($random_product) {

$category_data = $GLOBALS['catalogue']->getCategoryStatusByProductID($random_product[0]['product_id']);
$category_status = false;
if (is_array($category_data)) {
foreach ($category_data as $trash => $data) {
if ($data['status'] == 1 && $data['primary'] == 1) {
$category_status = true;
$where = $GLOBALS['catalogue']->outOfStockWhere(array('status' => '1', 'featured' => '1'));
$query = 'SELECT `product_id` FROM `'.$GLOBALS['config']->get('config', 'dbprefix').'CubeCart_inventory` WHERE '.$where;
$featured_products = $GLOBALS['db']->misc($query, false);
$n = ($featured_products ? count($featured_products) : 0);
if ($n > 0) {
$random_product = false;
$tries = 0;
while (!$random_product && $tries < 15) {
$random_id = $featured_products[mt_rand(0, $n - 1)]['product_id'];
$category_data = $GLOBALS['catalogue']->getCategoryStatusByProductID($random_id);
$category_status = false;
if (is_array($category_data)) {
foreach ($category_data as $trash => $data) {
if ($data['status'] == 1 && $data['primary'] == 1) {
$category_status = true;
break;
}
}
}
}
if (!$category_status) {
if ($p<15) {
$p++;
$this->_displayRandomProduct($p);
if ($category_status) {
$random_product = $GLOBALS['db']->select('CubeCart_inventory', false, array('product_id' => $random_id), false, 1);
}
} else {
++$tries;
}
}

$image = $this->getProductImage($random_product[0]['product_id']);
$product = $random_product[0];

$GLOBALS['language']->translateProduct($product);

$product['image'] = $image;

$product['ctrl_sale'] = (!$GLOBALS['tax']->salePrice($product['price'], $product['sale_price']) || !$GLOBALS['config']->get('config', 'catalogue_sale_mode')) ? false : true;


$GLOBALS['catalogue']->getProductPrice($product);
$sale = $GLOBALS['tax']->salePrice($product['price'], $product['sale_price']);
$product['price_unformatted'] = $product['price'];
$product['sale_price_unformatted'] = ($sale) ? $product['sale_price'] : null;
$product['price'] = $GLOBALS['tax']->priceFormat($product['price']);
$product['sale_price'] = ($sale) ? $GLOBALS['tax']->priceFormat($product['sale_price']) : null;

$product['ctrl_purchase'] = true;
if ($product['use_stock_level']) {
// Get Stock Level
$stock_level = $GLOBALS['catalogue']->getProductStock($product['product_id']);
if ((int)$stock_level <= 0) {
// Out of Stock
if (!$GLOBALS['config']->get('config', 'basket_out_of_stock_purchase')) {
// Not Allowed
$product['ctrl_purchase'] = false;
}
if ($random_product) {
$image = $this->getProductImage($random_product[0]['product_id']);
$product = $random_product[0];

$GLOBALS['language']->translateProduct($product);

$product['image'] = $image;

$product['ctrl_sale'] = (!$GLOBALS['tax']->salePrice($product['price'], $product['sale_price']) || !$GLOBALS['config']->get('config', 'catalogue_sale_mode')) ? false : true;

$GLOBALS['catalogue']->getProductPrice($product);
$sale = $GLOBALS['tax']->salePrice($product['price'], $product['sale_price']);
$product['price_unformatted'] = $product['price'];
$product['sale_price_unformatted'] = ($sale) ? $product['sale_price'] : null;
$product['price'] = $GLOBALS['tax']->priceFormat($product['price']);
$product['sale_price'] = ($sale) ? $GLOBALS['tax']->priceFormat($product['sale_price']) : null;

$product['ctrl_purchase'] = true;
if ($product['use_stock_level']) {
// Get Stock Level
$stock_level = $GLOBALS['catalogue']->getProductStock($product['product_id']);
if ((int)$stock_level <= 0) {
// Out of Stock
if (!$GLOBALS['config']->get('config', 'basket_out_of_stock_purchase')) {
// Not Allowed
$product['ctrl_purchase'] = false;
}
}
$product['url'] = $GLOBALS['seo']->buildURL('prod', $product['product_id']);
foreach ($GLOBALS['hooks']->load('class.gui.display_random_product') as $hook) include $hook;
$GLOBALS['smarty']->assign('featured', $product);
$content = $GLOBALS['smarty']->fetch('templates/box.featured.php');
$GLOBALS['smarty']->assign('RANDOM_PROD', $content);
}
} elseif ($p<20) { // Math to generate random product might give a product_id that doesn't exist or doen't meet WHERE criteria
$p++;
$this->_displayRandomProduct($p);
$product['url'] = $GLOBALS['seo']->buildURL('prod', $product['product_id']);
foreach ($GLOBALS['hooks']->load('class.gui.display_random_product') as $hook) include $hook;
$GLOBALS['smarty']->assign('featured', $product);
$content = $GLOBALS['smarty']->fetch('templates/box.featured.php');
$GLOBALS['smarty']->assign('RANDOM_PROD', $content);
}
if($random_product) return $product;
}

/**
Expand Down
1 change: 1 addition & 0 deletions language/definitions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@
<string name="product_details" introduced="5.0.0"><![CDATA[Product Details]]></string>
<string name="product_description" introduced="5.0.0"><![CDATA[Product Description]]></string>
<string name="product_doesnt_exist" introduced="5.0.0"><![CDATA[That product couldn't be found.]]></string>
<string name="product_featured" introduced="6.0.10"><![CDATA[Include in featured products]]></string>
<string name="product_id" introduced="5.0.0"><![CDATA[Product Id]]></string>
<string name="product_info" introduced="5.0.0"><![CDATA[Product Information]]></string>
<string name="product_latest" introduced="5.0.0"><![CDATA[Include in latest products]]></string>
Expand Down
3 changes: 2 additions & 1 deletion setup/db/install/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ CREATE TABLE IF NOT EXISTS `CubeCart_inventory` (
`product_weight` decimal(10,3) DEFAULT NULL COMMENT 'Product Weight',
`tax_type` int(10) unsigned DEFAULT NULL COMMENT 'Tax Type',
`tax_inclusive` tinyint(1) unsigned DEFAULT '0' COMMENT 'Price inclusive of tax',
`featured` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT 'Included on Homepage',
`featured` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT 'Featured product',
`latest` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT 'Included on Homepage',
`seo_meta_title` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'SEO Meta Title',
`seo_meta_description` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'SEO Meta Description',
`seo_meta_keywords` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'SEO Meta Keywords',
Expand Down
3 changes: 3 additions & 0 deletions setup/db/upgrade/6.1.0.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ALTER TABLE `CubeCart_inventory` CHANGE COLUMN `featured` `featured` tinyint(1) unsigned NOT NULL DEFAULT 0 COMMENT 'Featured product'; #EOQ
ALTER TABLE `CubeCart_inventory` ADD COLUMN `latest` tinyint(1) unsigned NOT NULL DEFAULT 1 COMMENT 'Included on Homepage' AFTER `featured`; #EOQ
UPDATE `CubeCart_inventory` SET `latest`=`featured`; #EOQ
ALTER TABLE `CubeCart_order_history` ADD `initiator` CHAR(1) NOT NULL DEFAULT 'G'; #EOQ
UPDATE `CubeCart_order_history` SET `initiator` = 'U'; #EOQ
CREATE TABLE `CubeCart_email_log` (
Expand Down

0 comments on commit 9b5ce31

Please sign in to comment.