prestashop create new prouct lazy array

Solutions on MaxInterview for prestashop create new prouct lazy array by the best coders in the world

showing results for - "prestashop create new prouct lazy array"
Greta
17 Jan 2018
1/**
2     * Creates relevant product information for frontend output
3     * Uses PrestaShop LazyArray
4     * 
5     * @author R4xx4r
6     * @link https://www.prestashop.com/forums/topic/1027643-how-to-get-a-productlistinglazyarray/
7     * 
8     * @param array $allSelectedProductIds array with all id's of the selected products 
9     * @param int $languageId language id of the shop you are in
10     * 
11     * @return array all product information we need for our frontend rendering
12     */
13    public function getFrontendProductInformation($allSelectedProductIds, $languageId)
14    {
15        // set default category Home
16        $category = new Category((int)2);
17
18        // create new product search proider
19        $searchProvider = new CategoryProductSearchProvider(
20            $this->context->getTranslator(),
21            $category
22        );
23
24        // set actual context
25        $context = new ProductSearchContext($this->context);
26        
27        // create new search query
28        $query = new ProductSearchQuery();
29        $query->setResultsPerPage(PHP_INT_MAX)->setPage(1);
30        $query->setSortOrder(new SortOrder('product', 'position', 'asc'));
31        
32        $result = $searchProvider->runQuery(
33            $context,
34            $query
35        );
36
37        // Product handling - to get relevant data
38        $assembler = new ProductAssembler($this->context);
39        $presenterFactory = new ProductPresenterFactory($this->context);
40        $presentationSettings = $presenterFactory->getPresentationSettings();
41        $presenter = new ProductListingPresenter(
42            new ImageRetriever(
43                $this->context->link
44            ),
45            $this->context->link,
46            new PriceFormatter(),
47            new ProductColorsRetriever(),
48            $this->context->getTranslator()
49        );
50
51        $products = array();
52        foreach ($result->getProducts() as $rawProduct) {
53            $productId = $rawProduct['id_product'];
54            if(in_array($productId, $allSelectedProductIds)) {
55                $product = $presenter->present(
56                    $presentationSettings,
57                    $assembler->assembleProduct($rawProduct),
58                    $this->context->language
59                );
60                array_push($products, $product);
61            }
62        }
63
64        return $products;
65    }