Jump to content

[SOLVED] Get random products only with quantity greater than zero


Piotr Kaczor

Recommended Posts

Hello

I'm using method getProducts from object Category to get random products, but I need only products with quantity greater than zero. How to achieve that?

$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
$nb = (int)Configuration::get('HOME_FEATURED_RND_NBR');
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), null, null, false, true, true, ($nb ? $nb : 8));

Thank You for any advice :)

Best regards

Link to comment
Share on other sites

In the file classes/Category.php, copy the function GetProducts and change the name to getAvailableProducts or so.

then add to the its SQL WHERE part something like this:

 

WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
AND cp.`id_category` = '.(int)$this->id
.($active ? ' AND product_shop.`active` = 1' : '')
.' AND stock.quantity > 0 '
.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '')
.' GROUP BY product_shop.id_product';
 

Save the file and use the new function to get the products with available quantity.

 

Hope that helps.

 

 

Merry Christmas!!

pascal

  • Like 2
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...