Jump to content

Custom field in Admin Product form


Recommended Posts

Hi

I am trying to add a custom field by the name "minimumprice" in product form right after the wholesale_price. I have used the following code to achieve it. But it is not working. Custom field is not displayed in the product form.

 

public function hookActionProductFormBuilderModifier(array $params) {

/** @var $formBuilder \Symfony\Component\Form */

$formBuilder = $params['form_builder']; 

$allFields = $formBuilder->all(); foreach ($allFields as $inputField => $input) {

$formBuilder->remove($inputField); }

foreach ($allFields as $inputField => $input) {

$formBuilder->add($input);

if ($inputField == 'wholesale_price') {

/** @var MoneyType::class \Symfony\Component\Form\Extension\Core\Type\MoneyType */

$formBuilder->add( 'minimumprice', MoneyType::class, ['label' => 'Minimum Price'] );

} } } 

Link to comment
Share on other sites

  • 1 month later...
  • 4 weeks later...
On 6/3/2022 at 6:47 AM, bk_developer said:

Hi

I am trying to add a custom field by the name "minimumprice" in product form right after the wholesale_price. I have used the following code to achieve it. But it is not working. Custom field is not displayed in the product form.

 

public function hookActionProductFormBuilderModifier(array $params) {

/** @var $formBuilder \Symfony\Component\Form */

$formBuilder = $params['form_builder']; 

$allFields = $formBuilder->all(); foreach ($allFields as $inputField => $input) {

$formBuilder->remove($inputField); }

foreach ($allFields as $inputField => $input) {

$formBuilder->add($input);

if ($inputField == 'wholesale_price') {

/** @var MoneyType::class \Symfony\Component\Form\Extension\Core\Type\MoneyType */

$formBuilder->add( 'minimumprice', MoneyType::class, ['label' => 'Minimum Price'] );

} } } 

 

Have you created a module and did you register the hook 'actionProductFormBuilderModifier' in your module install ?

 

 

Link to comment
Share on other sites

6 minutes ago, stifler97 said:

I wonder where is the filed going to be stored?

Original post from bk_developer does not state anything about how he saves his data.. 

Is he using a entity repository with Doctrine or just plain hacking into the database, who knows !?

 

But here's my take to tackle this : 

To add a field to product, you'll need to register actionAdminProductsControllerSaveAfter 

in your module code : 

public function hookActionAdminProductsControllerSaveAfter(&$params)
    {
        if (!isset($_REQUEST['form']['id_product']))
            return;

		// let's assume that your formbuilder is custom_product when POST is done..
        $formData = $_REQUEST['custom_product_fields']; // this is where your custom posted data are. 
        $formData['product_id'] = $_REQUEST['form']['id_product']; // this might get handy while saving.
		
		//*** Insert your custom save logic here 
		// Note : either if you modded the DB or having relation entity fields, you can use whatever technique suits you best.
		        
    }

 

To add precision on my previous post, 

As for v1.7.8.6, i'm not aware that the magic hook ActionProductFormBuilderModifier exists. 

From what i've found, hooks to display in form are

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

 

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