Search code examples
prestashopprestashop-1.6

Exclude products out of stock from list of new products - Prestashop


I am using a module that displays new added products in home page. I need to customize the module so that this list doesnt contain products sold. In other words, if a product is out of stock before the number of days it is condidered new is over, then do not show this product in list.

I can do it in view part by using {if $product.quantity < 0}{/if} but my goal is to perform it in controller. Here is my code:

function hookHome($params)
        {
        global $smarty, $cookie;


          $nb = intval(Configuration::get('HOME_NEW_PRODUCTS_NBR'));
        $rand = intval(Configuration::get('HOME_NEW_PRODUCTS_RANDOM'));
        if ($rand == 1) {
          $products = Product::getNewProducts(intval($cookie->id_lang), 0, $nb); 
        if ( $products )
        {
        shuffle($products);
        array_slice($products, ($nb ? $nb : 10));
        }
        }
        else 
        {
          $products = Product::getNewProducts(intval($cookie->id_lang), NULL - 0, (intval($nb ? $nb : 4)), false, NULL, NULL);
        }       
        $smarty->assign(array(
    ....
        'products' => $products,
    ....
    );
        return $this->display(__FILE__, 'homenewproducts.tpl');
        }

How can I override the class Product so that the method getNewProducts take into account excluding products out of stock?

Or at least, how can I remove from $products the products with quantity =0 using PHP?

Your help is appreciated.


Solution

  • Well, the solution I am using now is:

    In product.php, I changed the sql queries in getNewProducts method inside of NewProductsController so that it takes into account if product is available in stock

    I added AND 'quantity'!=0 in line 2062 and $sql->where('p.'quantity' != 0'); in line 2086 . Prestashop 1.6.0.6.

    Of course, better override the classe Product.php than modifying it.

    I hope it can help.