Jump to content

Problem with adding field in the product tab (Backoffice) in Prestashop 1.7


Recommended Posts

Hi, I want to add a new checkbox in the product tab that will be called product_unchecked in Prestashop 1.7.8.3.

My problem is that when I check the checkbox and hit save no error appears, but when I refresh the product tab again, the checkbox is unchecked and in the database does not change the value to 1.


I have seen several tutorials but I can't find the solution, as they are quite old.
These are the steps I have followed, I attach screenshots to see if a solution can be found.

1.- Add field in the database


image.png.7582e875e6eabde06cbab7322bef3ea4.png.43059f85bac3287397ce9f4cfbb8be46.png

 

2.- I create the file product.php in override/classes/Product.php
 

<?php

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

 

3.- I add the following lines in src/PrestaShopBundle/Form/Admin/Product/ProductInformation.php

 

->add('producto_descatalogado', FormType\CheckboxType::class, [
                'label' => $this->translator->trans('producto descatalogado', [], 'Admin.Global'),
                'required' => false,
            ])

 

4.- I add the following lines in src/PrestaShopBundle/Model/Product/AdminModelAdapter.php:

 

 private $multiShopKeys = [
    [...]
        'producto_descatalogado',
    ];

 

 private $translatableKeys = [
     [...]
        'producto_descatalogado',
    ];

 

private $unmapKeys = [
    [...]
        'producto_descatalogado',
    ];

 

 private function mapStep1FromData(Product $product)
    {
       [...]
            'producto_descatalogado' => $product->producto_descatalogado== 0 ? false : true,
            
        ];
    }

 

 5.- I add the following lines in 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', {
             [...]
              'formDescatalogado': form.step1.producto_descatalogado,
            })
          }}
        {% endblock %}

 

6. I add the following lines src/PrestaShopBundle/Resources/views/Admin/Product/ProductPage/Panels/essentials.html.twig 

 

<div class="form-group mb-4">
      <h2>Descatalogado</h2>
      {{ form_widget(formDescatalogado) }} 
    </div>

 

Attached is a screenshot of the product sheet with the checkbox
 

image.png.3c1cbc9e9fddef1482356f50ac0a3ff8.png.c5ea5227d4544dfdc007801a5468c1a8.png

That's all, thank you.
 

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