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?
问题:
回答1:
Yes this can be done by using the URL Rewrite Management option in Magento Admin.
In Magento Admin:
- Select from menu bar Catalog > URL Rewrite Management.
- Click on the Add URLRewrite button.
- Select the category you want to redirect from.
- Make a note of the ID Path (e.g.: category/10) and the Request Path (e.g.: flowerpots.html)
- Repeat steps 1 & 2, but this time select Custom from the Create Urlrewrite dropdown box.
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.
Select Redirect Permanent (301) from the Redirect dropdown.
- Save.
Now when the category is clicked on on your website it will re-direct to the product.
回答2:
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/
回答3:
Follow the Steps:
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).
Just after the lines
/** * Template for Mage_Page_Block_Html */
- 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;
}
}
}
}
?>