我在这里遇到了一些严重的 Magento 问题。正如预期的那样:
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => 1));
将返回我的 $category_id 的所有启用的产品。然而这个:
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', array('eq' => 0));
不退回有缺陷的产品。我似乎找不到退回禁用产品的方法,我也不知道为什么。
我试过这个:
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
这本来是可行的,但显然可能已被弃用。
有谁知道如何获取某个类别中的所有产品(启用和禁用)?
别担心,您只是被一个非常不寻常的常量定义所困^^。尝试一下:
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter(
'status',
array('eq' => Mage_Catalog_Model_Product_Status::STATUS_DISABLED)
);
无论出于何种原因,Varien 决定将这个
STATUS_DISABLED
常量定义为 2
,而不是更直观(且常用)的 0
值。
我认为你可以通过将商店设置为默认值来做到这一点
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
确保保存当前存储值并在执行上述操作后将其设置回来。
或者通过使用 magento 外部的脚本并通过
调用 magerequire_once '../app/Mage.php';
$app = Mage::app();
Mage::register('isSecureArea', true);
$products = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('status', 1)
->addAttributeToFilter('visibility', 4)
->setOrder('price', 'ASC');
我还没有找到上述问题的答案。但我通过使用不同的方法节省了很多时间。
我导出了 Magento 中所有产品的 CSV,删除了除类别 ID 和 SKU 之外的所有列(这是我所需要的),然后对其进行过滤以返回所有 sku。
如果它对任何人有帮助,那就是代码-
<?php
$file = fopen('allprods.csv', 'r');
// You can use an array to store your search ids, makes things more flexible.
// Supports any number of search ids.
$id = array($_GET['id']);
// Make the search ids safe to use in regex (escapes special characters)
$id = array_map('preg_quote', $id);
// The argument becomes '/id/i', which means 'id, case-insensitive'
$regex = '/'.implode('|', $id).'/i';
$skus = array();
while (($line = fgetcsv($file)) !== FALSE) {
list($ids, $sku) = $line;
if(preg_match($regex, $ids)) {
$skus[] = $sku;
}
}
$count = count($skus);
$i = 1;
echo $category_id;
foreach ($skus as $sku){
echo $sku;
if($i != $count) { echo "`"; }
$i++;
}
此解决方案是使用之前有关过滤 CSV 的主题创建的,此处
所以现在我还可以活下去。然而 - 这个问题仍然需要答案!
如果在后端配置->目录中打开
catalog_flat_product
,则没有任何效果。
试试这个..这将最终启用和禁用所有产品
$collection = Mage::getResourceModel('catalog/product_collection'); //this will give you all products
foreach($collection as $col)
{
$var = Mage::getModel('catalog/product')->loadByAttribute('sku',$col->getSku());
echo"<pre>";print_r($var->getData());echo"</pre>";
}
非常简单,之后您可以轻松地按状态过滤产品