magento交叉销售子类别

时间:2010-11-01 15:09:26

标签: magento

您好 我有一个类别XXX类别我有一个子类别yyy。 产品被分配到yyy类别。此外,我已经交叉销售了5种产品。 当我点击yyy时,我需要获得在左侧指定为交叉销售的所有交叉销售产品名称。

分类

XXXXXXXX
 YYYYYYY
 zzzzzzz

交叉销售产品

Crosssell1 Crosssell2 Crosssell3

我怎么能去做?

1 个答案:

答案 0 :(得分: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%分数表示不同)

相关问题