Domanda

I have create an attribute viewed_count, I will write a code to update it from observer.

Now I don't want anyone to edit it from admin, So I want to make it disabled from admin edit.

How can I achieve it? Please help me find solution on this.

È stato utile?

Soluzione

Attribute can be disabled on form if you add disabled attribute on textbox

You can do it by javascript.

Add app/code/Vendor/Module/view/adminhtml/layout/catalog_product_edit.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <script src="Vendor_Module::js/custom_product_edit.js"/>
    </head>
</page>

Add JS file in app/code/Vendor/Module/view/adminhtml/web/js/custom_product_edit.js

require([
    'jquery'
], function ($) {
    'use strict';
    jQuery(document).ready(function(){
        jQuery(document).ajaxStop(function () {
            jQuery('input[name=product\\[viewed_count\\]]').prop("disabled",true);
        });
    });
});

I don't know if it is a good approach or not, but this is the best solution I can think of.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top