开发者

magento cross selling subcategory

Hi I have a category XXX under the category i have a subcategory yyy. The products are assigned to the yyy category.Also i have cross sold 5 products . When i click the yyy i need to get all the cross sold product names which are assigned as crossselling in the left .

Categories

XXXXXXXX

yyyyyyy

zzzzzzz

CROSSSELLING PRODUCTS

Crosssell1 Crosssell2 Crosssell3

H开发者_开发问答ow i can go about it ?


You will need to create a block that will appear in the left column. Let's make a function to get the initial list of product IDs. If your block extends Mage_Catalog_Block_Product_List then it might like something like this:

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

But that will only work for products currently being shown on the page. If you need to show for all associated products on later pages in the category then the function might be like this:

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

Either function will return a list of integers.

Next is the essential part. Getting the associated products.

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;
}

This returns an object of Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Link_Product_Collection which you can iterate over from a template the same way as a product list. This bit is open to adjusting and theming as you see fit.

<?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 ?>

(Hopefully you have recently learnt how to accept answers, but your 0% score suggests othewise)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜