【问题标题】:get category url through product url通过产品 url 获取类别 url
【发布时间】:2015-03-24 11:36:36
【问题描述】:

我怎样才能得到category url through product url

我有产品网址,但我想获得类别url

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

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

【问题讨论】:

  • 您的代码是否有任何错误?您可以从其 id 中获取类别 url,例如 Mage::getModel("catalog/category")->load($category_id)->getUrl();
  • 我必须把这段代码放在哪里
  • 可以使用product url获取category url吗?

标签: php magento wamp


【解决方案1】:

一个这样的解决方案可以如下:

<?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();
    }
?>

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

【讨论】:

    猜你喜欢
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2012-11-05
    • 2012-06-06
    • 1970-01-01
    相关资源
    最近更新 更多