11

Plugin Interceptors and overriding with preference does not work on static methods.

Need to override getOptionArray method in Magento\Catalog\Model\Product\Visibility to return a custom visibility

/**
     * Retrieve option array
     *
     * @return array
     */
    public static function getOptionArray()
    {
        return [
            self::VISIBILITY_NOT_VISIBLE => __('Not Visible Individually'),
            self::VISIBILITY_IN_CATALOG => __('Catalog'),
            self::VISIBILITY_IN_SEARCH => __('Search'),
            self::VISIBILITY_BOTH => __('Catalog, Search')
        ];
    }

Any other way this can be achieved ?

  • No. I'm asking how to add a new visibility option. getOption array being a static method cannot be modified with interceptors or preference. – user3339988 Nov 30 '17 at 4:52
  • Sorry, I marked wrong question. This one should help you magento.stackexchange.com/questions/134386/… – PY Yick Nov 30 '17 at 4:54
  • That one doesn't answer my question. – user3339988 Nov 30 '17 at 4:55
  • Anyone? i've been stuck with this for 2-3 days. Any alternative way to add visibility option ? – user3339988 Nov 30 '17 at 6:17
1

Overriding only getOptionArray() is not just enough because if you see in Magento\Catalog\Model\Product\Visibility at line 107, 120 & 134, getOptionArray() is called with self.Self will always call the original method rather than new one, hence you simply need to copy and paste getAllOption(), getAllOptions() & getOptionText() in your overridden Model Class (Same where you have overriden getOptionArray())

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.