Magento read-only and hidden product attributes
Asked Answered
C

7

15

I would like to have some Magento product attributes that are not editable from the admin interface and some that are not visible at all in that interface (as a method of storing some persistent information about a product that should not be viewed by human users.. it's the only way of doing this that i can think of, any other suggestions are welcome).

So my question is: Do all Magento attributes have to be visible and editable from the admin interface? If not, how can they be made read-only or hidden?

I noticed that in the admin interface there are some read-only fields, so it must be possible to do this one way or another. After searching stackoverflow for this I found a possible solution involving JavaScript, but I would like to not go down that path if it's at all possible.

Contemporize answered 17/6, 2011 at 9:55 Comment(0)
C
16

OK, it looks like it can be done after all. After adding an observer for the catalog_product_load_after event, the lockAttribute method of the Mage_Catalog_Model_Abstract class may be used to make a product attribute read-only. Here is the code for the observer method:

public function lockAttributes($observer) {
    $event = $observer->getEvent();
    $product = $event->getProduct();
    $product->lockAttribute('attribute_code');
}
Contemporize answered 22/6, 2011 at 9:46 Comment(0)
R
13

Since the catalog_product_load_after event is dispatched for every product load, the attributes supplied in the lock_attributes method are locked after every product load. This could have unexpected results: it is not possible to change the value of the attributes in the lock_attributes method without explicitly unlocking them.

Instead of using the catalog_product_load_after event, it suffices to add an observer for the catalog_product_edit_action event: this event is dispatched only when editing a product in the admin interface.

Rodgers answered 24/10, 2011 at 10:36 Comment(0)
C
2

I think Aad Mathijssen and Epicurus combined have the best answer to the question, with a little clarification. As Aad points out, catalog_product_load_after is called after every product load and that means on the FrontEnd as well!

If we are looking to protect attribute fields only in the admin panels, catalog_product_edit_action is the more appropriate choice.

Your etc/config.xml will then be something like this:

<catalog_product_edit_action>
  <observers>
    <lock_attributes>
      <class>yourmodule/observers</class>
      <method>lockAttributes</method>
    </lock_attributes>
  </observers>
</catalog_product_edit_action>
Cento answered 6/5, 2014 at 15:52 Comment(0)
L
1

No i guess its not possible from the attribute manager. A easy quick and dirty solution would be to use css to hide the input and label.

Levanter answered 17/6, 2011 at 15:8 Comment(0)
A
1

I have developed exactly such extension that works for products, categories and CMS pages. You just have to define some rules and choose which attributes you want to show as read-only.

Magento Admin Read-Only Product Rules

Extension URL: https://www.bubbleshop.net/magento-admin-readonly.html

Airline answered 21/8, 2015 at 9:55 Comment(0)
E
0

Using this thread and some more digging around; the lockAttribute method is from an abstract class which means that it's possible to also be used on category attributes as well. I caught the 'catalog_category_load_after' observer and used it to lock my desired category attributes:

public function lockCategoryAttributes($observer) {
    $event = $observer->getEvent();
    $c = $event->getCategory();
    $c->lockAttribute('attribute_code');
}

I'm not sure if that's the right observer to use but it works.

So yes it is possible to lock category attributes or make them readonly.

Emlin answered 1/8, 2012 at 16:9 Comment(0)
B
0

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_load_after">
      <observer name="product_lock_attributes" instance="Vendor\Module\Observer\Lock"/>
    </event>
</config>

Observer\Lock.php

namespace Vendor\Module\Observer;
 
class Lock implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $event = $observer->getEvent();
        $product = $event->getProduct();
        $product->lockAttribute('attribute_code');
     }
}
Brassware answered 21/8, 2020 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.