Jump to content

How to get category listing in a product


artivelab

Recommended Posts

Hello everyone,

 

I am using prestashop 1.7 and developing a template on it. It's my first time that I am trying to work on a template from scratch and play with prestashop's core.

 

What I want to do is:

When viewing a product on the product page I want to have 2 related products.

I added a hook there and installed a free module I found here "relatedFree module".

I played with the setting so I don't have to add manualythe to each product the category id, I pass it through smarty on the hook. 

 

So far so good. Where I am stack is, when trying to get the listing of 2 randoms products.

I used $category->getProducts(....); it returns an array of products but not like the one on the category page.

I tryied Product::getProducts(..);  yet again has different array from category page.

 

In caregory page (products.tpl), we get a default variable named "listing" that then we loop through to print all the products.

{foreach from=$listing.products item="product"}

That's the default functionality.

 

How can I get the same $listing variable in my code ?

the $listing variable has "url" for the link of the product and the cover image

but the $category-getProducts doesn't. it has a "link" for the link of the product and no images. only the id.

 

I want to use the same tpl that prints the products and because the arrays are different with different variables it breaks. 

 

 

Link to comment
Share on other sites

  • 1 year later...
  • 1 month later...

Hello and sorry for the late reply,

I will post the solutions I found below. @mantas393

What I did after alot of searching, I created a function inside the main php file of the module that I wanted to get the products the same way as prestashop returns them.

public function prepareBlocksProducts($products) {
        if ($products != false)
        {
            $products_for_template = [];
            $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_for_template = [];
            foreach ($products as $rawProduct)
            {
                $products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language);
            }

            return $products_for_template;
        }
        else
        {
            return false;
        }
    }

So I pass an array of products that I got from the category as followed:

$products = $category->getProducts($lang, 0, 100);

and used the function we created to update the way the array is returned and their link / urls etc like so:

 $products = $this->prepareBlocksProducts($products);

Let me know if you need more information,

Giannis

  • Thanks 1
Link to comment
Share on other sites

  • 4 months later...

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