通过产品网址获取类别网址

时间:2015-03-24 11:36:36

标签: php magento wamp

我如何获得category url through product url

我有产品网址,但我希望获得类别url enter image description here

我想在用户点击产品名称时重定向到类别页面

Site Link

我的代码是:

<?php
    if($this->getItems()->getSize()):
        $category = new Mage_Catalog_Model_Category();
        $category->load($categoryid);
        $collection = $category->getProductCollection();
        $collection->addAttributeToSelect('*');
        ?>
        <div class="col-lg-7 col-md-7 col-sm-7 cpl-xs-12">  
            <div class="ndlSimilarTop">
                <div class="ndlSimilatProductTitle">RECOMMENDED PRODUCTS</div>
                <div id="amazingcarousel-container-1" style="overflow:hidden">
                    <div id="amazingcarousel-1" style="display:block;position:relative;width:100%;max-width:711px;margin:0px auto 0px;">
                        <div class="amazingcarousel-list-container" style="overflow:hidden;">
                            <ul class="amazingcarousel-list">
                              <?php foreach($this->getItems() as $_item): ?>
                                    <li class="amazingcarousel-item">
                                        <div class="amazingcarousel-item-container">
                                            <div class="amazingcarousel-image">
                                                <img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(217, 173) ?>"  alt="product-img" />

                                                <ul class="mask mask1">
                                                    <li class="ndlHoverContent ndlHoverContent1">
                                                        <a href="<?php echo $this->getAddToWishlistUrl($_item) ?>" class="ndlListWishlist ndlListWishlist1 ndlListWishlist2"><img src="<?php echo $this->getSkinUrl() ?>images/wishlist-white-icon.png" alt="">Add to wishlist</a>
                                                        <a href="#" class="ndlListAddtobag ndlListAddtobag1 ndlListAddtobag2"><img src="<?php echo $this->getSkinUrl() ?>images/mybag-white-icon.png" alt="">Add to bag</a>
                                                    </li>
                                                </ul>
                                            </div>
                                            <?php echo "Product Url:".$_item->getProductUrl();
                                             echo "CAT Url:".$_item->getCategoryUrl(); 
                                            ?>
                                            <div class="ndlListDetail">
                                                <div class="ndlProductListName"><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $this->htmlEscape($_item->getName()) ?></a>
                                                <?php echo $this->getPriceHtml($_item, true, '-related') ?>
                                            </div>

                                        </div>
                                    </li>
                                <?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

下面是一个这样的解决方案:

<?php
    $productUrlPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
    $rewrite = Mage::getModel('core/url_rewrite')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->loadByRequestPath($productUrlPath);
    $productId = $rewrite->getProductId();
    $product = Mage::getModel('catalog/product')->load($productId);
    $categoriesId = $product->getCategoryIds();
    foreach($categoriesId as $categoryId){
        $category = Mage::getModel('catalog/category')->load($categoryId);
        echo $category->getUrl();
    }
?>

注意:您可以拥有多个类别的产品。