Jump to content

Recommended Posts

Hi everybody,

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


I'm currently working on a module which should display products (you can choose in the backend area) as additional section in the default products template - like "highly recommended products".


I finished the whole Backend part have an array with my product ID's .

As I mentioned before I wanna use the default templates which are available after a fresh installation and so I the template which 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, which uses exactly this template too -  I see something like

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

So we have our product data as a ProductListingLazyArray.


I get my product data by looping over my array and make

 

 but as I get my products with 

$product = new Product($productId, true);

So of course I don't have a ProductListingLazyArray - I just have a product as an product object.

 

Do you have any idea - how I can "transform" my product object to this ProductListingLazyArray?
Or is it even the right way ?

 

THANKS for your help, ideas and recommendations!

 

Wish you a good start into the week!

Best

Edited by R4xx4r
solved the issue (see edit history)

Share this post


Link to post
Share on other sites

I just found the solution :)

I "faked" a search and then checked with the data i chose in the backend - here is the code:

 

/**
     * 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;
    }

 

Hope this helps somebody out there :)

If you have questions - just ask and I try to help :) 

 

Best

Share this post


Link to post
Share on other sites
  • 6 months later...

Hello, with this function is it possible to change front product display in a category ?

I've developped a module widget that display a form with selects to find some products and I managed to get the ids of the product but I don't know how to alter the product list.

Share this post


Link to post
Share on other sites
On 1/22/2021 at 5:09 PM, serwol said:

Hello, with this function is it possible to change front product display in a category ?

I've developped a module widget that display a form with selects to find some products and I managed to get the ids of the product but I don't know how to alter the product list.

Have a look at the stock ps_viewedproduct module. The function getViewedProducts() should contain the code you need to get the display data correctly, and the template in that module illustrates how to display them.

Edited by Paul C (see edit history)

Share this post


Link to post
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...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More