Jacki98715
Jacki98715

Reputation: 5

I would like to do custom modifications in Products

I would like to do custom modification in Products. I must get information from a custom table and add to array product in product lists. I would like to have this field available in all the category etc. Does anyone know which class is responsible for such modifications?

Prestashop 1.7.4

Upvotes: 0

Views: 657

Answers (2)

Alexander Grosul
Alexander Grosul

Reputation: 1814

As for me, the best implementation is to create a module which will handle your requirements. All that you need you can achieve with hooks actionProductFormModifier(if you want to modify default part of a product form) or displayAdminProductsExtra(if it's ok to handle it with module form) to add a new field to the class and actionGetProductPropertiesAfter to modify product data array before output. After all the modifications you can just get information with {$product.your_new_field} in any template. Even though the method seems complicated it is encouraged one by Prestashop developers because they discourage classes overrides

Upvotes: 0

ggg
ggg

Reputation: 1192

The easy way is to override class "Product" and "category".

there is a lot of example on the web, so the following is not all the code source but just an explanation to show custom field on product list page.

Example with a database fields to store your data in a new table that you created: "id", "content"

Create method with an SQL query who load content by product_id and add it to the category controller who call the product_list page:

$sql = 'SELECT content FROM '._DB_PREFIX_.'new_table WHERE id='.(int)$my_content_id ;
$content = Db::getInstance()->getValue($sql);

Example override product class:

class Product extends ProductCore {

    public $my_content_id;


    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, \Context $context = null) {



        self::$definition['fields']['my_content_id'] = [
            'type' => self::TYPE_STRING,
            'required' => false, 'size' => 255
        ];

        parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }
}

To show your value in the product-listing page :

In the template product-list, call your variable :

<div class="my_content_by_product_id">{$product.my_content_by_product_id}</div>

Some inspiration :

Call new fiel in a template file:

https://www.prestashop.com/forums/topic/609377-insert-custom-field-in-product-listtpl/

Module to display data inside the product list block:

https://www.prestashop.com/forums/topic/951320-module-to-display-data-inside-the-product-list-block/

Here is an module example to add field in Products :

Add field in product Prestashop 1.7

You can also modify the product form in the administration in following this :

https://www.prestashop.com/forums/topic/606651-prestashop-17-override-of-admin-product/#comment-2549505

Upvotes: 1

Related Questions