0

I'm new to Magento development. I was trying to display related products in the product view.

While method ->getName() works fine, ->getProductUrl() renders the path of the actual product. I tried create an external link for www.google.com, i had the same issue.

Thank you for your help.

Here is my code :

$categoryId = array_shift($_product->getCategoryIds());
$related_prods = Mage::getModel('catalog/product')
                ->getCollection()
                ->addAttributeToSelect('*')
                ->addUrlRewrite($categoryId)
                ->addAttributeToFilter('entity_id',
                        array('in'=>$_product->getRelatedProductIds()));

<?php foreach($related_prods as $related) { ?>
<a href="<?php $related->getProductUrl()."<br />";?>">
<?php echo $related->getName();?></a>
<?php } ?>
1
  • please post your code in the question
    – Marius
    Nov 11, 2015 at 16:16

2 Answers 2

2

Don't reinvent the wheel, code for loading the related products already exists in the core. There is a block that shows related products and in the default themes you don't even need to do anything to show it. If it is missing in your theme, you can add this to your theme's layout XML:

<reference name="product.info">
    <block type="catalog/product_list_related" name="catalog.product.related" as="related_products" template="catalog/product/list/related.phtml" />
</reference>

And in the product template, where you want to show the related products:

<?php echo $this->getChildHtml('related_products'); ?>

Now you can adjust catalog/product/list/related.phtml to change the HTML, or create a new template and change the path in the XML above.

Minimal working template example with name and URL:

<?php foreach($this->getItems() as $_item): ?>
    <a href="<?php echo $_item->getProductUrl() ?>">
        <?php echo $this->escapeHtml($_item->getName()) ?>
    </a>
<?php endforeach; ?>

If you don't understand where to add which code, read: What is the correct way/approach to modify a Magento template?

0

I am not sure about question,may be want to get product url with category path.

First,you should reindex for generate proper url for getProductUrl()

addUrlRewrite() function add category path to product url whenever the setting Use Categories Path for Product URLs [ setting location admin>system>Configuration>Catalog>Catalog/Search Engine Optimization] is yes for current store other wise magento cannot append category path to url.

You can see that condition at class Mage_Catalog_Model_Resource_Product_Collection.

   public function addUrlRewrite($categoryId = '')
    {
        $this->_addUrlRewrite = true;
        if (Mage::getStoreConfig(Mage_Catalog_Helper_Product::XML_PATH_PRODUCT_URL_USE_CATEGORY, $this->getStoreId())) {
            $this->_urlRewriteCategory = $categoryId;
        } else {
            $this->_urlRewriteCategory = 0;
        }

        if ($this->isLoaded()) {
            $this->_addUrlRewrite();
        }

        return $this;
    }

Also addUrlRewrite() only except one category id of a particular collection and does not support multiple categories ids of certain product collection.

Else, array_shift($_product->getCategoryIds()); is not proper way to get category ids.,because of getCategoryIds() give a array like

Assume that product have two category ids

Then out put like ['0'=>'Cat_ID_first','1'=>'Cat_ID_Second'].

So, array_shift is not proper way get category Id. If you have single category id then use

if(count($_product->getCategoryIds())>0):
$categoryIds = $_product->getCategoryIds();
$categoryId = $categoryIds[0] ;

... do what ever
endif;
1
  • Thank for you answer. The answer that was given above was correct and i forgot to accept it.
    – mockingbid
    Jan 18, 2016 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.