In the product information, going to the next product shows me some that are out of stock.
I have the configuration that there are no products out of stock ...
Edit:
I think the problem comes from here and should filter the products looking at the stock:
public function getCategoryProductIds($current_category) {
$category_products = $current_category->getProductCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_saleable', 1, 'left')
->addAttributeToSort('position','asc');
$cat_prod_ids = $category_products->getAllIds();
return $cat_prod_ids;
}
How do I filter the products in stock?
Template
<?php
$porto_helper = $this->helper('Smartwave\Porto\Helper\Data');
$product_view = $porto_helper->getConfig('porto_settings/product');
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
if(isset($product_view['prev_next']) && $product_view['prev_next']) {
$_product = $block->getProduct();
$prev_product = $porto_helper->getPrevProduct($_product);
$next_product = $porto_helper->getNextProduct($_product);
Helper
public function getPrevProduct($product) {
$current_category = $product->getCategory();
if(!$current_category) {
foreach($product->getCategoryCollection() as $parent_cat) {
$current_category = $parent_cat;
}
}
if(!$current_category)
return false;
$cat_prod_ids = $this->getCategoryProductIds($current_category);
$_pos = array_search($product->getId(), $cat_prod_ids);
if (isset($cat_prod_ids[$_pos - 1])) {
$prev_product = $this->getModel('Magento\Catalog\Model\Product')->load($cat_prod_ids[$_pos - 1]);
return $prev_product;
}
return false;
}
public function getNextProduct($product) {
$current_category = $product->getCategory();
if(!$current_category) {
foreach($product->getCategoryCollection() as $parent_cat) {
$current_category = $parent_cat;
}
}
if(!$current_category)
return false;
$cat_prod_ids = $this->getCategoryProductIds($current_category);
$_pos = array_search($product->getId(), $cat_prod_ids);
if (isset($cat_prod_ids[$_pos + 1])) {
$next_product = $this->getModel('Magento\Catalog\Model\Product')->load($cat_prod_ids[$_pos + 1]);
return $next_product;
}
return false;
}