2010-11-01 86 views
0

嗨 我有一个类别XXX下的类别我有一个子类别yyy。 该产品被分配到yyy类别。此外我已交叉销售5件产品。 当我点击yyy时,我需要获得所有交叉销售的产品名称,这些产品名称在左侧分配为交叉销售。magento交叉销售子类别

分类

XXXXXXXX
YYYYYYY
zzzzzzz

交叉销售产品

Crosssell1 Crosssell2 Crosssell3

我怎么能走呢?

回答

2

您将需要创建一个将出现在左列中的块。我们来创建一个函数来获取产品ID的初始列表。如果你的块扩展Mage_Catalog_Block_Product_List那么它可能会喜欢这样的:

public function getCurrentProductIds() 
{ 
    return $this->getLoadedProductCollection()->getAllIds(); 
} 

但是,这仅适用于当前页面上显示的产品工作。如果你需要显示在该类别后页面中的所有相关产品,那么该功能可能是这样的:

public function getCurrentProductIds() 
{ 
    return Mage::registry('current_category')->getProductCollection()->getAllIds(); 
} 

两个函数会返回一个整数列表。

接下来是必不可少的部分。获取相关产品。

public function getCrossSellsCollection() 
{ 
    $productIds = $this->getCurrentProductIds(); 
    $link = Mage::getSingleton('catalog/product_link') 
     ->useCrossSellLinks();   // very important, this sets the linkTypeId to 5 
    $collection = $link->getProductCollection() 
     ->addProductFilter($productIds)   // find products associated with these 
     ->addExcludeProductFilter($productIds) // don't let these sames ones be loaded twice, that's a guaranteed error 
     ->addAttributeToSelect(array('name', 'url_key, 'url_path')); // list as many attributes here as you need 
    return $collection; 
} 

这返回的Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection一个对象,你可以从一个模板的方式为产品列表相同的迭代。这一点是开放的调整和主题,你认为合适。

<?php $_crossSellsCollection = $this->getCrossSellsCollection(); ?> 
<?php if ($_crossSellsCollection->count()): ?> 
<ul> 
    <?php foreach ($_crossSellsCollection as $_product): ?> 
    <li> 
     <a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName() ?></a> 
    </li> 
    <?php endforeach ?> 
</ul> 
<?php endif ?> 

(希望你最近学会了如何接受的答案,但你的0%的分数表明othewise)

+0

得到它的帮助表示感谢 – 2010-11-05 03:51:28

+0

谢谢,clockworkgeek。这是一个可惜的,我不能downvote的作者(125声誉阈值):) – Wiseman 2011-03-23 09:16:07