Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento:获取至少有一个类别的产品集合_Magento - Fatal编程技术网

Magento:获取至少有一个类别的产品集合

Magento:获取至少有一个类别的产品集合,magento,Magento,我想获得至少有一个类别的产品集合。实际上我想忽略那些不属于任何类别的产品。有人能帮忙吗??? 我的代码如下: protected function _prepareCollection() { $store = $this->_getStore(); $collection = Mage::getModel('catalog/product')->getCollection() ->addAttributeToSelect('sku')

我想获得至少有一个类别的产品集合。实际上我想忽略那些不属于任何类别的产品。有人能帮忙吗??? 我的代码如下:

protected function _prepareCollection()
{
    $store = $this->_getStore();
    $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('sku')
        ->addAttributeToSelect('name')
        ->addAttributeToSelect('attribute_set_id')
        ->addAttributeToSelect('type_id')
        ->joinField('qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id=entity_id',
            '{{table}}.stock_id=1',
            'left');

    if ($store->getId()) {
        //$collection->setStoreId($store->getId());
        $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
        $collection->addStoreFilter($store);
        $collection->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'inner', $adminStore);
        $collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId());
        $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner', $store->getId());
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
        $collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
    }
    else {
        $collection->addAttributeToSelect('price');
        $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
        $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
    }

    $this->setCollection($collection);

    parent::_prepareCollection();
    $this->getCollection()->addWebsiteNamesToResult();

    return $this;
}
谢谢,

使用以下代码片段:

<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    require_once("app/Mage.php");
    Mage::app();

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection 
        ->joinField('category_id',
            'catalog_category_product',
            'category_id',
            'product_id=entity_id',
            null,
            'right');
所以它将获取目录\类别\产品表中id的所有产品,换句话说,脚本将获取类别中的所有产品

将其他属性加载到集合:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
更新1

我试过用你的——效果很好

试试这个-

$collection = $this->_prepareCollection();
$collection 
    ->joinField('category_id',
        'catalog_category_product',
        'category_id',
        'product_id=entity_id',
        null,
        'right');
使用以下代码段:

<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    require_once("app/Mage.php");
    Mage::app();

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection 
        ->joinField('category_id',
            'catalog_category_product',
            'category_id',
            'product_id=entity_id',
            null,
            'right');
所以它将获取目录\类别\产品表中id的所有产品,换句话说,脚本将获取类别中的所有产品

将其他属性加载到集合:

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
更新1

我试过用你的——效果很好

试试这个-

$collection = $this->_prepareCollection();
$collection 
    ->joinField('category_id',
        'catalog_category_product',
        'category_id',
        'product_id=entity_id',
        null,
        'right');

杰克,非常感谢你的回复。我想在产品网格中添加此过滤器。但当我在网格集合代码上应用您的代码片段时,我得到了一个错误(重复ID错误):(请在加载收藏的第一条消息代码段中输入。我的代码加载所有具有任何类别的产品。请查看更新1。如果您告诉我“我使用了您的代码,但失败了”,请告诉我您到底使用了哪些代码以及如何使用。Jake,非常感谢您的回复。我想在产品网格中添加此过滤器。但是,当我在网格集合代码中应用您的代码片段时,我遇到了一个错误(重复ID错误)。:(请在加载收藏的第一条消息代码段中输入。我的代码加载所有具有任何类别的产品。请查看更新1。如果您告诉我“我使用了您的代码,但失败了”,请告诉我您到底使用了哪些代码以及如何使用。