Magento read-only and hidden product attributes

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 03:26:54

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');
}

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.

MXWest

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>

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.

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!