php 产品导航навигацияпопродуктам

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 产品导航навигацияпопродуктам相关的知识,希望对你有一定的参考价值。

<?php
  $_helper = $this->helper('catalog/output');
  $_product = $this->getProduct();
  $prev_url = $next_url = $url = $_product->getProductUrl();

  if ($this->helper('catalog/data')->getCategory())
  {
      $category = $this->helper('catalog/data')->getCategory();
  }
  else
  {
      $_ccats = $this->helper('catalog/data')->getProduct()->getCategoryIds();
      $category = Mage::getModel('catalog/category')->load($_ccats[0]);
  }
  if(!$_product->getCategoryIds())
  {
      $not_in_cat = 1;
  }

  $children = $category->getProductCollection();
  $_count = is_array($children) ? count($children) : $children->count();
  if ($_count)
  {
      foreach ($children as $product) {
          $plist[] = $product->getId();
      }
      /**
       * Determine the previous/next link and link to current category
       */
      $current_pid  = $this->helper('catalog/data')->getProduct()->getId();
      $curpos   = array_search($current_pid, $plist);
      // get link for prev product
      $previd   = isset($plist[$curpos+1])? $plist[$curpos+1] : $current_pid;
      $product  = Mage::getModel('catalog/product')->load($previd);
      $prevpos  = $curpos;
      while(!$product->isVisibleInCatalog())
      {
          $prevpos += 1;
          $nextid   = isset($plist[$prevpos])? $plist[$prevpos] : $current_pid;
          $product  = Mage::getModel('catalog/product')->load($nextid);
      }
      $prev_url = $product->getProductUrl();
      // get link for next product
      $nextid   = isset($plist[$curpos-1])? $plist[$curpos-1] : $current_pid;
      $product  = Mage::getModel('catalog/product')->load($nextid);
      $nextpos  = $curpos;
      while(!$product->isVisibleInCatalog())
      {
          $nextpos -= 1;
          $nextid   = isset($plist[$nextpos])? $plist[$nextpos] : $current_pid;
          $product  = Mage::getModel('catalog/product')->load($nextid);
      }
      $next_url = $product->getProductUrl();
  }
  if(empty($not_in_cat)):
?>
<ul class="category-nav">
  <?php if ($url <> $prev_url):?>
  <li>
      <a class="prev" href="<?php echo $prev_url; ?>">
          <?php echo $this->__('&laquo; Previous'); ?>
      </a>
  </li>
  <?php endif; ?>
  <?php if ($url <> $next_url):?>
  <li>
      <a class="next" href="<?php echo $next_url; ?>">
          <?php echo $this->__('Next &raquo;'); ?>
      </a>
  </li>
  <?php endif; ?>
</div>
<?php endif; ?>

$_product = $this->getProduct();
    
if(!$_product->getCategoryIds())
    return; // Don't show Previous and Next if product is not in any category
 
$cat_ids = $_product->getCategoryIds(); // get all categories where the product is located
$cat = Mage::getModel('catalog/category')->load( $cat_ids[0] ); // load first category, you should enhance this, it works for me
$category_url = $cat->getUrl();
 
$order = Mage::getStoreConfig('catalog/frontend/default_sort_by');
$direction = 'asc'; // asc or desc
 
$category_products = $cat->getProductCollection()->addAttributeToSort($order, $direction);
$category_products->addAttributeToFilter('status',1); // 1 or 2
$category_products->addAttributeToFilter('visibility',4); // 1.2.3.4
 
$cat_prod_ids = $category_products->getAllIds(); // get all products from the category
$_product_id = $_product->getId();
 
$_pos = array_search($_product_id, $cat_prod_ids); // get position of current product
$_next_pos = $_pos+1;
$_prev_pos = $_pos-1;
 
// get the next product url
if( isset($cat_prod_ids[$_next_pos]) ) {
    $_next_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_next_pos] );
}
else {
    $_next_prod = Mage::getModel('catalog/product')->load( reset($cat_prod_ids) );
}

// get the previous product url
if( isset($cat_prod_ids[$_prev_pos]) ) {
    $_prev_prod = Mage::getModel('catalog/product')->load( $cat_prod_ids[$_prev_pos] );
} else {
    $_prev_prod = Mage::getModel('catalog/product')->load( end($cat_prod_ids) );
}
 
<ul class="product-nav">
  <?php if($_prev_prod != NULL): ?>
      <li><a href="<?php echo $_prev_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>">&lt; <?php echo $this->__('Previous'); ?> <span>(<?php echo $_prev_prod->getName(); ?>)</span></a></li>
  <?php endif; ?>
  <li><a class="back" href="<?php echo $category_url; ?>"><?php echo $this->__('Back to') . ' ' . $cat->getName(); ?></a></li>
  <?php if($_next_prod != NULL): ?>
      <li><a href="<?php echo $_next_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>"><?php echo $this->__('Next'); ?> <span>(<?php echo $_next_prod->getName(); ?>)</span> &gt;</a></li>
  <?php endif; ?>
</ul>

以上是关于php 产品导航навигацияпопродуктам的主要内容,如果未能解决你的问题,请参考以下文章

php Проверкаестьлифильтрынавигацияпродуктывтекущейкатегории

markdown Интеграцияновогодокумента(БСП)

php ВозвращаетIDрубриктоваровдляуказанногопродавца。

python Скриптпреобразованияматрицызаполняемойпострочновматрицузаполняемуюпоспирали---найденчерезгугл

php Хрензнаетдлячегоэтафункция,нопоназваниюпонятночтодлядебага

php Регистрацияпользователявопределеннуюгруппу