Magento - 产品页面中的上一个和下一个产品链接(查看产品状态)

时间:2013-08-21 13:06:58

标签: php magento zend-framework

我在产品视图(view.phtml)中实现了“上一个/下一个”按钮,它工作正常。唯一的问题是它还试图显示状态为“已禁用”的产品。这导致404 Not found页面。

任何解决方法/解决方法?
这是我的代码:

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

任何帮助将不胜感激 感谢

1 个答案:

答案 0 :(得分:0)

启用该产品。

残疾产品无法在前端显示。