Magento read-only and hidden product attributes

后端 未结 7 1330
迷失自我
迷失自我 2020-12-16 14:10

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 st

相关标签:
7条回答
  • 2020-12-16 14:48

    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.

    0 讨论(0)
  • 2020-12-16 14:52

    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.

    0 讨论(0)
  • 2020-12-16 14:59

    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 讨论(0)
  • 2020-12-16 15:06

    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>
    
    0 讨论(0)
  • 2020-12-16 15:07

    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');
    }
    
    0 讨论(0)
  • 2020-12-16 15:07

    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.

    0 讨论(0)
提交回复
热议问题