【问题标题】:How get all products by category name in Magento如何在 Magento 中按类别名称获取所有产品
【发布时间】:2014-08-01 22:22:08
【问题描述】:

如何获取给定类别名称的所有产品?

这就是我获取所有产品的方式

$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);

但是如何通过类别名称获得?

【问题讨论】:

    标签: magento categories product


    【解决方案1】:

    您可以使用以下代码从名称中获取类别 id

    $cat = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', 'cat_name')->getData();
    $cat_id = $cat[0]['entity_id'];
    

    现在,在产品集合中使用 $cat_id 来获取按类别 id 过滤的产品

    【讨论】:

    • 在我看到这个之前我已经解决了我的问题,但你的回答与我的解决方案基本相同。谢谢
    【解决方案2】:

    您想要类别名称中的类别 ID 对吗?所以,你可以进一步使用它。试试下面。 $cat = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name', 'Category_Name_Here'); $cat->getFirstItem()->getEntityId();

    【讨论】:

    • 我想要给所有产品一个类别名称
    • $products = Mage::getSingleton('catalog/category')->load($cat->getFirstItem()->getEntityId()) ->getProductCollection() ->addAttributeToSelect('* ');
    【解决方案3】:

    您可以在加入字段 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’) 
    

    并改为按类别名称过滤...

    $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);
    

    【讨论】:

    • 你能给我修改后的代码吗?我想确定你的意思。
    • 我进行了编辑以提示该做什么我不是 100% 确定要使用的名称
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    相关资源
    最近更新 更多