我希望获得至少包含一个类别的产品系列。实际上我想忽略那些不属于任何类别的产品。谁能帮忙??? 我的代码如下:
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;
}
谢谢,
答案 0 :(得分:1)
使用此代码段:
<?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,如:
$product->getData('category_id');
因此,所有产品的id都在catalog_category_product表中,换句话说,脚本会将所有产品分类。
将其他属性加载到集合中:
$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');