12

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 ?

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

1 Answer 1

2

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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.