5

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.

  • IMHO you should think about, if you really want to solve it this way. you're saving the product everytime someone visits the detail page. that sounds so unperformant. how will you deal with race-conditions? – Philipp Sander Jun 18 at 10:05
  • In that case I can make a cronjob that will get all the visited data and update product count at day end. – M Umair Jun 18 at 10:07
4

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.

  • Thank you for the answer – M Umair Jun 18 at 11:20
  • Applying this solution – M Umair Jun 18 at 11:24
  • It worked. Thanks – M Umair Jun 18 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.