MAGENTO设置首页产品显示 & 显示隐藏商品

<?php



header( 'Content-Type:   text/html;   charset=utf-8 ');
@set_time_limit(10000);
@ini_set('memory_limit','100M');
require_once 'app/Mage.php';
// 设置密码
$PASSWORD = '10000000';
// 初始化 网站结构

Mage :: app() -> setCurrentStore(Mage_Core_Model_App :: ADMIN_STORE_ID);

$collection = Mage :: getModel('catalog/category') -> getCollection() -> addAttributeToSelect('name') -> addAttributeToSelect('is_active')->addAttributeToFilter('level',1); // 获取所有分类 ID
foreach($collection as $col) {
	$topcate[$col -> getId()] = $col -> getName();
} 

function getSubcatArr($catid){
	$subids = Mage::getModel('catalog/category')->load($catid)->getChildren();
	if(empty($subids))
		return false;
	$subCategories = explode(',', $subids);
		// 获取所有分类 ID
		foreach($subCategories as $subCategoryId) {
			$cat = Mage::getModel('catalog/category')->load($subCategoryId);
			$subcates[$cat -> getId()] = $cat -> getName();
		} 
		return $subcates;
	
}
//SEO 结尾标示符号
$endTag = Mage::getStoreConfig('catalog/seo/category_url_suffix');
ini_set("memory_limit","-1");
ini_set("output_buffering", 1);
ini_set("implicit_flush", 1);
ob_end_clean();
ob_start();

$websites = Mage::app()->getWebsites();
	foreach($websites as $website)
		$sites[$website->name]=$website->website_id;
$root_cat=str_replace('www.','',$_SERVER['HTTP_HOST']);
$storeId=$sites[$root_cat];
if(empty($storeId))
	$storeId=1;// 默认商店  单网站系统
$model = Mage::getModel('catalog/product')->setStoreId($storeId); //getting product model
$unable_collection = $model->getCollection()
->addAttributeToFilter('status', array('eq' => '-1'))
->addStoreFilter($storeId);

$unable=count($unable_collection);



// 批量默认结束


if(!empty($_GET['unable_num'])){
	echo $unable;
	exit;
}

$collection = $model->getCollection()
->addAttributeToFilter('status', array('eq' => 1))
->addStoreFilter($storeId);

$enable=count($collection);


echo '显示:'.($enable).'/'.'全部'.($totalNum=$enable+$unable).'(隐藏'.$unable.')'.'<br>';

// 批量编辑产品
if(!empty($_GET['alledit']) and $_GET['pw']==$PASSWORD){
echo '<h1>'.$_SERVER['HTTP_HOST'].'</h1><hr>';


if(empty($_POST['alledit_submit'])){
?>
<h2>注意: 每次设置均全部初始化, 更新一遍.</h2>
<form method="post" action="?alledit=ok&pw=<?php echo $PASSWORD;?>">
	
	隐藏百分比产品<input type="text" id="" name='hiddenpecnet' value=10 />  
	随机隐藏<input type="checkbox" id="" name='random' checked/>(说明: 不勾选随机隐藏 为顺序隐藏)<br>
	
	<input type="submit" name='alledit_submit'/>



</form>


<?php
}
if(empty($_POST['alledit_submit']))
	exit('');
if(empty($_POST['hiddenpecnet'])  )
	exit('');



if(!empty($_POST['hiddenpecnet'])){
	$needhiddennum=intval($totalNum * $_POST['hiddenpecnet'] / 100);
	$beginNum = $totalNum - $needhiddennum;
	echo '需要隐藏'.$needhiddennum.'个产品';
}else{
	$needhiddennum=0;
	$beginNum = $totalNum - $needhiddennum;
	echo '需要隐藏'.$needhiddennum.'个产品';
}



$model = Mage::getModel('catalog/product'); //getting product model
$collection = $model->getCollection()
->addStoreFilter($storeId);
$total=count($collection);
$beginNumTT = $total - $needhiddennum;
for($i=0;$i<$total;$i++){
	if($i>=$beginNumTT)
		$arr_value[$i]=true;
	else
		$arr_value[$i]=false;


}
if($_POST['random']=='on'){
	echo '!!! 随机隐藏!!!!';
	shuffle($arr_value);
	

}
$num=0;
	foreach ($collection as $k=>$product) //loop for getting products
	{                  
		$model = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getID());
		$num++;
		ob_flush();
		flush();
		echo '<br>第'.$num.'个 Name:'.$model->getName().'<br>'.$model->getData("url_key").$endTag;

		//setDescription
		//setShortDescription

		//$product->setData("name", str_replace(' test','',$model->getName()));
		//$product->save();
		

		
		Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
		//$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
		$model->setStoreId($storeId);
		// 标题处理
		
		if($_POST['dont_hide']!='on'){
		if($arr_value[$num]){
			$model->setData("status", "-1");
			echo '<font color=gray> 隐藏</font> ';
		}
		else
			echo '<font color=blue> 显示</font> ';
		}else
			echo '<font color=blue> 忽略隐藏操作</font> ';
		if($model->save())
				echo '<font color=green> ok</font><br>';
		else
				echo '<font color=red> false</font><br>';

	}
echo "<br>批量隐藏结束";
exit;
}
// 批量替换结束





// 显示隐藏商品  $unable_collection
if(!empty($_GET['diplaynum']) and $_GET['pw']==$PASSWORD ){


			shuffle($unable_collection);
		
			foreach($unable_collection as $product){
				$numcount++;
					ob_flush();
					flush();
				

			

				
				
				Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
					//$product = Mage::getModel('catalog/product')->load(PRODUCT_ID);
				$product->setStoreId($storeId);
				$product->setData("status", "1");

				if($product -> save()){
					$model = Mage::getModel('catalog/product')->load($product->getID());
					//$model = Mage::getModel('catalog/product')->setStoreId($storeId)->load($product->getID());
					echo '<br>第'.$numcount.'个 Name:'.$model->getName().'<br>'.$model->getData("url_key").$endTag.'  <font color=blue>显示成功</font>';
				}

				if($numcount>=$_GET['diplaynum'])
					exit;
			}
exit;
}
// 结束 设置首页产品显示  & 显示隐藏商品  $unable_collection





?>

  

posted @ 2012-09-22 20:31  shineme  阅读(474)  评论(0编辑  收藏  举报