Jump to content

My custom Product back-office field displays and loads fine, but won't save


Recommended Posts

I've added a custom 'my_field' field to the back office of my Product, to the Quantities section. It should be a non-translateable basic integer field. This is what I did:

In the Database

ALTER TABLE ps_product ADD COLUMN my_field INT DEFAULT 0

 

In Product.php

public $my_field;
public static $definition = array(
        'table' => 'product',
        'primary' => 'id_product',
        'multilang' => true,
        'multilang_shop' => true,
        'fields' => array(
            /* Classic fields */
			...
            'my_field' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
			...
public function loadStockData() {
	...
	$this->my_field = Db::getInstance()->getValue('SELECT `my_field` FROM `ps_product` WHERE id_product = ' . $this->id);
	...
}

 

In \src\PrestaShopBundle\Form\Admin\Product\ProductQuantity.php (to the appropriate place)

->add(
  'my_field',
  FormType\NumberType::class,
  [
    'label' => $this->translator->trans('My custom field', [], 'Admin.Catalog.Feature'),
    'constraints' => [
      new Assert\NotBlank(),
      new Assert\Type(['type' => 'numeric']),
  	],
  ]
)

 

In AdminModelAdapter.php

private $multiShopKeys = array(
	...
	'my_field', // Not sure if this is necessary
	...
);

...

private $unmapKeys = array(
	...
	'my_field',
	...
);

...

private function mapStep3FormData(Product $product)
{
  return array(
    ...
    'my_field' => $product->my_field,
    ...
  );
}

 

Lastly I've added my field to the product and combinations twig templates.

Now the field displays just fine, exactly when I want it to be. It nicely fetches the value from the database (0 by default, or different number if I change it manually in the db), but the field won't save. I tried performing an update query in Products.php's update() method like this:

public function update($null_values = false)
{
  ...

  // Update my custom field column
  Db::getInstance()->update('product', array(
  	'my_field' => $this->my_field,
  ), 'id_product = ' . (int) $this->id);

  ...
}

But it does not work, since $this->my_field seems to be always zero.

What is the correct way to save my value? Thank you.

Edited by TinoArts (see edit history)
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...