2012-03-22 13 views
0

BElowは、単一の製品の関連製品を選択する機能です。私は、関連する製品がない場合、他のランダムな製品がアレイに追加されるような方法で動作させたいと思っていました。ランダムは同じカテゴリの他のプロダクトであり、同じカテゴリにプロカットがない場合は他のカテゴリから取り出すことができます。マゼンタの関連製品の代わりにランダムな製品

protected function _prepareData() 
    { 
     $product = Mage::registry('product'); 
     /* @var $product Mage_Catalog_Model_Product */ 

     $this->_itemCollection = $product->getRelatedProductCollection() 
      ->addAttributeToSelect('required_options') 
      ->addAttributeToSort('position', Varien_Db_Select::SQL_ASC) 
      ->addStoreFilter() 
     ; 

     if (Mage::helper('catalog')->isModuleEnabled('Mage_Checkout')) { 
      Mage::getResourceSingleton('checkout/cart')->addExcludeProductFilter($this->_itemCollection, 
       Mage::getSingleton('checkout/session')->getQuoteId() 
      ); 
      $this->_addProductAttributesAndPrices($this->_itemCollection); 
     } 
//  Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($this->_itemCollection); 
     Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($this->_itemCollection); 

     $this->_itemCollection->load(); 

     foreach ($this->_itemCollection as $product) { 
      $product->setDoNotUseCategoryId(true); 
     } 

     return $this; 
    } 

おかげ Abnab

答えて

1

あなたはすべての関連製品があるかどうかを確認するためにcount()を使用することができます。存在しない場合は、必要なフィルタを使用して新しい製品コレクションを読み込むことができます。一例として、私はcategory_idで下にフィルタリングしました。私はMagento collections(またはhere)を読むことをお勧めします。

protected function _prepareData() 
{ 
... 
    $this->_itemCollection->load(); 

    // If there are no related products, find more products in same category. 
    if (count($this->_itemCollection) < 1) { 
     $this->_itemCollection = Mage::getModel('catalog/product')->getCollection() 
      ->addAttributeToSelect('required_options') 
      ->addAttributeToFilter('category_id', $product->getCategoryId()); 
    } 

    foreach ($this->_itemCollection as $product) { 
... 
関連する問題