Created
November 17, 2015 01:03
-
-
Save MichaelThessel/f7caca1d84b3367da888 to your computer and use it in GitHub Desktop.
SNS Avaz Producttabs performance fixes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/code/local/Sns/Producttabs/Block/Grid.php b/app/code/local/Sns/Producttabs/Block/Grid.php | |
index 2bee3dd..cc43c3b 100644 | |
--- a/app/code/local/Sns/Producttabs/Block/Grid.php | |
+++ b/app/code/local/Sns/Producttabs/Block/Grid.php | |
@@ -1,15 +1,32 @@ | |
<?php | |
-class Sns_Producttabs_Block_Grid extends Sns_Producttabs_Block_List{ | |
- protected function _toHtml(){ | |
+class Sns_Producttabs_Block_Grid extends Sns_Producttabs_Block_List { | |
+ public function __construct($attributes = array()) { | |
+ parent::__construct(); | |
+ | |
+ $this->_config = Mage::helper('producttabs/data')->get($attributes); | |
+ | |
+ $request = Mage::app()->getRequest(); | |
+ $cacheKey = 'producttabs_grid_'; | |
+ $cacheKey .= $request->isAjax() ? 'ajax_' : 'noajax_'; | |
+ $cacheKey .= $request->getActionName() . '_'; | |
+ $cacheKey .= md5(serialize($this->_config) . serialize($request->getParams())); | |
+ | |
+ $this->addData(array( | |
+ 'cache_lifetime' => 86400, | |
+ 'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG, Mage_Catalog_Model_Category::CACHE_TAG), | |
+ 'cache_key' => $cacheKey, | |
+ )); | |
+ } | |
+ | |
+ protected function _prepareLayout(){ | |
if(!$this->getConfig('isenabled')) return; | |
- $type = Mage::app()->getRequest()->getParam('pdt_type'); | |
- if( $type!='' ){ | |
+ $type = Mage::app()->getRequest()->getParam('pdt_type'); | |
+ if ($type != '') { | |
$template_file = 'sns/producttabs/items.phtml'; | |
- }else{ | |
- if($this->getTemplate()) $template_file = $this->getTemplate(); | |
+ } else { | |
+ if ($this->getTemplate()) $template_file = $this->getTemplate(); | |
else $template_file = 'sns/producttabs/default.phtml'; | |
} | |
$this->setTemplate($template_file); | |
- return parent::_toHtml(); | |
} | |
} | |
diff --git a/app/code/local/Sns/Producttabs/Block/List.php b/app/code/local/Sns/Producttabs/Block/List.php | |
index e136d45..a1b9d32 100644 | |
--- a/app/code/local/Sns/Producttabs/Block/List.php | |
+++ b/app/code/local/Sns/Producttabs/Block/List.php | |
@@ -7,10 +7,24 @@ protected function _construct() { | |
parent::_construct(); | |
} | |
- public function __construct($attributes = array()){ | |
+ public function __construct($attributes = array()) { | |
+ | |
parent::__construct(); | |
$this->_config = Mage::helper('producttabs/data')->get($attributes); | |
+ | |
+ $request = Mage::app()->getRequest(); | |
+ $cacheKey = 'producttabs_list_'; | |
+ $cacheKey .= $request->isAjax() ? 'ajax_' : 'noajax_'; | |
+ $cacheKey .= $request->getActionName() . '_'; | |
+ $cacheKey .= md5(serialize($this->_config) . serialize($request->getParams())); | |
+ | |
+ $this->addData(array( | |
+ 'cache_lifetime' => 86400, | |
+ 'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG, Mage_Catalog_Model_Category::CACHE_TAG), | |
+ 'cache_key' => $cacheKey, | |
+ )); | |
} | |
+ | |
public function _isAjax() | |
{ | |
$isAjax = Mage::app()->getRequest()->isAjax(); | |
@@ -76,7 +90,7 @@ public function getStoreId(){ | |
public function setStoreId($storeId=null){ | |
$this->_storeId = $storeId; | |
} | |
- protected function getProductCollection($product_sort_by=''){ | |
+ protected function getProductCollection($product_sort_by = ''){ | |
$collection = Mage::getSingleton('catalog/product')->getCollection(); | |
$collection->addAttributeToSelect('*'); | |
$collection->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED); | |
@@ -87,23 +101,24 @@ protected function getProductCollection($product_sort_by=''){ | |
$collection->addAttributeToFilter('visibility', $visibility); | |
$sort_by = explode(',', $this->_config['order_type']); | |
- if($product_sort_by==""){ | |
- if( !empty($sort_by[0]) ){ | |
+ if ($product_sort_by == ''){ | |
+ if(!empty($sort_by[0])){ | |
$product_sort_by = $sort_by[0]; | |
- }else{ | |
+ } else { | |
$product_sort_by = 'created_at'; | |
} | |
} | |
// add more attribute | |
- if ( $product_sort_by=='top_rating' || $product_sort_by=='most_reviewed' ){ //echo "dung<br/>"; | |
+ if ($product_sort_by=='top_rating' || $product_sort_by=='most_reviewed') { | |
$this->_addReviewsCount($collection); | |
} | |
- if ( $product_sort_by=='most_viewed' ){ | |
+ if ($product_sort_by=='most_viewed') { | |
$this->_addViewsCount($collection); | |
} | |
- if ( $product_sort_by=='best_sales' ){ | |
+ if ($product_sort_by=='best_sales') { | |
$this->_addOrderedCount($collection); | |
} | |
+ | |
return $collection; | |
} | |
@@ -340,7 +355,9 @@ public function getProductsCat($catid, $isTotal=0){ | |
} | |
} | |
} | |
+ | |
if (isset($category_ids) && count($category_ids)>0) $this->_addCategoryFilter($collection, $category_ids); | |
+ | |
// Sort products in collection | |
$dir = strtolower( $options->product_order_dir ); | |
if (!in_array($dir, array('asc', 'desc'))){ | |
@@ -481,26 +498,36 @@ private function _addReviewsCount(& $collection, $reviews_count_alias="sns_revie | |
) | |
); | |
} | |
+ | |
private function _addOrderedCount(& $collection, $ordered_qty_alias="sns_ordered_count"){ | |
$order_table = Mage::getSingleton('core/resource')->getTableName('sales/order'); | |
$read = Mage::getSingleton('core/resource')->getConnection ('core_read'); | |
- $orders_active_query = $read->select()->from(array('o_table'=>$order_table), 'o_table.entity_id')->where("o_table.state<>'" . Mage_Sales_Model_Order::STATE_CANCELED . "'"); | |
+ | |
+ $orders_active_query = $read->select() | |
+ ->from(array('o_table'=>$order_table), 'o_table.entity_id') | |
+ ->where("o_table.state <> '" . Mage_Sales_Model_Order::STATE_CANCELED . "'") | |
+ ->limit(1000) | |
+ ->order('o_table.entity_id DESC'); | |
+ | |
$res = $orders_active_query->query()->fetchAll(); | |
+ | |
$order_ids = array(); | |
- if ( count($res) ){ | |
- foreach($res as $row){ | |
+ if (count($res)) { | |
+ foreach($res as $row) { | |
array_key_exists('entity_id', $row) && array_push($order_ids, $row['entity_id']); | |
} | |
} | |
+ | |
$order_item_table = Mage::getSingleton('core/resource')->getTableName('sales/order_item'); | |
$collection->getSelect()->join( | |
- array('oi_table' => $order_item_table), | |
- 'e.entity_id=oi_table.product_id'.(count($order_ids) ? ' AND oi_table.order_id IN('.implode(',', $order_ids).')' : ''), | |
- array( | |
- $ordered_qty_alias => 'SUM(oi_table.qty_ordered)' | |
- ) | |
+ array('oi_table' => $order_item_table), | |
+ 'e.entity_id=oi_table.product_id' . (count($order_ids) ? ' AND oi_table.order_id IN('.implode(',', $order_ids).')' : ''), | |
+ array( | |
+ $ordered_qty_alias => 'SUM(oi_table.qty_ordered)' | |
+ ) | |
); | |
$collection->getSelect()->group('e.entity_id'); | |
+ | |
return $this; | |
} | |
diff --git a/app/code/local/Sns/Producttabs/controllers/IndexController.php b/app/code/local/Sns/Producttabs/controllers/IndexController.php | |
index 67e99da..1e45e54 100644 | |
--- a/app/code/local/Sns/Producttabs/controllers/IndexController.php | |
+++ b/app/code/local/Sns/Producttabs/controllers/IndexController.php | |
@@ -8,18 +8,15 @@ public function ajaxAction() { | |
$layout = Mage::getSingleton('core/layout'); | |
$block = $layout->createBlock('producttabs/grid'); | |
echo '{"listProducts":' . json_encode($block->toHtml()) .'}'; | |
- $this->renderLayout(); | |
} | |
public function ajaxsliderAction() { | |
$layout = Mage::getSingleton('core/layout'); | |
$block = $layout->createBlock('producttabs/slider')->setTemplate('sns/producttabs/items-slider.phtml'); | |
echo '{"listProducts":' . json_encode($block->toHtml()) .'}'; | |
- $this->renderLayout(); | |
} | |
public function ajaxgridAction() { | |
$layout = Mage::getSingleton('core/layout'); | |
$block = $layout->createBlock('producttabs/grid'); | |
echo '{"listProducts":' . json_encode($block->toHtml()) .'}'; | |
- $this->renderLayout(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment