Add field in product Prestashop 1.7

感情迁移 提交于 2019-11-26 17:11:36

问题


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


回答1:


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




回答2:


Here is an option to do this using module and does not change core files

in your MyModule.php

use PrestaShopBundle\Form\Admin\Type\TranslateType;
use PrestaShopBundle\Form\Admin\Type\FormattedTextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
public function hookDisplayAdminProductsExtra($params)
{
    $productAdapter = $this->get('prestashop.adapter.data_provider.product');
    $product = $productAdapter->getProduct($params['id_product']);

    $formData = [
       'ebay_reference' => $product->ebay_reference,
    ];
    $formFactory = $this->get('form.factory');
    $form = $formFactory->createBuilder(FormType::class, $formData)
        ->add('ebay_reference', TranslateType::class, array(
            'required' => false,
            'label' => 'Ebay reference',
            'locales' => Language::getLanguages(),
            'hideTabs' => true,
            'required' => false
        ))
    ->getForm()
    ;
    return $this->get('twig')->render(_PS_MODULE_DIR_.'MyModule/views/display-admin-products-extra.html.twig', [
        'form' => $form->createView()
    ]) ;

}
public function hookActionAdminProductsControllerSaveBefore($params)
{
    $productAdapter = $this->get('prestashop.adapter.data_provider.product');
    $product = $productAdapter->getProduct($_REQUEST['form']['id_product']);
    foreach(Language::getLanguages() as $language){
        $product->ebay_reference[ $language['id_lang'] ] = 
            $_REQUEST['form']['ebay_reference'][$language['id_lang']];
    }
    $product->save();

}

in your display-admin-products-extra.html.twig

<div class="row" >
    <div class="col-md-12">
        <div class="form-group">
            <h3>Ebay reference</h3>
            {{ form_errors(form.ebay_reference) }}
            {{ form_widget(form.ebay_reference) }}
        </div>
    </div>
</div>


来源:https://stackoverflow.com/questions/43675696/add-field-in-product-prestashop-1-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!