简体   繁体   English

如何使用模块和挂钩将自定义产品字段添加到prestashop

[英]How to add custom product fields to prestashop using a module and hooks

I need to add some fields to prestashop product (HSN code and one more). 我需要在prestashop产品中添加一些字段(HSN代码和另外一个)。 I am very new to prestashop and there is no guide to do the same with latest build 1.7. 我是prestashop的新手,并没有指导与最新版本1.7做同样的事情。

I have followed answers made on stackoverflow and I am able to show the form fields but unable to save and validate the value. 我已经跟踪了stackoverflow上的答案,我能够显示表单字段但无法保存并验证该值。 Here is the code snippet I have used (I preferred this because it uses the hooks). 这是我使用的代码片段(我更喜欢这个,因为它使用了钩子)。

    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();

    }

I am stucked at data saving part. 我被困在数据保存部分。 Need some guidance to it in recommended way. 需要以推荐的方式对其进行一些指导。 Also need the suggestion to read the code of any module bundled with prestashop to help in this. 还需要建议读取与prestashop捆绑在一起的任何模块的代码来帮助解决这个问题。

Add field in product Prestashop 1.7 在产品Prestashop 1.7中添加字段

This part of the code just describes how to create a form with necessary fields but it doesn't handle product class extending. 这部分代码仅描述了如何创建具有必要字段的表单,但它不处理产品类扩展。 So if you would have this attribute(ebay_reference) with all relations in your product class everything would work. 因此,如果您的产品类中包含所有关系的属性(ebay_reference),一切都会有效。 So I suppose that you need to implement steps for /classes/Product.php and for src/PrestaShopBundle/Model/Product/AdminModelAdapter.php from the original answer of here Add field in product Prestashop 1.7 and add necessary field to the DB. 所以我想你需要为产品Prestashop 1.7中的原始答案添加字段中的 /classes/Product.phpsrc/PrestaShopBundle/Model/Product/AdminModelAdapter.php实现步骤, /classes/Product.php DB添加必要的字段。

Also, if you don't want to modify or override default product class, you can create own table(s) to keep your data there like with id_product key, but it could be more difficult to propagate that data to all product instances in the store. 此外,如果您不想修改或覆盖默认产品类,则可以使用id_product键创建自己的表以保存数据,但将数据传播到所有产品实例可能更加困难。商店。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM