Jump to content

Best seller module: show only available products


Recommended Posts

Hello,

 

Go to YOUR_SHOP_ROOT_DIR/modules/blockbestsellers/

 

And open blockbestsellers.php file in an editor

 

now replace the following code

 

public function hookRightColumn($params)
    {
        if (Configuration::get('PS_CATALOG_MODE'))
            return ;

        global $smarty;
        $currency = new Currency((int)($params['cookie']->id_currency));
        $bestsellers = ProductSale::getBestSalesLight((int)($params['cookie']->id_lang), 0, 5);
        if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
            return;
        $best_sellers = array();
        if($bestsellers)
            foreach ($bestsellers AS $bestseller)
            {
                    $bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
                    $best_sellers[] = $bestseller;
            }
            
        $smarty->assign(array(
            'best_sellers' => $best_sellers,
            'mediumSize' => Image::getSize('medium')));
        return $this->display(__FILE__, 'blockbestsellers.tpl');
    }

 

with

 

public function hookRightColumn($params)
    {
        if (Configuration::get('PS_CATALOG_MODE'))
            return ;

        global $smarty;
        $currency = new Currency((int)($params['cookie']->id_currency));
        $bestsellers = ProductSale::getBestSalesLight((int)($params['cookie']->id_lang), 0, 5);
        if (!$bestsellers AND !Configuration::get('PS_BLOCK_BESTSELLERS_DISPLAY'))
            return;
        $best_sellers = array();
        if($bestsellers)
            foreach ($bestsellers AS $bestseller)
            {
                $product = new Product($bestseller['id_product']);
                if($product->quantity>0)
                {
                    $bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
                    $best_sellers[] = $bestseller;
                }
            }
            
        $smarty->assign(array(
            'best_sellers' => $best_sellers,
            'mediumSize' => Image::getSize('medium')));
        return $this->display(__FILE__, 'blockbestsellers.tpl');
    }

 

Now you can see only the products will be shown those are available(in stock) on your shop

 

Thanks :)

Link to comment
Share on other sites

Hi limon, thanks for the help.

I try your fix, it works, but it's not completely right.

 

Because in this way for example with 10 products showed (4 of that not available), it remove the not available but it doesn't replace with the next available products. So it show only 6 products.

 

I'm trying to show always 10, so if 1 is out of stock the module should take the 11th top seller. I hope you understand...

Link to comment
Share on other sites

Hello,

 

Thanks for following my instruction. Yes I know that. What you are wanting to get that you have to override a class named ProductSale.php

 

you can find this file at here YOUR_ROOT/classes/ProductSale.php

 

Open this file in a text editor and replace the following code

 

public static function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10)
    {
         global $link;

        if ($pageNumber < 0) $pageNumber = 0;
        if ($nbProducts < 1) $nbProducts = 10;
        
        $groups = FrontController::getCurrentCustomerGroups();
        $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');

        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
        SELECT p.id_product, pl.`link_rewrite`, pl.`name`, pl.`description_short`, i.`id_image`, il.`legend`, ps.`quantity` AS sales, p.`ean13`, p.`upc`, cl.`link_rewrite` AS category
        FROM `'._DB_PREFIX_.'product_sale` ps
        LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product`
        LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
        LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
        LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
        LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = p.`id_category_default` AND cl.`id_lang` = '.(int)$id_lang.')
        WHERE p.`active` = 1
        AND p.`id_product` IN (
            SELECT cp.`id_product`
            FROM `'._DB_PREFIX_.'category_group` cg
            LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
            WHERE cg.`id_group` '.$sqlGroups.'
        )
        ORDER BY sales DESC
        LIMIT '.(int)($pageNumber * $nbProducts).', '.(int)($nbProducts));

        if (!$result)
            return false;

        foreach ($result AS &$row)
        {
             $row['link'] = $link->getProductLink($row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
             $row['id_image'] = Product::defineProductImage($row, $id_lang);
        }
        return $result;
    }

 

 

With

 

public static function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10)
    {
         global $link;

        if ($pageNumber < 0) $pageNumber = 0;
        if ($nbProducts < 1) $nbProducts = 10;
        
        $groups = FrontController::getCurrentCustomerGroups();
        $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');

        $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
        SELECT p.id_product, pl.`link_rewrite`, pl.`name`, pl.`description_short`, i.`id_image`, il.`legend`, ps.`quantity` AS sales, p.`ean13`, p.`upc`, cl.`link_rewrite` AS category
        FROM `'._DB_PREFIX_.'product_sale` ps
        LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product`
        LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.')
        LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
        LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
        LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = p.`id_category_default` AND cl.`id_lang` = '.(int)$id_lang.')
        WHERE p.`active` = 1
        AND p.`quantity`>0
        AND p.`id_product` IN (
            SELECT cp.`id_product`
            FROM `'._DB_PREFIX_.'category_group` cg
            LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
            WHERE cg.`id_group` '.$sqlGroups.'
        )
        ORDER BY sales DESC
        LIMIT '.(int)($pageNumber * $nbProducts).', '.(int)($nbProducts));

        if (!$result)
            return false;

        foreach ($result AS &$row)
        {
             $row['link'] = $link->getProductLink($row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
             $row['id_image'] = Product::defineProductImage($row, $id_lang);
        }
        return $result;
    }

 

Now You will see your result.

Link to comment
Share on other sites

  • 4 months later...
  • 7 months later...
  • 11 months later...
  • 9 months later...

Hello. How can I do it in Prestashop 1.6? Thank you for support.

 

It is the same piece of code for prestashop 1.6.0.8. In classes/ProductSale.php, look for function getBestSalesLight and insert "AND stock.quantity > 0" in line 200. Remember to clear the cache in BO> Advance Parameters > Performance to see the changes reflected in home page.

Link to comment
Share on other sites

I tried to follow the instruction but i guesse its no more the same code.

The code is changed in the module blockbestsellers.

Can someone please provide me the new code where products which have quantity 0 are not displayed in this block

Also for block new products and block specials. I think all 3 are quite simular... so if one is provided i will manage. (hope)

 

Thanks

Link to comment
Share on other sites

  • 2 years later...

I found a solution in prestashop 1.6 under Preferences / Products and DISABLE "Display unavailable product attributes on the product page" and also "Allow ordering of out-of-stock products" Now my site only shows products on stock only

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...