Save custom field in product - Magento 2.3.4

487 views Asked by At

I am trying to add a custom field to the product for using as a calculator, I have created a module with following

/app/code/Vendor/ProductCalculator/view/adminhtml/ui_component/product_form.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="magenest">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="label" xsi:type="string">Shrink Film Width Calculator</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="dataScope" xsi:type="string">data.magenest</item>
                <item name="sortOrder" xsi:type="number">10</item>
            </item>
        </argument>
        <field name="status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Status</item>
                    <item name="dataScope" xsi:type="string">status</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="componentType" xsi:type="string">field</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">select</item>
                    <item name="options" xsi:type="array">
                        <item name="0" xsi:type="array">
                            <item name="value" xsi:type="number">0</item>
                            <item name="label" xsi:type="string">Inactive</item>
                        </item>
                        <item name="1" xsi:type="array">
                            <item name="value" xsi:type="number">1</item>
                            <item name="label" xsi:type="string">Active</item>
                        </item>
                    </item>
                </item>
            </argument>
        </field>
        <field name="value">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Value</item>
                    <item name="dataScope" xsi:type="string">value</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                    <item name="componentType" xsi:type="string">field</item>
                    <item name="dataType" xsi:type="string">text</item>
                    <item name="formElement" xsi:type="string">input</item>
                </item>
            </argument>
        </field>
    </fieldset>
</form>

This adds the two fields status and value to the product edit form. Now I want to save the data to database. So I have created the file /app/code/Vendor/ProductCalculator/etc/adminhtml/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_save_after">
        <observer name="Vendor_ProductCalculator::save_custom_data"
                  instance="Vendor\ProductCalculator\Observer\HandleSaveProduct" />
    </event>
</config>

Then the observer is created at app/code/Vendor/ProductCalculator/Observer/HandleSaveProduct.php

<?php

namespace Vendor\ProductCalculator\Observer;

use Magento\Framework\Event\ObserverInterface;

class HandleSaveProduct implements ObserverInterface
{

    /**
     * Custom factory
     *
     * @var \Vendor\ModuleName\Model\CustomProductsFactory
     */
    protected $_CustomProductsFactory;

    /**
     * Http Request
     *
     * @var \Magento\Framework\App\Request\Http
     */
    protected $request;
    
    
    /**
     * @param \Magento\Customer\Model\ResourceModel\Group\Collection $customerGroup
     * @param \Magento\Framework\App\Request\Http $request
     * @param array $data
     */
    public function __construct(
        \Magento\Catalog\Model\ProductFactory $customProductsFactory,
        \Magento\Framework\App\Request\Http $request,
        array $data = []
    ) {
        $this->_CustomProductsFactory = $customProductsFactory;
        $this->request = $request;
    }
    
    
    /**
     *
     *  @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $observer->getProduct();
        
        if ((!empty($product))) {
            $productId = $product->getId();
            $requestId = $this->request->getParam('id');
            $productName =  $product->getName();
            $customFieldSetData = serialize($product->getMagenest());
            if ($productId) {
                $this->saveCustomProductData($requestId,$productId,$customFieldSetData);
            }
        }
    }
    

    public function saveCustomProductData($requestId,$productId,$customFieldSetData)
    {
        
        $model = $this->_CustomProductsFactory->create();
        
        if (empty($requestId)) {
            $data = ['magenest'=>$customFieldSetData,'product_id'=>$productId];
        } else {
            //load model
            $model->load($productId, 'product_id');
            $data = ['magenest'=>$customFieldSetData,'product_id'=>$productId];
        }
        
        $model->addData($data);
        
        $model->save();
    }
} 

Now, when I save the product the page is hanging. The DB gets into some deadlock and I have to restart the MySQL server. Please help

0

There are 0 answers