Alien
Alien

Reputation: 11

Prestashop 1.7 custom field in admin product page - Saving issue

I've added a custom field into Prestashop 1.7 product I'm missing something because I can't save the value of the new field(checkbox)

What did till now:

  1. I've add new field to table ps_product

hide_discount_table - tinyint(1) - NOT NULL

  1. override/classes/Product.php
    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
        {
            self::$definition['fields']['hide_discount_table'] = array('type' => self::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool');
        }
  1. src/PrestaShopBundle/Form/Admin/Product/ProductInformation.php
    // LINE 238
    ->add('hidden_discount_table', FormType\CheckboxType::class, [
        'label' => $this->translator->trans('Hide discount table', [], 'Admin.Global'),
        'required' => false,
    ])
  1. src/PrestaShopBundle/Model/Product/AdminModelAdapter.php:
    private $translatableKeys = array(
        //...LINE 140
        'hidden_discount_table',
    );
    private $unmapKeys = array(
        //... LINE 161
        'hidden_discount_table',
    );
    private function mapStep1FromData(Product $product)
    {   
        return array(
            //... LINE 518
            'hide_discount_table' => $product->hide_discount_table== 0 ? false : true,
        );
    }
  1. src/PrestaShopBundle/Resources/views/Admin/Product/ProductPage/product.html.twig:
    {# PANEL ESSENTIALS #}
            {% block product_panel_essentials %}
              {% set formQuantityShortcut = form.step1.qty_0_shortcut is defined ? form.step1.qty_0_shortcut : null  %}
              {{ include('@Product/ProductPage/Panels/essentials.html.twig', {
                  'formPackItems': form.step1.inputPackItems,
                  'productId': id_product,
                  'images': form.step1.vars.value.images,
                  'formShortDescription': form.step1.description_short,
                  'formDescription': form.step1.description,
                  'formManufacturer': form.step1.id_manufacturer,
                  'formHiddenDiscount': form.step1.hide_discount_table, // NEW ENTERY
                  'formFeatures': form.step1.features,
                  'formManufacturer': form.step1.id_manufacturer,
                  'formRelatedProducts': form.step1.related_products,
                  'is_combination_active': is_combination_active,
                  'has_combinations': has_combinations,
                  'formReference': form.step6.reference,
                  'formQuantityShortcut': formQuantityShortcut,
                  'formPriceShortcut': form.step1.price_shortcut,
                  'formPriceShortcutTTC': form.step1.price_ttc_shortcut,
                  'formCategories': form.step1,
                })
              }}
            {% endblock %}
  1. src/PrestaShopBundle/Resources/views/Admin/Product/ProductPage/Panels/essentials.html.twig (in right column)
    <div class="form-group mb-4">
      <h2>Additional features</h2>
      {{ form_widget(formHiddenDiscount) }} 
    </div>

On front-end, I can print the value from DB in product.tpl

    {$product->hide_discount_table}

That works fine.

What doesn't work:

Upvotes: 1

Views: 1474

Answers (2)

Med Baalla
Med Baalla

Reputation: 1

You should to add your field in ps_product_lang table not ps_product

Upvotes: -1

Fran Cerezo
Fran Cerezo

Reputation: 948

Try in override/classes/Product.php

public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
{
    parent::$definition['fields']['hide_discount_table'] = array('type' => 
    parent::TYPE_BOOL, 'shop' => true, 'validate' => 'isBool');
    parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
}

And delete file /var/cache/{your_environment}/class_index.php

Upvotes: 0

Related Questions