开发者

Magento read-only and hidden product attributes

开发者 https://www.devze.com 2023-03-13 20: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 infor

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 r开发者_如何学编程ead-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.


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.


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.

Magento read-only and hidden product attributes

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号