In this blog, we will learn how to save the custom form field and display the updated value on the Product Page using the PrestaShop hook. So, we will define PrestaShop’s given hook.
I hope you already know how to add a custom form field on the PrestaShop product page using hook. If are new to this please read our previous blog. Please click here.
Let’s continue to our topic,
Step 1: Register the PrestaShop’s given hook for product save i.e. “actionProductSave” in the install function as follows
public function install() { return parent::install() && $this->registerHook(['actionProductFormBuilderModifier']) && $this->registerHook(['actionProductSave']); }
Step 2: Define hook and perform save operations for particular products.
/** * Modify product form builder * * @param array $params */ public function hookActionProductFormBuilderModifier(array $params): void { /** @var ProductFormModifier $productFormModifier */ $productFormModifier = $this->get(ProductFormModifier::class); $productId = isset($params['id']) ? new ProductId((int) $params['id']) : null; $productFormModifier->modify($productId, $params['form_builder']); } /** * Modify product form builder * * @param array $params */ public function hookActionProductSave(array $params): void { // Please write your logic and operation and save the data as per your need // We are using configuration table to save the data $productData = Tools::getValue('product'); $demoPrice = $productData['pricing']['demo_module_pricing_field']; $demoSEOText = $productData['seo']['demo_module_custom_field']; $idWkProduct = $params['id_product']; Configuration::updateValue('wk_price_data_' . $idWkProduct, $demoPrice); Configuration::updateValue('wk_seo_data_' . $idWkProduct, $demoSEOText); }
To save the data of Products you can use your own defined table or you can act using input data like send the email or anything else. We have saved the data in the configuration table for example only.
Step 3: You can display those saved values by setting the values in the modify() function of the ProductFormModifier class.
<module-name>/src/Form/Modifier/ProductFormModifier.php
/** * @param ProductId|null $productId * @param FormBuilderInterface $productFormBuilder */ public function modify( ?ProductId $productId, FormBuilderInterface $productFormBuilder ): void { $idWkProduct = $productId->getValue(); $data['seo'] = Configuration::get('wk_seo_data_' . $idWkProduct); $data['price'] = Configuration::get('wk_price_data_' . $idWkProduct); $this->modifyDescriptionTab($data, $productFormBuilder); }
You will get the result as follows after save the custom form field.
That’s all.
If you are facing any issues or doubts in the above process, please feel free to contact us in the comment section.
I would be happy to help.
Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.
Be the first to comment.