Jump to content

Add a lot of fields in products


Recommended Posts

Hello,

I try to add lot of fields in the product and the can change regularly, so I want not set in hard the field but can add with function.

When I do it with overidde in declare all field before it works :

Firs I modify database to add fields :

$sql = [];

$sql[] = 'ALTER TABLE `' . _DB_PREFIX_ . 'product_lang`
    ADD field1 TEXT NULL,
    ADD field2 TEXT NULL,
    ADD field3 TEXT NULL';

$sql[] = 'ALTER TABLE `' . _DB_PREFIX_ . 'product`
    ADD field4 DATE NULL,
    ADD field5 DATE NULL,
    ADD field6 BOOLEAN NULL';

foreach ($sql as $query) {
    if (Db::getInstance()->execute($query) == false) {
        return false;
    }
}

I overrides the product with my new class variable and fields definitions

class Product extends ProductCore
{
	public $field1;
	public $field2;
	public $field3;
	public $field4;
	public $field5;
	public $field6;

	public function __construct(
        $id_product = null,
        $full = false,
        $id_lang = null,
        $id_shop = null,
        Context $context = null
    ) {
        self::$definition['fields']['field1'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
		self::$definition['fields']['field2'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
		self::$definition['fields']['field3'] = [
            'type' => self::TYPE_STRING,
            'lang' => true,
            'validate' => 'isString',
            'required' => false
        ];
        self::$definition['fields']['field4'] = [
            'type' => self::TYPE_DATE,
            'validate' => 'isDateOrNull',
            'required' => false
        ];
        self::$definition['fields']['field5'] = [
            'type' => self::TYPE_DATE,
            'validate' => 'isDateOrNull',
            'required' => false
        ];
        self::$definition['fields']['field6'] = [
            'type' => self::TYPE_BOOL,
            'required' => false
        ];
		parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
	}
}

So I try to add field as things progress in the code whith this function but it don't work he must declare his class variable before in product 

public function insertField($name, $lang = true, $inputType = "text", $dbType = null)
    {
        if (empty($dbType)) {
            if ($inputType == "date") {
                $dbType = 'DATE';
            } elseif ($inputType == "checkbox") {
                $dbType = 'BOOLEAN';
            } elseif ($inputType == "number") {
                $dbType = 'FLOAT';
            } else {
                $dbType = 'TEXT';
            }
        }

        Db::getInstance()->execute(
            "INSERT INTO " . _DB_PREFIX_ . "productextrafields (
                `name`, `lang`, `type`)
            VALUES('" . $name . "'," . (int)$lang . ", '" . $inputType . "')"
        );

        if ($lang) {
            Db::getInstance()->execute(
                'ALTER TABLE `' . _DB_PREFIX_ . 'product_lang`
                ADD '. $name . ' ' . $dbType . ' NULL'
            );
        } else {
            Db::getInstance()->execute(
                'ALTER TABLE `' . _DB_PREFIX_ . 'product`
                ADD '. $name . ' ' . $dbType . ' NULL'
            );
        }
        $prducts = Product::getProducts(
            Configuration::get('PS_LANG_DEFAULT'),
            1,
            1000,
            'id_product',
            'ASC'
        );
        Product::$definition['fields'][$name] = [
            'type' => Product::TYPE_STRING,
            'lang' => $lang,
            'validate' => 'isString',
            'required' => false
        ];
        foreach ($prducts as &$value) {
            $product = new Product($value['id_product']);
            object($product);
            $product->{$name} = null;
            $product->save();
        }
    }

How can I do it ?

Thank you in advance.

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

Hello,

Thank you very much for your reply, I will study this solution.

What I'm wondering is does it allow to retrieve the fields directly in the front office just by adding the field in the theme templates or do we have to change something in the Prestashop core to call via our new class?

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