Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add field in product Prestashop 1.7

Why is prestashop don't save my modification into database? Using prestashop 1.7

/override/classes/Product.php

class Product extends ProductCore {
public $por_gan; public function __construct ($idProduct = null, $idLang = null, $idShop = null) {
$definition = self::$definition;
$definition['fields']['por_gan'] = array('type' => self::TYPE_INT, 'required' => false);

parent::__construct($idProduct, $idLang, $idShop); } }

In ProductInformation.php

->add('por_gan', 'Symfony\Component\Form\Extension\Core\Type\NumberType', array(
        'required' => false,
        'label' => $this->translator->trans('Beneficio', [], 'Admin.Catalog.Feature'),
        'constraints' => array(
            new Assert\NotBlank(),
            new Assert\Type(array('type' => 'numeric'))
        ),          
    ))

In form.html.twing

<div class="col-md-6">
        <label class="form-control-label">% de beneficio</label
        {{ form_widget(form.step1.por_gan) }}
</div>

Thanks

like image 261
Juan Antonio Beloso Daparte Avatar asked Dec 02 '22 13:12

Juan Antonio Beloso Daparte


1 Answers

I’ve successfully added an extra tab in admin product page. It's working fine. I think a better approach would be to create a module in order to make that modification easier to maintain.

Or you can use displayAdminProductsExtra hook, actionProductUpdate hook and actionProductAdd

The extra field is : frais_a_prevoir

I show all the files to modify but you have to check where the modification should be done inside the file (make a search and you will find)

Override /classes/Product.php

In class /classes/Product.php, there are 3 modifications to do :

1)

/** @var string Frais à prévoir */
    public $frais_a_prevoir;

2)

'frais_a_prevoir' => array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),

3)

$sql->select(
            'p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity, pl.`description`, pl.`description_short`, pl.`frais_a_prevoir`, 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

<ul class="nav nav-tabs bordered">
                      <li id="tab_description_short" class="nav-item"><a href="#description_short" data-toggle="tab" class="nav-link description-tab active">{{ 'Summary'|trans({}, 'Admin.Catalog.Feature') }}</a></li>
                      <li id="tab_description" class="nav-item"><a href="#description" data-toggle="tab" class="nav-link description-tab">{{ 'Description'|trans({}, 'Admin.Global') }}</a></li>
                      <li id="tab_frais_a_prevoir" class="nav-item"><a href="#frais_a_prevoir" data-toggle="tab" class="nav-link description-tab">{{ 'frais_a_prevoir'|trans({}, 'Admin.Global') }}</a></li>
                    </ul>

                    <div class="tab-content bordered">
                      <div class="tab-pane panel panel-default active" id="description_short">
                        {{ form_widget(form.step1.description_short) }}
                      </div>
                      <div class="tab-pane panel panel-default " id="description">
                        {{ form_widget(form.step1.description) }}
                      </div>
                      <div class="tab-pane panel panel-default " id="frais_a_prevoir">
                        {{ form_widget(form.step1.frais_a_prevoir) }}
                      </div>
                    </div>

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

->add('frais_a_prevoir', 'PrestaShopBundle\Form\Admin\Type\TranslateType', array(
                    'type' => 'Symfony\Component\Form\Extension\Core\Type\TextareaType',
                    'options' => [
                        'attr' => array('class' => 'autoload_rte'),
                        'required' => false
                    ],
                    'locales' => $this->locales,
                    'hideTabs' => true,
                    'label' => $this->translator->trans('frais_a_prevoir', [], 'Admin.Global'),
                    'required' => false
                ))

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

$this->translatableKeys = array(
            'name',
            'description',
            'description_short',
            'frais_a_prevoir',
            'link_rewrite',
            'meta_title',
            'meta_description',
            'available_now',
            'available_later',
            'tags',
        );

        //define unused key for manual binding
        $this->unmapKeys = array('name',
            'description',
            'description_short',
            'frais_a_prevoir',
            'images',
            'related_products',
            'categories',
            'suppliers',
            'display_options',
            'features',
            'specific_price',
            'virtual_product',
            'attachment_product',
        );

2)

'frais_a_prevoir' => $this->product->frais_a_prevoir,

In database, add a column frais_a_prevoir in table product_lang

like image 97
Sébastien Gicquel Avatar answered Dec 24 '22 03:12

Sébastien Gicquel