Directly go to Product Detail page on click of a c

2019-05-08 03:59发布

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?

3条回答
倾城 Initia
2楼-- · 2019-05-08 04:26

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;
  }
 }
 }
} 
?>
查看更多
Rolldiameter
3楼-- · 2019-05-08 04:28

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.

查看更多
Anthone
4楼-- · 2019-05-08 04:33

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/

查看更多
登录 后发表回答