Jump to content

Prestashop 1.7 avoid Out of stock products from being in a products list


ZiedDams

Recommended Posts

Hi guys this is  my function code

$category = new Category($random_category_id);

$searchProvider = new CategoryProductSearchProvider(
  $this->context->getTranslator(),
  $category
);

$context = new ProductSearchContext($this->context);

$query = new ProductSearchQuery();

$nProducts = $my_nb_products;
$query
	->setResultsPerPage($nProducts)
	->setPage(1);
	->setSortOrder(new SortOrder('product', 'position', 'asc'));

$result = $searchProvider->runQuery(
	$context,
	$query
);

$assembler = new ProductAssembler($this->context);
$presenterFactory = new ProductPresenterFactory($this->context);
$presentationSettings = $presenterFactory->getPresentationSettings();
$presenter = new ProductListingPresenter(
	new ImageRetriever(
    $this->context->link
    ),
    $this->context->link,
    new PriceFormatter(),
    new ProductColorsRetriever(),
    $this->context->getTranslator()
);
$product_list = array();
foreach ($result->getProducts() as $rawProduct) {
	$product_list[] = $presenter->present(
    	$presentationSettings,
    	$assembler->assembleProduct($rawProduct),
    	$this->context->language
	);
}
return $product_list;

is there a way to prevent out of stock product from being in the product list

for example adding something to the

ProductSearchQuery

using the

$query->setSearchString("") // i don't know what to put here

Or my be another solution ...

Any help Please !!

Link to comment
Share on other sites

You could change the query in the getProducts method(class Category), where you can add stock check (this function is used to bring the products), or you could change the settings for product visibility, i.e. when a product is out of stock, you change its visibility to nowhere, and when it comes back in stock, you make it visible again (there are modules that do this, you can even find free ones).

 

Link to comment
Share on other sites

You can try to include in the foreach something like this

 

foreach ($result->getProducts() as $rawProduct) {

  if($rawProduct['quantity'] < 1) continue;

$product_list[] = $presenter->present( $presentationSettings,

$presentationSettings,

$assembler->assembleProduct($rawProduct),

$this->context->language

);

}

Link to comment
Share on other sites

39 minutes ago, ventura said:

You can try to include in the foreach something like this

 

foreach ($result->getProducts() as $rawProduct) {

  if($rawProduct['quantity'] < 1) continue;

$product_list[] = $presenter->present( $presentationSettings,

$presentationSettings,

$assembler->assembleProduct($rawProduct),

$this->context->language

);

}

It's ok if he doesn't need a certain number of products, or if he doesn't use pagination. Because for example, if 20 products are brought in, and some of them are out of stock, he will be left with less.

  • Like 1
Link to comment
Share on other sites

2 hours ago, Ress said:

You could change the query in the getProducts method(class Category), where you can add stock check (this function is used to bring the products), or you could change the settings for product visibility, i.e. when a product is out of stock, you change its visibility to nowhere, and when it comes back in stock, you make it visible again (there are modules that do this, you can even find free ones).

 

thanks you @Ress , this is actually one of the weirdest problem that i faced in Prestashop it seam like a simple condition , I also found other posts with this same problem and weird solutions , Have a nice day .

Link to comment
Share on other sites

12 minutes ago, ventura said:

You can try to include in the foreach something like this

 

foreach ($result->getProducts() as $rawProduct) {

  if($rawProduct['quantity'] < 1) continue;

$product_list[] = $presenter->present( $presentationSettings,

$presentationSettings,

$assembler->assembleProduct($rawProduct),

$this->context->language

);

}

well, thank you for your time.

Link to comment
Share on other sites

  • 1 month later...
On 10/5/2022 at 3:11 PM, ZiedDams said:

the closest solution that i found was this ..

$query->setSortOrder(new SortOrder('product','quantity','DESC'));

 

This code helped me. Is it possible to display random products, but only those that are in stock?

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...