簡體   English   中英

Magento按商店ID獲取產品集合過濾器

[英]Magento Get Product Collection Filter By Store Id

兩個商店都有不同的根類別。 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()); 
};

如果我打印商店ID,它會給我正確的號碼。 我在第二個商店只有一個產品,為什么我要從所有商店回收所有產品? 我可以將主商店中的每個產品設置為不在Store2中顯示,然后添加可見性過濾器,但這將需要永遠。

另外,我剛注意到,如果我回應產品商店ID,我會得到當前ID,而不是它分配給的商店:

echo $_testproduct->getStoreId()

如何解決這個問題?

試試這個你得到你想要的

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

您還可以嘗試將商店過濾器添加到資源模型,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM