0

I'm trying to get the Category URL in my Home page, I'm working in the file app/design/fronted/[package]/[theme]/template/catalog/product/widget/new/content/new_grid.phtml

I've got this code above the product title

<?php
/**
 * get categories from a product
 */
$categoryIds = $this->helper('catalog/output')->productAttribute($_product, $_product->getCategoryIds());
/**
 * looping through the array of category ids
 */
foreach($categoryIds as $categoryId) {
    $category = Mage::getModel('catalog/category')->load($categoryId);
    if($category->parent_id == 4):
?>
    <h6 class="author"><a href="<?php echo $category->getUrlKey() ?>">De <?php echo $category->getName() ?></a></h6>
<?php
    endif;
}
?>

Some times I use "getUrl" instead of "getUrlKey", and both give me a "404" page. I believe the error is in some Magento configuration, not in the code.

Some one have an answer to any approach?

Thanks in advence!

1

Re-index everything and especially make sure that Catalog URL Rewrites is up to date.

Also check mod_rewrite to ensure that it is enabled. You can tell if this is the problem when you have SEO Friendly URLs turned on but still need to put index.php in the URL.

1

Easiest way to show category links at home page as follows

  1. Go to CMS Page[Home Page] at admin end.
  2. Edit Home page content. There is a widget option in content editor.enter image description here

  3. After click on that option, there will be a pop up window. After that select "Catalog Category links" from dropdown.enter image description here

  4. After that click on select category to get category list.

  • Thanks @suvankar-giri, but that's not what I need. I've choose "Catalog New Product List", and need that the CATEGORY's name with link appear between the product title and price. Yet, I try to add that widget and the link genereted gives me "404" too. – Felipe Abranches Jan 30 '15 at 20:38
  • the root category "mysite.com/catalog/category/view/s/artistas/id/16/?___store=default". the artist category "mysite.com/catalog/category/view/s/carolina-mazzini/id/17/?___store=default" – Felipe Abranches Jan 30 '15 at 20:39
1

First of all, do not copy, borrow, create or even think about code in templates that follows the performance killing principle: $model->load($id) within a loop. In fact, try to avoid load() all together.

Secondly, the Catalog/Output output is there to generate HTML code for catalog data and yet you're using it to obtain information that is readily available on the product model.

Thirdly, getUrlKey() generates almost always 404's if used as only thing in a link, because it doesn't contain the base URL, nor a slash prefix and any URL suffix configured in the backend.

So, we get:

// Obtain a Magento collection for all associated categories.
$categories = $_product->getCategoryCollection();
// Make sure we get their friendly URLs and filter parent_id:
$categories->addUrlRewriteToResult()
    ->addAttributeToFilter('parent_id', 4);
// Now we loop through the category collection
foreach($categories AS $category) :

At this point, $category->getUrl() should generate the correct URL. Make sure the URL rewrite index is up to date. More on Magento collections here.

URL Rewrite issues

Your url rewrite table seems to be polluted, but there's also indications your standard router won't work, so let's verify this first.

  1. Disable all caches in the Magento backend.
  2. Run this SQL query: DELETE FROM core_url_rewrite WHERE request_path='catalog/category/view/id/14'
  3. Turn off flat category and flat product tables in backend, to eliminate noise.
  4. Request your homepage to see if Magento is working.
  5. Request http://yoursite.com/catalog/category/view/id/14

If 5 works, your router is working. If 5 gives you a webserver 404, then mod_rewrite isn't enabled and/or rewrite rules aren't set correctly. If 5 gives you a Magento 404, then your standard router is broken.

  • Thank's @Melvyn, I've used your code, but I've retrieve the same links that I've retrieve before. I think the problem is in Category config. I have the ROOT category that already came in Magento, and others 2: "Categories" and "Artists". "Categories" I use as parent category to the categories' left navigation (it's configured in Admin). This second category, "Artists", is the one that's giving me trouble. Look the "404" url: "mysite.com/catalog/category/view/s/felipe-abranches/id/14/", where "felipe-abranches" is a child category of "Artists" category, "14" is "felipe-abranches" id. – Felipe Abranches Jan 30 '15 at 20:34
  • That doesn't look right at all. It has the id_path with a string s/felipe-abranches/ mixed in. Which sort of looks like a part of a sed regular expression. The id_path is always available, so your category should be viewable with /catalog/category/view/id/14. What's the result of the following database query: 'SELECT id_path, target_path, request_path FROM core_url_rewrite WHERE id_path LIKE "%category%/id/14%"' – Melvyn Jan 30 '15 at 20:42
  • using "%category%/id/14%" gives 0. using "%category/14%" gives id_path: category/14, target_path: catalog/category/view/id/14, request_path: felipe-abranches.html. the links turns "mysite.com/felipe-abranches.html" but still gives me 404. There wasn't this rule in "Catalog URL Rewrites", so I created it, as all the others children of "Artists" category, altrough all "Categories"' children where there. Is some config that I've missed in "Artists" and children to make the url work? Thanks! – Felipe Abranches Jan 30 '15 at 23:22
  • Thanks a lot for the atention, @Melvyn. I took the five steps twice, and I still have the link mysite.com/catalog/category/view/s/felipe-abranches/id/18/ given me 404, and editing by hand to mysite.com/catalog/category/view/id/18/ gives me 404 too. So, my standard router is broken... How do I solve that? Have I to open a new question? EDITED: All the categories' checkbox "Create Permanent Redirect for old URL" is marked, all children categories of "Categories" heva an autogenerated friendly URL, but all children categories of "Artists" haven't – Felipe Abranches Feb 2 '15 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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