ZiedDams Posted October 5, 2022 Share Posted October 5, 2022 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 More sharing options...
ZiedDams Posted October 5, 2022 Author Share Posted October 5, 2022 the closest solution that i found was this .. $query->setSortOrder(new SortOrder('product','quantity','DESC')); Link to comment Share on other sites More sharing options...
Ress Posted October 7, 2022 Share Posted October 7, 2022 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 More sharing options...
ventura Posted October 7, 2022 Share Posted October 7, 2022 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 More sharing options...
Ress Posted October 7, 2022 Share Posted October 7, 2022 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. 1 Link to comment Share on other sites More sharing options...
ZiedDams Posted October 7, 2022 Author Share Posted October 7, 2022 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 More sharing options...
ZiedDams Posted October 7, 2022 Author Share Posted October 7, 2022 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 More sharing options...
Peter.31 Posted December 4, 2022 Share Posted December 4, 2022 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now