php - Magento Display out of stock products not working -
i wasted more 5 hours trying figure out why can't see out of stock products in home page of magento. i'm using sm tablisting and, before ask, yes, supports out of stock products.
i know story. configuration > inventory > display out of stock products > yes > save & re-index. bad still can't see out of stock products in home page.
i debugged entire list.php file of magento , seems problem in core of magento. let me explain. i'm using inventory functionalities , when product not in stock (quantity = 0) magento puts "availability" equal "not available". obvious reasons can't put "available" when quanitity 0 , array of products in home page comes "available" products. what's solution? don't point of setting display out of stock products > yes.
i update message code website using show products in home page:
class sm_tablisting_block_list extends mage_catalog_block_product_abstract { protected $_config = null; protected $products_viewed = null; public function __construct($attributes = array()){ parent::__construct(); $this->_config = mage::helper('tablisting/data')->get($attributes); } public function getconfig($name=null, $value=null){ if (is_null($this->_config)){ $this->_config = mage::helper('tablisting/data')->get(null); } if (!is_null($name) && !empty($name)){ $valueret = isset($this->_config[$name]) ? $this->_config[$name] : $value; return $valueret; } return $this->_config; } public function setconfig($name=null, $value=null){ if (is_null($this->_config)) $this->getconfig(); if (is_array($name)){ mage::log($name); $this->_config = array_merge($this->_config, $name); return; } if (!empty($name)){ $this->_config[$name] = $value; } return true; } public function getconfigobject(){ return (object)$this->getconfig(); } public function generatehash(){ $config = $this->getconfig(); $this->hash = md5( serialize($config) ); return $this->hash; } public function _beforehtml(){ $this->generatehash(); } protected function _tohtml(){ if(!$this->getconfig('isenabled')) return; $is_ajax = mage::app()->getrequest()->getparam('is_ajax'); if( $is_ajax ){ $cat_id = mage::app()->getrequest()->getparam('tab_cat_id'); $order_id = mage::app()->getrequest()->getparam('order_id'); $type = mage::app()->getrequest()->getparam('data_type'); if( $type == 'order' ){ $child_items = $this->getproductsorder($order_id); } else { $child_items = $this->getproducts($cat_id); } $template_file = 'sm/tablisting/default_items.phtml'; }else{ $template_file = 'sm/tablisting/default.phtml'; } $this->settemplate($template_file); return parent::_tohtml(); } public function getstoreid(){ if (is_null($this->_storeid)){ $this->_storeid = mage::app()->getstore()->getid(); } return $this->_storeid; } public function setstoreid($storeid=null){ $this->_storeid = $storeid; } protected function getproductcollection(){ $collection = mage::getsingleton('catalog/product')->getcollection(); $collection->addattributetoselect('*'); $collection->addattributetofilter('status', mage_catalog_model_product_status::status_enabled); $visibility = array( mage_catalog_model_product_visibility::visibility_both, mage_catalog_model_product_visibility::visibility_in_catalog ); $collection->addattributetofilter('visibility', $visibility); // add price data $collection->addpricedata(); $this->_addviewscount($collection); $this->_addreviewscount($collection); $this->_addorderedcount($collection); return $collection; } public function setproductcollection($collection=null){ $this->_productcollection = $collection; } public function gettotal(){ if ($this->getconfig('product_category')==''){ return; } $storeid = mage::app()->getstore()->getid(); $category_collection = mage::getmodel('catalog/category')->getcollection(); $category_collection->setstoreid($storeid); $category_collection->addisactivefilter(); $category_collection->addattributetoselect('*'); $category_collection->addidfilter( $this->getconfig('product_category') ); $list = array(); $items = array(); $result = array(); foreach ($category_collection $category) { $category_obj = new stdclass(); $category_obj->id = $category->getid(); // category products $product_collection = $category->getproductcollection(); $product_collection->addattributetoselect('*'); $product_collection->addstorefilter($storeid); // select active & visible in catalog products mage::getsingleton('catalog/product_status')->addvisiblefiltertocollection($product_collection); mage::getsingleton('catalog/product_visibility')->addvisibleincatalogfiltertocollection($product_collection); foreach ($product_collection $product){ $product_obj = new stdclass(); $product_obj->id = $product->getid(); $stocklevel = (int)mage::getmodel('cataloginventory/stock_item')->loadbyproduct($product)->getqty(); $stock = $product->getstockitem(); $status = $stock->getisinstock(); if( $stocklevel > 0 && $status == '1' ){ $items[$product_obj->id] = $product_obj; } } $result = $items; } return $result; } public function getcategory(){ $list = array(); $params = mage::app()->getrequest()->getparams(); $is_ajax = $params['is_ajax']; $cat_id = $params['tab_cat_id']; $orderid = $this->_config['product_order_by']; $total = count($this->gettotal()); //var_dump($total); $all = new stdclass(); $all->id = '*'; $all->count = $total; $all->title = 'all product'; $all->sel = 'sel'; $all->orderid = $orderid; $all->child = $this->getproducts(); if ( $is_ajax ){ $all->child = $this->getproducts('*'); } array_unshift($list, $all); if ( $this->getconfig('product_category')=='' ){ return array(); } $storeid = mage::app()->getstore()->getid(); $category_collection = mage::getmodel('catalog/category')->getcollection(); $category_collection->setstoreid($storeid); $category_collection->addisactivefilter(); $category_collection->addattributetoselect('*'); $category_collection->addidfilter( $this->getconfig('product_category') ); foreach ($category_collection $category) { $items = array(); $category_obj = new stdclass(); $category_obj->id = $category->getid(); $category_obj->title = $category->getname(); $category_obj->link = $category->geturl(); $product_collection = $category->getproductcollection(); foreach( $product_collection $product ){ $product_obj = new stdclass(); $product_obj->id = $product->getid(); $stocklevel = (int)mage::getmodel('cataloginventory/stock_item')->loadbyproduct($product)->getqty(); $stock = $product->getstockitem(); $status = $stock->getisinstock(); if( $stocklevel > 0 && $status == '1' ){ $items[$product_obj->id] = $product_obj; } } $category_obj->count = count($items); $category_obj->orderid = $orderid; if ( $is_ajax ){ $category_obj->child = $this->getproducts($cat_id); } $list[]= $category_obj; } return $list; } public function getproducts($cat_id ){ $collection = $this->getproductcollection(); $params = mage::app()->getrequest()->getparams(); $is_ajax = $params['is_ajax']; $cat_id = $params['tab_cat_id']; $cat_config = $params['config_categoryid']; $items = array(); $this->addreviewsummarytemplate('sm', 'sm/tablisting/summary.phtml'); $items = array(); if ( mage::registry('current_category') ){ //is category view page. $current_category = mage::registry('current_category'); $current_category_id = $current_category->getid(); $product_ids = $current_category->getproductcollection()->getallids(); $collection->addidfilter($product_ids); $category_ids = array(); } else { if( $is_ajax ){ if( $cat_id != '*' ){ $category_ids = preg_split("/[,\s\d]+/", $cat_id); } else { $category_ids = preg_split("/[,\s\d]+/", $cat_config); } } else { $category_ids = preg_split("/[,\s\d]+/", $this->_config['product_category']); } if (is_array($category_ids)){ foreach ($category_ids $i => $id) { if (!is_numeric($id)){ unset($category_ids[$i]); } } } } if (isset($category_ids) && count($category_ids)>0) $this->_addcategoryfilter($collection, $category_ids); // sort products in collection $dir = strtolower( $this->_config['product_order_dir'] ); if (!in_array($dir, array('asc', 'desc'))){ $dir = 'asc'; } $attribute_to_sort = $this->_config['product_order_by']; switch ($attribute_to_sort){ case 'name': case 'created_at': case 'price': $collection->addattributetosort($attribute_to_sort, $dir); break; case 'position': break; case 'random': $collection->getselect()->order(new zend_db_expr('rand()')); break; case 'top_rating': $collection->getselect()->order('sm_rating_summary desc'); break; case 'most_reviewed': $collection->getselect()->order('sm_reviews_count desc'); break; case 'most_viewed': $collection->getselect()->order('sm_views_count desc'); break; case 'best_sales': $collection->getselect()->order('sm_ordered_count desc'); break; } if( $is_ajax ){ $product_limitation = intval(mage::app()->getrequest()->getparam('ajax_tablisting_start')); $collection->getselect()->limit( $this->_config['product_limitation'], $product_limitation ); } else { $product_limitation = intval($this->_config['product_limitation']); if ( $product_limitation > 0 ){ $collection->setpagesize($product_limitation); } } $maxtitle = $this->getconfig('item_title_max_characs',-1); foreach( $collection $k => $product ) { $product_obj = new stdclass(); $product_obj->id = $product->getid(); if ( $maxtitle > 0 ){ $product_obj->title = mage::helper('tablisting/data')->truncate($product->getname(), $maxtitle, ''); } else { $product_obj->title = $product->getname(); } $description = $product->getshortdescription(); if ( (int)$this->getconfig('item_description_striptags') == 1 ){ $keep_tags = $this->getconfig('item_description_keeptags', ''); $keep_tags = str_replace(array(' '), array(''), $keep_tags); $tmp_desc = strip_tags($description ,$keep_tags ); $product_obj->description = $tmp_desc; } else { $product_obj->description = $description; } if (($maxchars=$this->getconfig('item_desc_max_characs',-1))>0){ $product_obj->description = mage::helper('tablisting/data')->truncate($product_obj->description, $maxchars, ''); } $product_obj->image = (string)mage::helper('catalog/image')->init($product, 'image')->resize($this->getconfig('item_image_width'), $this->getconfig('item_image_height')); $product_obj->link = $product->getproducturl(); $product_obj->price_html = $this->getpricehtml($product, true); $product_obj->review_html = $this->getreviewssummaryhtml($product, 'sm', true); $product_obj->orderid = $attribute_to_sort; $stocklevel = (int)mage::getmodel('cataloginventory/stock_item')->loadbyproduct($product)->getqty(); $stock = $product->getstockitem(); $status = $stock->getisinstock(); if( $stocklevel > 0 && $status == '1' ){ $items[$product_obj->id] = $product_obj; } //$items[$product_obj->id] = $product_obj; } return $items; } public function getcategoryorder(){ $total = count($this->gettotal()); $order_by = preg_split("/\,/", $this->_config['order_tab']); $items = array(); while( count($order_by) ){ $item = array(); $item = trim(array_shift($order_by)); array_push($items, $item); } $order_attr = mage::getmodel('tablisting/system_config_source_orderby'); $order_label = $order_attr->tooptionarray(true); $list = array(); $params = mage::app()->getrequest()->getparams(); if( $params ){ $is_ajax = $params['is_ajax']; $cat_id = $params['tab_cat_id']; $order_id = $params['order_id']; }else{ $is_ajax = ""; $cat_id = ""; $order_id = ""; } $total = count($this->gettotal()); if ( $this->getconfig('product_category')=='' ){ return array(); } $i = 0; foreach ( $items $category ) { $i++; $category_obj = new stdclass(); foreach ($order_label $titel){ if ( $category == $titel['value'] ){ $category_obj->title = $titel['label']; } } $category_obj->id = $category; $category_obj->orderid = $category; $category_obj->count = $total; if ( $is_ajax ){ $category_obj->child = $this->getproductsorder($order_id); } if( $i== 1 ){ $category_obj->child = $this->getproductsorder($order_id); $category_obj->id = $items['0']; $category_obj->sel = 'sel'; $category_obj->orderid = $items['0']; } $list[]= $category_obj; } return $list; } public function getproductsorder($order_id){ $collection = $this->getproductcollection(); $params = mage::app()->getrequest()->getparams(); if( $params ){ $is_ajax = $params['is_ajax']; $cat_id = $params['tab_cat_id']; $order_id = $params['order_id']; $cat_config = $params['config_categoryid']; }else{ $is_ajax = ""; $cat_id = ""; $order_id = ""; $cat_config = ""; } $order_by = preg_split("/\,/", $this->_config['order_tab']); $cat = array(); while( count($order_by) ){ $item = array(); $item = trim(array_shift($order_by)); array_push($cat, $item); } $items = array(); $this->addreviewsummarytemplate('sm', 'sm/tablisting/summary.phtml'); $items = array(); if ( mage::registry('current_category') ){ //is category view page. $current_category = mage::registry('current_category'); $current_category_id = $current_category->getid(); $product_ids = $current_category->getproductcollection()->getallids(); $collection->addidfilter($product_ids); $category_ids = array(); } else { if( $is_ajax ){ $category_ids = preg_split("/[,\s\d]+/", $cat_config); } else { $category_ids = preg_split("/[,\s\d]+/", $this->_config['product_category']); } if (is_array($category_ids)){ foreach ($category_ids $i => $id) { if (!is_numeric($id)){ unset($category_ids[$i]); } } } } if (isset($category_ids) && count($category_ids)>0) $this->_addcategoryfilter($collection, $category_ids); // sort products in collection $dir = strtolower( $this->_config['product_order_dir'] ); if (!in_array($dir, array('asc', 'desc'))){ $dir = 'asc'; } if( $is_ajax ){ $attribute_to_sort = $order_id; switch ($attribute_to_sort){ case 'name': case 'created_at': case 'price': $collection->addattributetosort($attribute_to_sort, $dir); break; case 'position': break; case 'random': $collection->getselect()->order(new zend_db_expr('rand()')); break; case 'top_rating': $collection->getselect()->order('sm_rating_summary desc'); break; case 'most_reviewed': $collection->getselect()->order('sm_reviews_count desc'); break; case 'most_viewed': $collection->getselect()->order('sm_views_count desc'); break; case 'best_sales': $collection->getselect()->order('sm_ordered_count desc'); break; } } else { $attribute_to_sort = $cat['0']; switch ($attribute_to_sort){ case 'name': case 'created_at': case 'price': $collection->addattributetosort($attribute_to_sort, $dir); break; case 'position': break; case 'random': $collection->getselect()->order(new zend_db_expr('rand()')); break; case 'top_rating': $collection->getselect()->order('sm_rating_summary desc'); break; case 'most_reviewed': $collection->getselect()->order('sm_reviews_count desc'); break; case 'most_viewed': $collection->getselect()->order('sm_views_count desc'); break; case 'best_sales': $collection->getselect()->order('sm_ordered_count desc'); break; } } if( $is_ajax ){ $product_limitation = intval(mage::app()->getrequest()->getparam('ajax_tablisting_start')); $collection->getselect()->limit( $this->_config['product_limitation'], $product_limitation ); } else { $product_limitation = intval($this->_config['product_limitation']); if ( $product_limitation > 0 ){ $collection->setpagesize($product_limitation); } } $maxtitle = $this->getconfig('item_title_max_characs',-1); foreach( $collection $k => $product ) { $product_obj = new stdclass(); $product_obj->id = $product->getid(); if ( $maxtitle > 0 ){ $product_obj->title = mage::helper('tablisting/data')->truncate($product->getname(), $maxtitle, ''); } else { $product_obj->title = $product->getname(); } $description = $product->getshortdescription(); if ( (int)$this->getconfig('item_description_striptags') == 1 ){ $keep_tags = $this->getconfig('item_description_keeptags', ''); $keep_tags = str_replace(array(' '), array(''), $keep_tags); $tmp_desc = strip_tags($description ,$keep_tags ); $product_obj->description = $tmp_desc; } else { $product_obj->description = $description; } if (($maxchars=$this->getconfig('item_desc_max_characs',-1))>0){ $product_obj->description = mage::helper('tablisting/data')->truncate($product_obj->description, $maxchars, ''); } $product_obj->image = (string)mage::helper('catalog/image')->init($product, 'image')->resize($this->getconfig('item_image_width'), $this->getconfig('item_image_height')); $product_obj->link = $product->getproducturl(); $product_obj->price_html = $this->getpricehtml($product, true); $product_obj->review_html = $this->getreviewssummaryhtml($product, 'sm', true); $stocklevel = (int)mage::getmodel('cataloginventory/stock_item')->loadbyproduct($product)->getqty(); $stock = $product->getstockitem(); $status = $stock->getisinstock(); if( $stocklevel > 0 && $status == '1' ){ $items[$product_obj->id] = $product_obj; } //$items[$product_obj->id] = $product_obj; } return $items; } public function getscripttags(){ $import_str = ""; $jshelper = mage::helper('core/js'); if (null == mage::registry('jsmart.jquery')){ // jquery has not added yet if (mage::getstoreconfigflag('tablisting_cfg/advanced/include_jquery')){ // if module allowed jquery. $import_str .= $jshelper->includeskinscript('sm/tablisting/js/jquery-1.8.2.min.js'); mage::register('jsmart.jquery', 1); } } if (null == mage::registry('jsmart.jquerynoconfict')){ // add once noconflict $import_str .= $jshelper->includeskinscript('sm/tablisting/js/jquery-noconflict.js'); mage::register('jsmart.jquerynoconfict', 1); } if (null == mage::registry('jsmart.tablisting.js')){ // add script module. //$import_str .= $jshelper->includeskinscript('sm/tablisting/js/jquery.isotope.js'); //mage::register('jsmart.tablisting.js', 1); } return $import_str; } private function _addcategoryfilter(& $collection, $category_ids){ $category_collection = mage::getmodel('catalog/category')->getcollection(); $category_collection->addattributetoselect('*'); $category_collection->addisactivefilter(); if (count($category_ids)>0){ $category_collection->addidfilter($category_ids); } if (!mage::helper('catalog/category_flat')->isenabled()) { $category_collection->groupbyattribute('entity_id'); } //$category_collection->groupbyattribute('entity_id'); $category_products = array(); foreach ($category_collection $category){ $cid = $category->getid(); if (!array_key_exists( $cid, $category_products)){ $category_products[$cid] = $category->getproductcollection()->getallids(); //mage::log("id: " . $cid ); //mage::log("collection->count(): " . count($category_products[$cid]) ); } } $product_ids = array(); if (count($category_products)){ foreach ($category_products $cp) { $product_ids = array_merge($product_ids, $cp); } } //mage::log("merged_count: " . count($product_ids)); $collection->addidfilter($product_ids); } /*private function _addcategoryfilter(& $collection, $category_ids){ $category_collection = mage::getmodel('catalog/category')->getcollection(); $category_collection->addattributetoselect('*'); $category_collection->addisactivefilter(); $product_ids = array(); if (count($category_ids)>0){ $category_collection->addidfilter($category_ids); } if (!mage::helper('catalog/category_flat')->isenabled()) { $category_collection->groupbyattribute('entity_id'); } $category_products = array(); foreach ($category_collection $category){ $cid = $category->getid(); if (!array_key_exists( $cid, $category_products)){ $category_products[$cid] = $category->getproductcollection()->getallids(); //mage::log("id: " . $cid ); //mage::log("collection->count(): " . count($category_products[$cid]) ); } } }*/ private function _addviewscount(& $collection, $views_count_alias="sm_views_count"){ // add views_count $reports_event_table = mage::getsingleton('core/resource')->gettablename('reports/event'); $reports_event_types_table = mage::getsingleton('core/resource')->gettablename('reports/event_type'); $collection->getselect() ->joinleft( array("re_table" => $reports_event_table), "e.entity_id = re_table.object_id", array( $views_count_alias => "count(re_table.event_id)" ) )->joinleft( array("ret_table" => $reports_event_types_table), "re_table.event_type_id = ret_table.event_type_id , ret_table.event_name = 'catalog_product_view'", array() )->group('e.entity_id'); } private function _addreviewscount(& $collection, $reviews_count_alias="sm_reviews_count", $rating_summary_alias="sm_rating_summary" ){ // add reviews_count , rating_summary $review_summary_table = mage::getsingleton('core/resource')->gettablename('review/review_aggregate'); $collection->getselect()->joinleft( array("rs_table" => $review_summary_table), "e.entity_id = rs_table.entity_pk_value , rs_table.store_id=" . $this->getstoreid(), array( $reviews_count_alias => "rs_table.reviews_count", $rating_summary_alias => "rs_table.rating_summary" ) ); } private function _addorderedcount(& $collection, $ordered_qty_alias="sm_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 . "'"); $order_item_table = mage::getsingleton('core/resource')->gettablename('sales/order_item'); $collection->getselect()->joinleft( array("oi_table" => $order_item_table), "e.entity_id=oi_table.item_id , oi_table.order_id in ($orders_active_query)", array( $ordered_qty_alias => "sum(oi_table.qty_ordered)" ) ); }
}