2

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;
}
2
  • Please share the code? Product next and previous is not default feature?
    – Amit Bera
    Commented Jul 2, 2018 at 7:52
  • @AmitBera It's from the Porto theme, I've put the template code
    – chdzma
    Commented Jul 2, 2018 at 8:14

1 Answer 1

0

Assume that you have a product, Now you need to filter your product collection by in stock product.

Magento2 have a function addIsInStockFilterToCollection at Magento\CatalogInventory\Helper\Stock by which you can filter a collectiuon by in stock product.

Edit:

Inject the class at __constuct function

\Magento\CatalogInventory\Helper\Stock

like

   /**
     * @var Magento\CatalogInventory\Helper\Stock
     */
    protected $stockHelper;

    public function __construct(
.....
    \Magento\CatalogInventory\Helper\Stock $stockHelper
....
       ) {

           $this->stockHelper = $stockHelper;
    }

then filter your collection at getCategoryProductIds

public function getCategoryProductIds($current_category) {
    $category_products = $current_category->getProductCollection()
            ->addAttributeToSelect('*')
            ->addAttributeToSort('position', 'asc');
    // add Stock in stock 
    $this->stockHelper->addIsInStockFilterToCollection($collection);

    $cat_prod_ids = $category_products->getAllIds();

    return $cat_prod_ids;
}
1
  • I have changed addIsInStockFilterToCollection($collection) by addInStockFilterToCollection($collection) and works now.
    – chdzma
    Commented Jul 2, 2018 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.