php Magento 按商店 ID 获取产品系列过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19650000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento Get Product Collection Filter By Store Id
提问by
Both stores have a different root category. Main Store is the default sample data, Second Store has just one product I was added. I would have thought that using the store filter, only products within the root category of the current store would show up. But I'm getting every product showing. I'm testing this by placing the following in my category view template:
两家商店都有不同的根类别。Main Store 是默认的示例数据,Second Store 只添加了一个我添加的产品。我原以为使用商店过滤器,只会显示当前商店的根类别内的产品。但我正在展示所有产品。我通过在我的类别视图模板中放置以下内容来测试这个:
$store_id = Mage::app()->getStore()->getId();
$_testproductCollection = Mage::getResourceModel('reports/product_collection')
->setStoreId($storeId)
->addStoreFilter($store_id)
->addAttributeToSelect('*');
$_testproductCollection->load();
foreach($_testproductCollection as $_testproduct){
echo $this->htmlEscape($_testproduct->getName());
};
If I print the store ID, it's giving me the correct number. I have only one product in second Store , so why am I getting every product from all stores returned? I can set every product in Main Store to not show in Store2 and then add a visibility filter, but that would take forever.
如果我打印商店 ID,它会给我正确的号码。我在 second Store 中只有一种产品,那么为什么我要退回所有商店的所有产品?我可以将 Main Store 中的每个产品设置为不在 Store2 中显示,然后添加一个可见性过滤器,但这需要永远。
Also, I just noticed, if I echo the products store ID, I get the current ID, not the store it's assigned to:
另外,我刚刚注意到,如果我回显产品商店 ID,我会得到当前 ID,而不是它分配给的商店:
echo $_testproduct->getStoreId()
How to solve this issue?
如何解决这个问题?
采纳答案by Patel Dixit
Try this You get as you want
试试这个你想要的
$counter = "";
/*$model=Mage::getModel('catalog/product')->setStoreId($post['stores']);
$rootCategoryId = Mage::app()->getStore($post['stores'])->getRootCategoryId();
$products = $model->getCollection();
$products->addStoreFilter($post['stores']);
$products->addAttributeToFilter('sku', array('nlike' => 'B%'));
$products->addAttributeToFilter('status',1);
$counter=$products->getData();*/
$model=Mage::getModel('catalog/product')->setStoreId($post['stores']);
$category_model = Mage::getModel('catalog/category');
$rootCategoryId = Mage::app()->getStore($post['stores'])->getRootCategoryId();
$_category = $category_model->load($rootCategoryId);
$all_child_categories = $category_model->getResource()->getAllChildren($_category);
foreach($all_child_categories as $storecategories):
$category = Mage::getModel('catalog/category')->load($storecategories);
$products = $category->getProductCollection();
//echo "Category id is::".$storecategories."Products are::".count($products);
//echo "<br/>";
foreach($products as $collection):
$removecatindex = $collection->getData();
unset($removecatindex['cat_index_position']);
$counter[] = $removecatindex;
endforeach;
endforeach;
回答by James
You can also try adding a store filter to the resource model like this:
您还可以尝试向资源模型添加商店过滤器,如下所示:
$collection = Mage::getResourceModel('catalog/product_collection')
->addStoreFilter($this->getStoreId())
->addAttributeToSelect('*');
回答by ashraf mohammed
$collection = Mage::getModel('catalog/product')->getCollection()->addStoreFilter($store_id)
->addAttributeToSelect('*') // select all attributes
->setPageSize(5000) // limit number of results returned
->setCurPage(1); // set the offset (useful for pagination)
// we iterate through the list of products to get attribute values
foreach ($collection as $product) {
echo $product->getName(); //get name
echo (float) $product->getPrice(); //get price as cast to float
echo $product->getDescription(); //get description
echo $product->getShortDescription(); //get short description
echo $product->getTypeId(); //get product type
echo $product->getStatus(); //get product status
// getCategoryIds(); returns an array of category IDs associated with the product
foreach ($product->getCategoryIds() as $category_id) {
$category = Mage::getModel('catalog/category')->load($category_id);
echo $category->getName();
echo $category->getParentCategory()->getName(); // get parent of category
}
//gets the image url of the product
echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).
'catalog/product'.$product->getImage();
echo $product->getSpecialPrice();
echo $product->getProductUrl(); //gets the product url
echo '<br />';
}