按类别列出Magento自定义产品
-
29-09-2020 - |
题
我正在使用Marlople个性化产品模块,该模块创建一个名为“Milople个性化产品”的自定义产品类型,我需要在类别ID下显示此产品的列表。我用这个代码试验:
$cat_id = 3;
$category = Mage::getModel('catalog/category')->load($cat_id);
$collection = $category->getProductCollection()->addAttributeToSort('position');
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
.
并且它工作形成所有默认的产品类型,除了个性化的外部。
解决方案
如果它是一种新产品类型,请尝试在集合中过滤它:
$collection = $category->getProductCollection()->addAttributeToSort('position')
->addAttributeToFilter('type_id', array('eq' => 'Milople Personalized Product'));
.
希望这就是你想要的。
编辑:
试试:
$category = new Mage_Catalog_Model_Category();
$category->load(3); //this is category id
$collection =
$category->getProductCollection()->addAttributeToFilter('type_id',
array('eq' => 'Milople Personalized Product'));
. 其他提示
以下代码是通过类别将所有产品导出到CSV中。
<?php
set_time_limit(0);
ini_set("memory_limit",-1);
ini_set('max_execution_time','1800000000');
require_once '../app/Mage.php';
Mage::app();
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$fp = fopen('category-product-export.csv', 'w');
$field = array('Product SKU','Category Name');
fputcsv($fp, $field);
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->load();
foreach ($_productCollection as $_product){
$cats = $_product->getCategoryIds();
$cnt = 0;
$catName = '';
foreach($cats as $id) {
$category->load($id);
$root = 'Root Catalog';
$isRoot = strtolower($root);
$categoryName = strtolower($category->getName());
if($categoryName == $isRoot){
continue;
}
$categories[$id]['name'] = $category->getName();
$categories[$id]['path'] = $category->getPath();
$path = explode('/', $categories[$id]['path']);
$len = count($path);
$string = '';
if($id > 2){
foreach ($path as $k=>$pathId)
{
$separator = '';
if($pathId > 2){
$category->load($pathId);
if($k != $len-1){ $separator = ' || ';}
$string.= $category->getName() . $separator;
}
}
if($cnt > 0) {
$catName.= ','.$string;
} else {
$catName = $string;
}
$cnt++;
}
}
//echo $catName;
$field = array($_product->getSku(),$catName);
fputcsv($fp, $field);
}
?>
<a href="category-product-export.csv">Download</a>
.