質問

1.9.0.1:で続行したい

現在の製品と同じカテゴリから12のランダムなアップセル製品を取得する。 彼らは販売可能でなければなりません。 彼らはすでに関連製品に入ってはいけません。 彼らはカートに入ってはいけません。

そのカテゴリに12がない場合は、親カテゴリからの製品を残しています。

これは複雑になっていると思いますが、それをすることができます。私がそれをすることができるかどうかわからない: - )

私の質問は、その複雑さを選択したパフォーマンスについて心配する必要がありますか?あなたはどういう意味ですか?

コード例は私がそれらを比較できることが素晴らしいでしょう。

役に立ちましたか?

解決

最近、非常に似たものを実装し、以下の関連コードを共有します。しかし、最初にパフォーマンスについてのいくつかのコメントはそれが質問でした:

あなたの要求のためのパフォーマンスに関する一般的な考え:

他のヒント

これは私の作業コードです:

//get needed variables
$_current_prod = Mage::registry('current_product');
$_current_cat =  Mage::getModel('catalog/layer')->getCurrentCategory();
$_current_cat_id = $_current_cat->getId();
$_parent_cat_id =  $_current_cat->getParentId();
//did array because later on it may get more complex order with more ids or I want to sort it different
$_cat_order = array($_current_cat_id,$_parent_cat_id);
//getting related products to filter from collection
$_related_products = $_current_prod->getRelatedProductIds();

//getting cart items to filter from collection
$_session= Mage::getSingleton('checkout/session');
$_cart_products = array();
foreach($_session->getQuote()->getAllItems() as $_item)
{
   $_cart_products[] = $_item->getProductId();
}

//merge product arrays to filter from collection
$_inuse_products = array_unique(array_merge($_related_products, $_cart_products));      

//getting collection
$_productCollection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('nin' => $_inuse_products))
->addAttributeToFilter('entity_id', array('neq' => $_current_prod->getId()))
->addAttributeToFilter('category_id', array(
    array('finset' => $_current_cat_id),
    array('finset' => $_parent_cat_id))
);

//sort collection = deepest category level first and limit it to 12
$_productCollection->getSelect()->order(new Zend_Db_Expr('FIELD(category_id, ' . implode(',', $_cat_order).')'))->limit(12);
.

最適化する必要があると思いますか?:)

これをView.phtmlに入れることが機能します。CSSを調整する必要があるだけです。

<?php $categories = $_product->getCategoryIds(); ?> 
<?php $result = array(); 
foreach($categories as $cat_id) { 
$category = Mage::getModel('catalog/category'); 
$category->load($cat_id); 
$collection = $category->getProductCollection(); 
foreach ($collection as $product) { 
$result[] = $product->getId(); 
} 
} 
?> 
<div class="box-others-also-like" style=" margin:0 auto;"> 
<ul> 
<?php if(sizeof($result) >= 10) 
{ 
$ourneed = array_rand($result,10); 
foreach($ourneed as $cc) 
{ 
$thisproduct= Mage::getModel('catalog/product')->load($result[$cc]); 
?> 
<li style=" border:1px solid #ccc; float:left; width:140px; margin:10px 5px 0 0; overflow:hidden;"> 
<a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct,'small_image')->resize(140) ?>" width="140" height="140" alt="<?php echo $thisproduct->getName(); ?>" /></a> 
<a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" title="<?php echo $thisproduct->getName(); ?>" >
<h3 style=" font:12px/1.55 arial; width:140px; height:4.25em; overflow:hidden;"><?php echo $thisproduct->getName(); ?></h3></a> 
</li> 
<?php } ?> 
<?php }else { 
foreach($result as $cc) 
{ 
$thisproduct= Mage::getModel('catalog/product')->load($cc); 
?> 
<li> 
<a href="<?php echo $thisproduct->getProductUrl(); ?>" title="<?php echo $thisproduct->getName(); ?>" ><img src="<?php echo $this->helper('catalog/image')->init($thisproduct, 'small_image')->resize(200) ?>" width="200" height="200" alt="<?php echo $thisproduct->getName(); ?>" /></a> </li> 
<?php } 
} 
?> 
</ul> 
</div>
.

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