开发者

Directly go to Product Detail page on click of a category in the nav bar

开发者 https://www.devze.com 2023-03-23 15:07 出处:网络
In my store, one of the category has only one product. Is it possible to take the user directly to the product开发者_开发技巧 detail page of this one product whenever they click this category in the n

In my store, one of the category has only one product. Is it possible to take the user directly to the product开发者_开发技巧 detail page of this one product whenever they click this category in the nav bar?


Yes this can be done by using the URL Rewrite Management option in Magento Admin.

In Magento Admin:

  1. Select from menu bar Catalog > URL Rewrite Management.
  2. Click on the Add URLRewrite button.
  3. Select the category you want to redirect from.
  4. Make a note of the ID Path (e.g.: category/10) and the Request Path (e.g.: flowerpots.html)
  5. Repeat steps 1 & 2, but this time select Custom from the Create Urlrewrite dropdown box.
  6. Enter the values in each field:

    • ID Path (from step 4)
    • Request Path (from step 4)
    • Target Path - enter path of your product (or the page you want to redirect to).
      • example 1: for product url www.myswebsite.co.uk/flowers.html enter flowers.html.
      • example 2: for product url www.myswebsite.co.uk/sale/garden/flowers.html enter sale/garden/flowers.html.
  7. Select Redirect Permanent (301) from the Redirect dropdown.

  8. Save.

Now when the category is clicked on on your website it will re-direct to the product.


This can be done programmatically by adding code to the page template in your theme folder which is called for showing the categories. Check full solution here - http://www.codeboss.in/web-funda/2015/01/30/magento-auto-redirect-to-product-details-page-if-category-have-only-one-product/


Follow the Steps:

  1. Open the page template which is assigned to the category pages. You will find it under “template/page/” folder in your theme directory. For example let us assume the page is 1column.phtml (for the One column layout).

  2. Just after the lines

/** * Template for Mage_Page_Block_Html */

  1. Add the following code :
<?php 
$product = Mage::registry('current_product');
if($product == ''){
$category = Mage::registry('current_category');
if(is_object($category)){
$catLoaded = Mage::getModel('catalog/category')->load($category->getEntityId());
 $collection = $catLoaded->getProductCollection();
$collection->addAttributeToSelect('*');
if(count($collection) == 1){
foreach($collection as $product){
 $productUrl = $product->getProductUrl();
 header("location:$productUrl");
 exit;
  }
 }
 }
} 
?>
0

精彩评论

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