質問

私のウェブサイトには、いくつかの店があります。 1つの特定のストアには、シンプルな製品と構成可能な製品の両方が含まれています。これは製品の膨大なコレクションです。すべての有効な製品を取得するための最速の方法は何ですか (including child products of configurable products) ストアIDによるこのストアのコレクション?

注:このさまざまな方法を試しました。しかし、それには多くの時間がかかります。時々、サーバーをクラッシュさせます。

注:私は使用しています Magento CE 1.3

どんな提案にも感謝します。

役に立ちましたか?

解決

$category = Mage::getModel('catalog/category')->load($categoryId);    
$collection = Mage::getModel('catalog/product')->getCollection()
        ->addCategoryFilter($category);
//you can use the next lines to add some minimal information about the products to the collection
//thanks @DavidManners
$collection->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) //add list attributes
            ->addMinimalPrice() //add prices
            ->addFinalPrice()
            ->addTaxPercents()
//or
//$collection->addAttributeToSelect('*');//if you want all the attributes
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);//only enabled products
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);//only visible products

今問題。通常、構成可能な製品に関連付けられた単純な製品は、カテゴリに関連付けられていないか、表示されないように設定されています。これが当てはまらない場合は、上記のコードで十分です。それらがカテゴリに関連付けられているが、それらが見えない場合は、最後の行にコメントするだけです。
それ以外の場合は、製品をループする必要があり、構成可能なものを見つけたら、関連する製品を取得します。

//this is needed if you want to get the out of stock simple products also
Mage::helper('catalog/product')->setSkipSaleableCheck(true);
$allProducts = array();
foreach ($collection as $product){
    $allProducts[] = $product;
    if ($product->getTypeId() == 'configurable'){
        $simpleProducts = $product->getTypeInstance(true)
                ->getUsedProducts(null, $product);
        foreach ($simpleProducts as $simple){
            $allProducts[] = $simple;
        }
    }
}

今、あなたはあなたが必要とするすべての製品を持っている必要があります $allProducts 配列。
これは、単純な関連製品を入手するために高速ではないかもしれませんが、必要なものを入手する必要があります。サーバーがクラッシュしないことを願っています。

他のヒント

@Mariusの答えを追加するには、使用済みのリソースを制限するには、Resource_iteratorクラスを使用できます。

Mage::getSingleton('core/resource_iterator')->walk($collection->getSelect(), array('productCallback'), array());

function productCallback($args)
{
  var_dump($args['row']);
  [...]
}

クエリを実行してすべての結果を一度に取得する代わりに、Magentoはその時点で1つの結果を処理し、次の結果に移行します。

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top