Christopher
Christopher

Reputation: 590

Prestashop 1.7 accommodate overrides from multiple modules

I've created 2 custom modules module_one and module_two. these modules directly affects the ps_product_lang table, so I made overrides to the class Product.php. Now, I can install both the module, but the last of the two modules I am going to install can't be enabled due to conflict of overriding the Product.php class.

To make my modules to work, I temporarily deleted the overrides in my custom modules, then directly editing the root_file/override/classes/Product.php. But my ideal setup is to have the overrides inside the modules so I can easily update them individually. How do I do that?

here's my directory:

Here's my module_one/override/classes/Product.php code:

<?php
class Product extends ProductCore {
    public $cus_field_1;
    public $cus_field_2;

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

            self::$definition['fields']['cus_field_1'] = [
                'type' => self::TYPE_STRING,
                'lang' => true,
                'required' => false, 'size' => 255
            ];
            self::$definition['fields']['cus_field_2'] = [
                'type' => self::TYPE_STRING,
                'lang' => true,
                'required' => false, 'size' => 255
            ];
            parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }
}

Here's my module_two/override/classes/Product.php code:

<?php
class Product extends ProductCore {
    public $cus_field_3;
    public $cus_field_4;

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

            self::$definition['fields']['cus_field_3'] = [
                'type' => self::TYPE_STRING,
                'lang' => true,
                'required' => false, 'size' => 255
            ];
            self::$definition['fields']['cus_field_4'] = [
                'type' => self::TYPE_STRING,
                'lang' => true,
                'required' => false, 'size' => 255
            ];
            parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }
}

Upvotes: 0

Views: 1054

Answers (2)

MacJ
MacJ

Reputation: 91

Are you trying to install both of your modules in the same shop?

Upvotes: 0

Alexander Grosul
Alexander Grosul

Reputation: 1814

I afraid that you cannot reach this point with your approach. Prestashop doesn't encourage to use overrides within modules because of their conflicts. During overriding Prestashop simply rewrite all files in directories and cannot splice it. So after each new module installation, only the last override will be available and this is confusing. They even don't accept modules with overrides to the official marketplace. So the correct way, in my vision, would be to create modules which will not use overrides and impact the core with them. You just need to extend your products table using SQL scripts and manage them through product actions hooks.

Upvotes: 1

Related Questions