Search code examples
phpprestashopprestashop-1.7prestashop-modules

PrestaShop: How to get a ProductListingLazyArray out of my products


I'm pretty new to PrestaShop - so sry if i ask basic thing

I'm currently working on a module which should display products you chose in the backend as additional section in the default products template - like "highly recommended products"

I finish the whole Backend part, and get the ID's as an array of the chosen products.

As I mentioned I wanna use the default templates which are available after a fresh installation and what I found is placed here themes\classic\templates\catalog\_partials\products.tpl.

Now my big problem is: I'm not able to get the data like it should be ...

If I debug e.g. the products which are displayed in the default search behaviour (this uses this template too) I see something like

object(PrestaShop\PrestaShop\Adapter\Presenter\Product\ProductListingLazyArray)#323 (11) { ["imageRetriever":"Pr .....

but as I get my products with

new Product($productId, true);

it is no ProductListingLazyArray ... its just an array with products ... and i dont see anything in the frontend (of course I dont, cause e.g. {$product.id_product} doesnt look like this in my array ...

Have you any ideas what I can do to "transform" my array of products to an ProductListingLazyArray ?? Or is my thinking wrong ?

THANKS to you all!


Solution

  • Solution
    I just "faked" a search and check if the data is in my array:

    /**
     * creates relevant product information for frontend output
     * 
     * @param array $allSelectedProductIds array with all id's of the selected products 
     * @param int $languageId language id of the shop you are in
     * 
     * @return array all product information we need for our frontend rendering
     */
    public function getFrontendProductInformation($allSelectedProductIds, $languageId)
    {
        // set default category Home
        $category = new Category((int)2);
    
        // create new product search proider
        $searchProvider = new CategoryProductSearchProvider(
            $this->context->getTranslator(),
            $category
        );
    
        // set actual context
        $context = new ProductSearchContext($this->context);
    
        // create new search query
        $query = new ProductSearchQuery();
        $query->setResultsPerPage(PHP_INT_MAX)->setPage(1);
        $query->setSortOrder(new SortOrder('product', 'position', 'asc'));
    
        $result = $searchProvider->runQuery(
            $context,
            $query
        );
    
        // Product handling - to get relevant data
        $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()
        );
    
        $products = array();
        foreach ($result->getProducts() as $rawProduct) {
            $productId = $rawProduct['id_product'];
            if(in_array($productId, $allSelectedProductIds)) {
                $product = $presenter->present(
                    $presentationSettings,
                    $assembler->assembleProduct($rawProduct),
                    $this->context->language
                );
                array_push($products, $product);
            }
        }
    
        return $products;
    }