2013-10-29 11 views
7

両方の店舗に異なるルートカテゴリがあります。メインストアはデフォルトのサンプルデータです。セカンドストアは追加された製品が1つだけです。私は、ストアフィルタを使用すると、現在の店舗のルートカテゴリ内の商品のみが表示されると考えていたでしょう。しかし、私はすべての製品を表示しています。私は、カテゴリビューテンプレートに次のように配置することで、これをテストしている:Magento店舗IDで商品コレクションを取得

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

私は店のIDを印刷する場合、それは私に正しい番号を与えています。私は2つ目の店で1つの製品しか持っていないので、なぜすべての店舗のすべての製品が返されていますか?メインストア内のすべての製品をStore2に表示しないように設定し、表示フィルタを追加することはできますが、これは永遠にかかるでしょう。この問題を解決するためにどのように

echo $_testproduct->getStoreId() 

また、私はちょうど気づいた、私は製品ストアのIDをエコーた場合、私は現在のIDを取得し、店はに割り当てられていませんか?

答えて

5

あなたは

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

をしたいとあなたはまた、このようなリソースのモデルにストアフィルタを追加してみてくださいすることができますあなたが取得し、これを試してみてください:

$collection = Mage::getResourceModel('catalog/product_collection') 
    ->addStoreFilter($this->getStoreId()) 
    ->addAttributeToSelect('*'); 
2
$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 />'; 
}