Magento 1.6.2用于在子类别中从一个产品移动到另一个产品的上一个下一个按钮

Magento 1.6.2用于在子类别中从一个产品移动到另一个产品的上一个下一个按钮,magento,Magento,我想创建一个上一个下一个按钮,用于在特定类别中从一个产品移动到另一个产品 我在view.phtml中使用了这段代码,但现在它为$search\u参数提供了未定义的变量错误 <?php // Previous and Next product links in product page $_product = $this->getProduct(); if(!$_product->getCategoryIds()) return; // Don't show Previous

我想创建一个上一个下一个按钮,用于在特定类别中从一个产品移动到另一个产品

我在view.phtml中使用了这段代码,但现在它为$search\u参数提供了未定义的变量错误

<?php // Previous and Next product links in product page

$_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

$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) );
}
?>                



<div class="clear"></div>
 <!--PREVIOUS BUTTON NEXT BUTTON STARTS-->
 <div class="previousNext">

<?php if($_prev_prod != NULL): ?>
<a href="<?php print $_prev_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>"><span style="margin-right: 270px;"><?php echo $this->__('<< PREVIOUS') ?></span></a>
<?php endif; ?>
<?php if($_next_prod != NULL): ?>
<a href="<?php print $_next_prod->getUrlPath(); if($search_parameter):?>?search=1<?php endif;?>"><span style="margin-left: 270px;"><?php echo $this->__('NEXT >>') ?></span></a>
<?php endif; ?>

</div>
<!--PREVIOUS BUTTON NEXT BUTTON ENDS-->
<div class="clear"></div>

他们终于在我的网站上获得了上述代码

以下是所遵循的步骤:

步骤1:添加产品时,确保每个产品仅添加到一个类别。如果产品属于多个类别,则此代码不起作用。换句话说,如果您要添加一个产品来表示Cat1>Subcat1>subsubcat2,请确保只将其添加到subsubcat2。您将能够在该子类别中移动上一个

步骤2:打开view.phtml并添加以下代码:

<?php // Previous and Next product links in product page

$_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

$order = Mage::getStoreConfig('catalog/frontend/default_sort_by');
$direction = 'desc'; // 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) );
}
?>

如果你只是从某个地方复制粘贴了这段代码,你可能会漏掉几行。thnks伙计们,我从这个网站上获取了这段代码。我尝试使用上面的代码,完全删除$search_参数。代码是有效的。。。但当它到达一个类别中的产品末尾时,它会显示下一个类别的产品,但带有指向当前类别的链接。。。因此给出了一个页面错误。谢谢各位…这是难以置信的糟糕代码。模板文件不应该包含这样的代码。
 <!--PREVIOUS BUTTON NEXT BUTTON STARTS-->
 <div class="previousNext">

<?php if($_prev_prod != NULL): ?>
<a href="<?php print $_prev_prod->getUrlPath();?>"><span style="margin-right: 270px;"><?php echo $this->__('<< PREVIOUS') ?></span></a>
<?php endif; ?>
<?php if($_next_prod != NULL): ?>
<a href="<?php print $_next_prod->getUrlPath();?>"><span style="margin-left: 270px;"><?php echo $this->__('NEXT >>') ?></span></a>
<?php endif; ?>

</div>
<!--PREVIOUS BUTTON NEXT BUTTON ENDS-->
<style>
.previousNext {width: 100%; height: 20px; text-align: right;  }
.previousNext a:link {font-size: 12px; color:#ffffff; font-weight: bold; text-decoration: none}
.previousNext a:visited {font-size: 12px; color:#ffffff; font-weight: bold; text-decoration: none}
.previousNext a:hover {font-size: 12px; color:#ff9900; font-weight: bold; text-decoration: underline}
.previousNext a:active {font-size: 12px; color:#ffffff; font-weight: bold; text-decoration: none}
</style>