Jump to content

[1.7] how to format products like $listings


iomedia

Recommended Posts

Hi everybody,

 

I need your help. I created a wishlist module for my shop and a new page listing all products that a user has add to his wishlist.

 

What I want to do is reuse the partial template catalog/_partials/miniatures/product.tpl and for that the product list must be same as $listings.products.

 

What I'm doing now is something like this:

$products = array();
foreach ($products_id as $product_id){
$products[] = (array)new Product((int)$product_id['id_product'], true, $this->context->language->id);
}

to make the products array. how can I get products in good format?

 

Thanks for your help

 

Florian

Link to comment
Share on other sites

  • 1 month later...

Hi,

 

I found a solution looking in ps_featuredproducts. Here how I do that:

protected function getProducts()
    {

        $customer = $this->context->customer->id;

        $sql = 'SELECT p.* FROM `'._DB_PREFIX_.'wishlist` wl LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = wl.id_product WHERE id_customer = '.$customer;

        $products = Db::getInstance()->executeS($sql);

        $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;
    }

Hope it helps someone.

  • Like 2
  • Thanks 5
Link to comment
Share on other sites

  • 1 year later...

It work just fine,
 

Don't forget to write:

use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter;
use PrestaShop\PrestaShop\Adapter\Image\ImageRetriever;
use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter;
use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever;

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

As the function is part of a module, and you are writing you own module, I would said you have to rewrite it on your own.

Don't forget to put the function result in smarty.

So for now, you have the product list creation done.

For the display template part, I would do (in your module front controller ?):

$this->module->display([path to your module file], [path to your template]);

And in your template this content:

{extends file='catalog/_partials/miniatures/product.tpl'}

Link to comment
Share on other sites

  • 1 month later...
On 7/18/2017 at 5:11 PM, iomedia said:

Hi,

 

I found a solution looking in ps_featuredproducts. Here how I do that:


protected function getProducts()
    {

        $customer = $this->context->customer->id;

        $sql = 'SELECT p.* FROM `'._DB_PREFIX_.'wishlist` wl LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = wl.id_product WHERE id_customer = '.$customer;

        $products = Db::getInstance()->executeS($sql);

        $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;
    }

Hope it helps someone.

Thank you so much! It`s working!

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

Hello! I use version 1.7.8.6. Here is my code

public function getProducts()
    {
        $customer = $this->context->customer->id;
        
        //Get Last Products
        $db = Db::getInstance();
        $request = 'SELECT DISTINCT `product_id` FROM `' . _DB_PREFIX_ . 'order_detail` ORDER BY `id_order` DESC LIMIT 10 ';
        $result = $db->executeS($request);
        foreach($result as $rs){
            $products[] = new Product($rs, false, $this->context->language->id);
        }
        // echo '<pre>';print_r($products);die;

        $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 = array();
        

        foreach ($products as $rawProduct) {
            $rawProduct = (array) $rawProduct;
            $products_for_template[] = $presenter->present(
                $presentationSettings,
                $assembler->assembleProduct($rawProduct),
                $this->context->language
            );
        }
        
        return $products_for_template;
    }

I get 2 errors.

First One :

 image.png.b026b3a399803f50685a55996bc7f445.png

I turn the $rawProduct to array :

$rawProduct = (array) $rawProduct;

 Then I get this error:

image.png.448b445e61acc8ecb1f1be0249087f69.png

Anyone has any idea? Thanks in advance!! 

Edited by TonyKapa (see edit history)
Link to comment
Share on other sites

  • 3 months later...
On 6/20/2022 at 5:08 PM, TonyKapa said:

Hello! I use version 1.7.8.6. Here is my code

public function getProducts()
    {
        $customer = $this->context->customer->id;
        
        //Get Last Products
        $db = Db::getInstance();
        $request = 'SELECT DISTINCT `product_id` FROM `' . _DB_PREFIX_ . 'order_detail` ORDER BY `id_order` DESC LIMIT 10 ';
        $result = $db->executeS($request);
        foreach($result as $rs){
            $products[] = new Product($rs, false, $this->context->language->id);
        }
        // echo '<pre>';print_r($products);die;

        $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 = array();
        

        foreach ($products as $rawProduct) {
            $rawProduct = (array) $rawProduct;
            $products_for_template[] = $presenter->present(
                $presentationSettings,
                $assembler->assembleProduct($rawProduct),
                $this->context->language
            );
        }
        
        return $products_for_template;
    }

I get 2 errors.

First One :

 image.png.b026b3a399803f50685a55996bc7f445.png

I turn the $rawProduct to array :

$rawProduct = (array) $rawProduct;

 Then I get this error:

image.png.448b445e61acc8ecb1f1be0249087f69.png

Anyone has any idea? Thanks in advance!! 

@TonyKapa

Product is using id instead of id_product. On my PrestaShop v1.7.7.4 works fine.

Try to use:

$assembler->assembleProduct(['id_product' => $rawProduct['id']])

instead of:

$assembler->assembleProduct(['id_product' => $rawProduct['id_product']])

 

  • Like 1
Link to comment
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...