如何为所有产品提供类别名称?
这就是我如何获得给出类别ID的所有产品
$cat_id = 1; // category id
$_productCollection = $product->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_id', array('in' => array($cat_id)) )
->addAttributeToFilter('visibility', 4) // Only catalog, search visiblity
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
->setCurPage(1)
->setPageSize(12);
但你怎么得到类别名称?
答案 0 :(得分:2)
您可以使用以下代码
从名称中获取类别ID$cat = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', 'cat_name')->getData();
$cat_id = $cat[0]['entity_id'];
现在,在产品系列中使用$cat_id
来获取按类别ID
答案 1 :(得分:0)
您可以在加入字段category_name后按名称而不是ID进行过滤。通过category_name替换category_id应该可以解决问题。
/**
* Join regular table field and use an attribute as fk
*
* Examples:
* (‘country_name’, ‘directory/country_name’, ‘name’, ‘country_id=shipping_country’, “{{table}}.language_code=’en’”, ‘left’)
*
* @param string $alias ‘country_name’
* @param string $table ‘directory/country_name’
* @param string $field ‘name’
* @param string $bind ‘PK(country_id)=FK(shipping_country_id)’
* @param string|array $cond “{{table}}.language_code=’en’” OR array(‘language_code’=>’en’)
* @param string $joinType ‘left’
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
joinField($alias, $table, $field, $bind, $cond=null, $joinType=’inner’)
并按category_name过滤......
$cat_name = "catname"; // category id
$_productCollection = $product->getCollection()
->joinField('category_name', 'catalog/category_product', 'category_name', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('category_name', array('in' => array($cat_name)) )
->addAttributeToFilter('visibility', 4) // Only catalog, search visiblity
->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
->setCurPage(1)
->setPageSize(12);
答案 2 :(得分:0)
您想要类别名称的类别ID吗?所以,你可以进一步使用它。试试下面。 $ cat = Mage :: getResourceModel('catalog / category_collection') - > addFieldToFilter('name','Category_Name_Here'); $ CAT-> getFirstItem() - > getEntityId();