Get support for PululuK/democustomfields17

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

PululuK/democustomfields17

democustomfields17

This module is an architectural skeleton for module developers, who want to add custom fields in the product page on the backoffice.

It's a base, you have to adapt it to your needs ... This will save you a lot of time!

Current produt hooks

  • displayAdminProductsExtra
  • displayAdminProductsMainStepLeftColumnMiddle
  • displayAdminProductsMainStepLeftColumnBottom
  • displayAdminProductsMainStepRightColumnBottom
  • displayAdminProductsQuantitiesStepBottom
  • displayAdminProductsPriceStepBottom
  • displayAdminProductsOptionsStepTop
  • displayAdminProductsOptionsStepBottom
  • displayAdminProductsSeoStepBottom

Requirements

Install

How to use ?

Imagine you want to add a new field in backoffice product page in extra tab.

NOTE : The hook fields builder must implement HookFieldsBuilderInterface and respect this name rule Hook<HookName>FieldsBuilder

See Types references here : https://devdocs.prestashop.com/1.7/development/components/form/types-reference/

<?php
        
    namespace PrestaShop\Module\Democustomfields17\Form\Product\Hooks;

    use PrestaShop\Module\Democustomfields17\Form\Product\Hooks\HookFieldsBuilderInterface;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\Extension\Core\Type\TextType;
    use Symfony\Component\Form\Extension\Core\Type\TextareaType;
    use PrestaShopBundle\Form\Admin\Type\TranslatableType;
    use PrestaShopBundle\Form\Admin\Type\SwitchType;
    use Module;

    class HookDisplayAdminProductsExtraFieldsBuilder implements HookFieldsBuilderInterface
    {
        public function addFields(FormBuilderInterface $adminFormBuilder, Module $module) :FormBuilderInterface
        {
            $adminFormBuilder
                ->add('my_text_field_example', TextType::class, [
                    'label' => $module->l('My text'),
                    'attr' => [
                        'class' => 'my-custom-class',
                        'data-hex'=> 'true'
                    ]
                ])
                ->add('my_switch_field_example', SwitchType::class, [
                    'label' => $module->l('My switch')
                ])
                ->add('my_translatable_text_field_example', TranslatableType::class, [
                    'label' => $module->l('My translatable text'),
                    'type' => TextareaType::class,
                    'locales' => $module->getLocales()
                ]);

            return $adminFormBuilder;
        }
    }

image

  • 3 : Process data after product form submit

See src/Form/Product/ProductFormDataHandler.php

NOTE : Make sure your fields exists in ProductCustomFields Model and you database table schema


<?php

namespace PrestaShop\Module\Democustomfields17\Form\Product;

use PrestaShop\Module\Democustomfields17\Form\FormDataHandlerInterface;
use PrestaShop\PrestaShop\Core\Module\Exception\ModuleErrorException;
use PrestaShop\Module\Democustomfields17\Factory\ProductCustomFieldsFactory;
use Exception;

final class ProductFormDataHandler implements FormDataHandlerInterface
{
    public function save(array $data): bool{

        $idProduct = (int) $data['id_product'];
        $productCustomFields = ProductCustomFieldsFactory::create($idProduct);
        $productCustomFields->id_product = $idProduct;
        $productCustomFields->my_switch_field_example = (bool) $data['my_switch_field_example'];
        $productCustomFields->my_translatable_text_field_example = $data['my_translatable_text_field_example'];
        $productCustomFields->my_text_field_example = $data['my_text_field_example'];

        try {
            if($productCustomFields->save()){
                return true;
            }
        } catch(Exception $e){
            throw new ModuleErrorException($e->getMessage());
        }

        return true;
    }

    public function getData(array $params): array{
        $productCustomFields = ProductCustomFieldsFactory::create(
            (int)$params['id_product'],
            $params['id_lang'] ?? null,
            $params['id_shop'] ?? null
        );

        return [
            'id' => $productCustomFields->id,
            'id_product' => $productCustomFields->id_product,
            'my_switch_field_example' => (bool) $productCustomFields->my_switch_field_example,
            'my_text_field_example' => $productCustomFields->my_text_field_example,
            'my_translatable_text_field_example' => $productCustomFields->my_translatable_text_field_example,
        ];
    }
}

  • 4 : Acces your data in Front

{$product.democustomfields17.YOUR_FIELD_NAME_HERE}

Ex.:

{$product.democustomfields17.my_text_field_example}
{$product.democustomfields17.my_translatable_text_field_example }
{$product.democustomfields17.my_switch_field_example}
Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2