Jump to content

Prestashop 1.7 : override of Admin Product


jeremiezip

Recommended Posts

Hello everybody, 

 

I'have recently installed PS 1.7 for a project. 

 

I discover the framework Symfony, and i would like to know the best way for override the Admin Product.  (for example: add a field in the product information) 

I changed these informations directly in the bundle Prestashop, but it is not clean.

 

It would be interesting to talk about it, all together !  :)

 

Thanks for your contribution,

 

J.

I

Link to comment
Share on other sites

Hello phinq, thank you for you reply.

 

Yes i know these hook, very usefull when you creating Module.

 

But i have the case of a customer who specifically wants to add fields in the main info of product BackOffice (not in a hook). Do you have any idea? 

But I have the case of a client who specifically wants to add fields in the main info of the product (back office
 

 

But I have the case of a client who specifically wants to add fields in the main info of the product (back office
 
 
But I have the case of a client who specifically wants to add fields in the main info of the product (back office
 
ut I have the case of a client who specifically wants to add fields in the main info of the product (back office


 
But I have the case of a client who specifically wants to add fields in the main info of the product (back office
 
Link to comment
Share on other sites

Hi jeremiezip,

 

I have the same issue, and I need to add new fields in Combinations too. I have read the documentation but there is no too much about it, except that Product works with Symfony and currently the only way (that I know) to add new fiels is through modules and hooks.

 

Maybe in next updates we will have other ways to do that, but for now, it is neccessary modify prestashop bundle if you want to make it easy for employee's experience.

 

I am trying to override some prestashopbundle controllers and templates through Symfony but I am not lucky for the moment.

Have you encountered any better solution?

Link to comment
Share on other sites

yes, this is my code, it works but it's not a good way because if you update Prestashop, you'll loose all your changes. This is the case of a customer who wants absolutely a textarea field in the product information tpl (not in a hook) : 

 

first in database in the table ps_product_lang add your field "my_extra_field_description" type TEXT

add an override of class product in override/classes , with the new field  (for this case, it doesn't work for me, i adding the field directly in the class, not in a override)... but this is an example
 

class Product extends ProductCore
{
    public $my_extra_field_description;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
      Product::$definition['fields']['my_extra_field_description'] = array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString');
      parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }


}

(don't forget to remove the cache)

 

src/PrestaShopBundle/Form/Admin/Product/ProductInformation.php

        ->add('my_extra_field_description', '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('my_extra_field_description', [], 'Admin.Global'),
            'required' => false
        ))

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

 

    //define translatable key
        $this->translatableKeys = array(
            'name',
            'description',
            'description_short',
            'my_extra_field_description',
            'link_rewrite',
            'meta_title',
            'meta_description',
            'available_now',
            'available_later',
            'tags',
        );

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

and below in the function getFormData() add field :

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

   

 

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">[spam-filter] 'Summary'|trans({}, 'Admin.Catalog.Feature') [spam-filter]</a></li>
                      <li id="tab_description" class="nav-item"><a href="#description" data-toggle="tab" class="nav-link description-tab">[spam-filter] 'Description vin'|trans({}, 'Admin.Global') [spam-filter]</a></li>
                        <li id="tab_my_extra_field_description" class="nav-item"><a href="#la_presse_en_parle" data-toggle="tab" class="nav-link description-tab">[spam-filter] 'My extra field description'|trans({}, 'Admin.Global') [spam-filter]</a></li>
                    </ul>

                    <div class="tab-content bordered">
                      <div class="tab-pane panel panel-default active" id="description_short">
                        [spam-filter] form_widget(form.step1.description_short) [spam-filter]
                      </div>
                      <div class="tab-pane panel panel-default " id="description">
                        [spam-filter] form_widget(form.step1.description) [spam-filter]
                      </div>
                      <div class="tab-pane panel panel-default " id="my_extra_field_description">
                        [spam-filter] form_widget(form.step1.my_extra_field_description) [spam-filter]
                      </div>
                    </div><br/>

So, if somebody know how to override a bundle ... thanks 

Edited by jeremiezip (see edit history)
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

Thanks for these precious indications. I try to change the field for a simple text field rather than HTML tab.

Everything works when i use form.step1 HTML field,  if i change for from.html.twig for form.step6 (text field) or any for other type i got errors, does anyone know what i am missing?

                            <h2>
                              [spam-filter] "Short name"|trans({}, 'Admin.Catalog.Feature') [spam-filter]
                              <span class="help-box" data-toggle="popover"
                                data-content="[spam-filter] "Add short name. Allowed special characters: .-_#\."|trans({}, 'Admin.Catalog.Help') [spam-filter]" ></span>
                            </h2>                              
                             [spam-filter] form_errors(form.step6.short_name) [spam-filter]
                            <div class="row">
                              <div class="col-xl-12 col-lg-12" id="short_name">
                                  [spam-filter] form_widget(form.step6.short_name) [spam-filter]
                              </div>
                            </div>  

Neither the property "short_name" nor one of the methods "short_name()", "getshort_name()"/"isshort_name()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in "/home/www/ps2/src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig" at line 310.

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

yes, this is my code, it works but it's not a good way because if you update Prestashop, you'll loose all your changes. This is the case of a customer who wants absolutely a textarea field in the product information tpl (not in a hook) : 

 

first in database in the table ps_product_lang add your field "my_extra_field_description" type TEXT

add an override of class product in override/classes , with the new field  (for this case, it doesn't work for me, i adding the field directly in the class, not in a override)... but this is an example

 

class Product extends ProductCore
{
    public $my_extra_field_description;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)
    {
      Product::$definition['fields']['my_extra_field_description'] = array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isString');
      parent::__construct($id_product, $full, $id_lang, $id_shop, $context);
    }


}

(don't forget to remove the cache)

 

 

src/PrestaShopBundle/Form/Admin/Product/ProductInformation.php

        ->add('my_extra_field_description', '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('my_extra_field_description', [], 'Admin.Global'),
            'required' => false
        ))

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

 

 

    //define translatable key
        $this->translatableKeys = array(
            'name',
            'description',
            'description_short',
            'my_extra_field_description',
            'link_rewrite',
            'meta_title',
            'meta_description',
            'available_now',
            'available_later',
            'tags',
        );

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

    

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">[spam-filter] 'Summary'|trans({}, 'Admin.Catalog.Feature') [spam-filter]</a></li>
                      <li id="tab_description" class="nav-item"><a href="#description" data-toggle="tab" class="nav-link description-tab">[spam-filter] 'Description vin'|trans({}, 'Admin.Global') [spam-filter]</a></li>
                        <li id="tab_my_extra_field_description" class="nav-item"><a href="#la_presse_en_parle" data-toggle="tab" class="nav-link description-tab">[spam-filter] 'My extra field description'|trans({}, 'Admin.Global') [spam-filter]</a></li>
                    </ul>

                    <div class="tab-content bordered">
                      <div class="tab-pane panel panel-default active" id="description_short">
                        [spam-filter] form_widget(form.step1.description_short) [spam-filter]
                      </div>
                      <div class="tab-pane panel panel-default " id="description">
                        [spam-filter] form_widget(form.step1.description) [spam-filter]
                      </div>
                      <div class="tab-pane panel panel-default " id="my_extra_field_description">
                        [spam-filter] form_widget(form.step1.my_extra_field_description) [spam-filter]
                      </div>
                    </div><br/>

So, if somebody know how to override a bundle ... thanks 

 

 

Hi Jeremiezip,

 

Thank you for your comment, it was very useful.

 

First of all, I tried overriding product class and it worked. I declare a new variable and it works properly.

But when I try to visualize in the backend the new field I got some issues: I add the declared variable in the overriden file in the getFormData() method from the AdminModelAdapter.php file and I obtain it completely empty. It seems like the variable wasn’t declared. However, if I declare it in the product class it finally works. May it be because AdminModelAdapter.php file just loads parent class? 

 

File  src/PrestaShopBundle/Model/Product/AdminModelAdapter.php  (onlye declare prent class)

public function __construct(

        \ProductCore $product,

         ....

 

File override\classes\Product.php

class Product extends ProductCore

{

    public $tablas_nutricionales;

    public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)

    {

        parent::$definition['fields']['tablas_nutricionales'] =  array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml');

        parent::__construct($id_product, $full, $id_lang, $id_shop);

    }

}

 

In another hand, it would be really useful to override a budle for not losing information when updating the shop. Did you get how to do it?

 

Thank you.

 

Best regards,

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

Of course Lucesmo, I have exactly the same result of you.

Pressed for time, i finally modify the product class directly. (symfony doesn't takes the override)

But it's a very bad practice... 

 

I don't know if it is a new philosophy of prestashop for encourages the usage of AdminProductHook... But I think it is necessary to have an explanation in the official documentation of Presta'. 

I've send a message to the Prestashop Team.

 

developer
 

Be on, if i solve the problem i forward the code here.

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

Thanks for these precious indications. I try to change the field for a simple text field rather than HTML tab.

Everything works when i use form.step1 HTML field,  if i change for from.html.twig for form.step6 (text field) or any for other type i got errors, does anyone know what i am missing?

                            <h2>
                              [spam-filter] "Short name"|trans({}, 'Admin.Catalog.Feature') [spam-filter]
                              <span class="help-box" data-toggle="popover"
                                data-content="[spam-filter] "Add short name. Allowed special characters: .-_#\."|trans({}, 'Admin.Catalog.Help') [spam-filter]" ></span>
                            </h2>                              
                             [spam-filter] form_errors(form.step6.short_name) [spam-filter]
                            <div class="row">
                              <div class="col-xl-12 col-lg-12" id="short_name">
                                  [spam-filter] form_widget(form.step6.short_name) [spam-filter]
                              </div>
                            </div>  

Neither the property "short_name" nor one of the methods "short_name()", "getshort_name()"/"isshort_name()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in "/home/www/ps2/src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig" at line 310.

 

hello, I edited my post because I realized that I had forgotten a part... sorry. don't forget to add field in your product class.

Link to comment
Share on other sites

  • 2 months later...

Hello,

 

Does anybody have any idea on how to edit the Product Option page of the product?

I edited:

in src/PrestaShopBundle/Form/Admin/Product/ProductOptions.php

I added after the ean13 add: 

        ->add('ean13_current_shop', 'Symfony\Component\Form\Extension\Core\Type\TextType', array(
            'required' => false,
            'error_bubbling' => true,
            'label' => $this->translator->trans('EAN-13 or JAN barcodez', [], 'Admin.Catalog.Feature'),
            'constraints' => array(
                new Assert\Regex("/^[0-9]{0,13}$/"),
            )
        ))
in src/PrestaShopBundle/Model/Product/AdminModelAdapter.php:
Also after ean13:
                'ean13_current_shop' => $this->product->ean13_current_shop,
in src/PrestaShopBundle/Resources/views/Admin/Product/form.html.twig
Also after ean13 field:
                     <fieldset class="col-md-4 form-group">
                        <label class="form-control-label">
                          [spam-filter] form.step6.ean13_current_shop.vars.label [spam-filter]
                          <span class="help-box" data-toggle="popover"
                            data-content="[spam-filter] "This type of product code is specific to Europe and Japan, but is widely used internationally. It is a superset of the UPC code: all products marked with an EAN will be accepted in North America."|trans({}, 'Admin.Catalog.Help') [spam-filter]" ></span>
                        </label>
                        [spam-filter] form_errors(form.step6.ean13_current_shop) [spam-filter]
                        [spam-filter] form_widget(form.step6.ean13_current_shop) [spam-filter]
                      </fieldset>

And also edited the Product.php class directly. And i added:

/** @var string Ean-13 barcode */
    public $ean13_current_shop;

and

'ean13_current_shop' =>                        array('type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => 13),

And still it does not show on the front office. And as a test i deleted the file form.html.twig and nothing disappeared from the Product Options tab of the product.

Any idea?

Using prestashop 1.7.2.0

Thanks

Link to comment
Share on other sites

Have you refresh the cache of the website? 

 

Otherwise, i have some information about our initial problem. I discussed with a Prestashop Teacher. He told me that it is no longer possible to override the admin product. It is done to encourage the exclusive usage of hooks.

 

I don't know if it's a fake news or not, but I think we can believe it.

Link to comment
Share on other sites

Hello, 

 

I have the website in maintenance mode.

And i have force recompile and no cache. also checked the smarty folder and nothing to delete.

 

Edit:

So now it works, i think something was blocked. I tried it from different browsers and it did not work. But after i changed the original ean13 to what i wanted and back it started working. So the code is just fine.

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

  • 5 months later...
  • 11 months later...

Hello,

I'm searching to override PrestaShopBundle\Model\Product\AdminModelAdapter class into module or override folder to don't be affected by a Prestashop update. How can I do that?

I tried some solutions but no one seems works :

namespace MyBundle\Model\Product;
class AdminModelAdapter extends PrestaShopBundle\Model\Product\AdminModelAdapter
class AdminModelAdapterOverride extends PrestaShopBundle\Model\Product\AdminModelAdapter
namespace PrestaShopBundle\Model\Product;
class AdminModelAdapterOverride extends AdminModelAdapter
Link to comment
Share on other sites

  • 1 year later...
  • 6 months later...
  • 3 weeks later...
23 hours ago, Guest said:

@karpoj23

my_field = your field name

In module:

hookActionProductUpdate ()

{

$id_product = Tools::getValue('id_product');

$my_field = Tools::getValue('my_field');

Db::getInstance()->execute("UPDATE "._DB_PREFIX_."product SET my_field = '".$my_field."' WHERE id_product = ".$id_product);

}

Thanks!!

Link to comment
Share on other sites

Hi again,

i have with the same problem when I try to save or add a new product, I change debug mode to true and I only receive Error 500, but Validator it is ok and I do not display the error.

 

I have compared with another domain with the same version and I see this:

 

domain fail:

only launch 1 hook - moduleRoutes

domain good:

launch 4 hooks - moduleRoutes, actionClearCache, actionProductSave, actionProductUpdate

Edited by karpoj23 (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...