Jump to content

[PS 1.7] Custom product field not saving


jibayski

Recommended Posts

Hello :)

I've added a custom field in product admin page by editing core.
It shows on the form, reads the value from DB but when i hit the Save button it doesn't update.
Maybe i should add something more?

Here is the code:

In class /classes/Product.php:

/** @var string Regal */
public $regal = null;
'fields' => array(
            /* Classic fields */
            'id_shop_default' =>            array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_manufacturer' =>            array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'id_supplier' =>                array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
            'reference' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
            'supplier_reference' =>        array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
            'location' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 64),
            'regal' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isString'),
            'width' =>                        array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
            'height' =>                    array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
            'depth' =>                        array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
            'weight' =>                    array('type' => self::TYPE_FLOAT, 'validate' => 'isUnsignedFloat'),
            'quantity_discount' =>            array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'ean13' =>                        array('type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => 13),
            'isbn' =>                        array('type' => self::TYPE_STRING, 'validate' => 'isIsbn', 'size' => 32),
            'upc' =>                        array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12),
            'cache_is_pack' =>                array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'cache_has_attachments' =>        array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'is_virtual' =>                array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'state' =>                     array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
$sql->select(
            'p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, pl.`description`, pl.`description_short`, pl.`regal`, pl.`link_rewrite`, pl.`meta_description`,
			pl.`meta_keywords`, pl.`meta_title`, pl.`name`, pl.`available_now`, pl.`available_later`, image_shop.`id_image` id_image, il.`legend`, m.`name` AS manufacturer_name,
			(DATEDIFF(product_shop.`date_add`,
				DATE_SUB(
					"'.$now.'",
					INTERVAL '.$nb_days_new_product.' DAY
				)
			) > 0) as new'
        );


In /src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig:

<div class="form-group">
                              <h2>
                                  Regal
                                <span class="help-box" data-toggle="popover"
                                      data-content="Where is the product located?" ></span>
                              </h2>
                                [spam-filter] form_errors(form.step1.regal) [spam-filter]
                              <div class="row">
                                <div class="col-xl-12 col-lg-12">
                                    [spam-filter] form_widget(form.step1.regal) [spam-filter]
                                </div>
                              </div>
                            </div>

In /src/PrestaShopBundle/Form/Admin/Product/productInformation.php:

            ->add('regal', 'Symfony\Component\Form\Extension\Core\Type\TextType', array(
                'required' => false,
                'label' => 'regal'
            ))

in src/PrestaShopBundle/Model/Product/AdminModelAdapter.php:

'step1' => [
                'type_product' => $this->product->getType(),
                'inputPackItems' => [
                    'data' => array_map(
                        function ($p) {
                            return [
                                "id" => $p->id,
                                "id_product_attribute" => isset($p->id_pack_product_attribute) ? $p->id_pack_product_attribute : 0,
                                "name" => $p->name,
                                "ref" => $p->reference,
                                "quantity" => $p->pack_quantity,
                                "image" => $p->image,
                            ];
                        },
                        $this->packAdapter->getItems($this->product->id, $this->locales[0]['id_lang'])
                    )
                ],
                'name' => $this->product->name,
                'description' => $this->product->description,
                'description_short' => $this->product->description_short,
                'active' => $this->product->active == 0 ? false : true,
                'price_shortcut' => $this->product->price,
                'regal' => $this->product->regal,
                'qty_0_shortcut' => $this->product->getQuantity($this->product->id),
                'categories' => ['tree' => $this->product->getCategories()],
                'id_category_default' => $this->product->id_category_default,
                'related_products' => [
                    'data' => array_map(
                        function ($p) {
                            return($p['id_product']);
                        },
                        call_user_func_array(
                            array($this->product, "getAccessoriesLight"),
                            array($this->locales[0]['id_lang'], $this->product->id)
                        )
                    )
                ],
                'id_manufacturer' => $this->product->id_manufacturer,
                'features' => $this->getFormFeatures(),
                'images' => $this->productAdapter->getImages($this->product->id, $this->locales[0]['id_lang'])
            ],

 

Link to comment
Share on other sites

  • 4 months later...
  • 1 year later...

Hello guys, I had this same problem as you. I will leave here the only thing I needed to modify and solved for me the question of not saving the information in BD:

In the example above:

'regal' => array('type' => self::TYPE_STRING, 'validate' => 'isString'),

Change to:

'regal' => array('type' => self::TYPE_STRING, 'validate' => 'isCleanHtml', 'size' => 64),

 

I followed the same as the default reference field.

 

Link to comment
Share on other sites

a note to the first question (jibayski) because he wrote details


He didn't specify the table into which he added the new field  "regal"

It seems from the definition, that "regal" field is added into ps_product table

  'regal' =>                    array('type' => self::TYPE_STRING, 'validate' => 'isString'),

because there is missing 'lang' => true,

but from select follows, that "regal" field is stored in ps_product_lang table.

$sql->select(
            'p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, pl.`description`, pl.`description_short`,
             // pl is alias for ps_product_lang !! 
             pl.`regal`, 
             //
             pl.`link_rewrite`, pl.`meta_description`,
			 pl.`meta_keywords`, pl.`meta_title`, pl.`name`, pl.`available_now`, 
             pl.`available_later`, image_shop.`id_image` id_image, il.`legend`, m.`name` AS manufacturer_name,
			(DATEDIFF(product_shop.`date_add`,
				DATE_SUB(
					"'.$now.'",
					INTERVAL '.$nb_days_new_product.' DAY
				)
			) > 0) as new'
        );


therefore imho
a) if "regal" is a field of ps_product table then $sql->select must not contain pl.regal
b) if "regal" is a field with language mutations then should be in the  ps_product_lang table and in the definition has to be "lang" item and it is good habit to set the size of the field

  'regal' =>                    array('type' => self::TYPE_STRING,'lang' => true, 'validate' => 'isString', 'size' => 255),

 

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