0

In class \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable there is the function bellow.

/**
 * Returns additional values for js config, con be overridden by descendants
 *
 * @return array
 */
protected function _getAdditionalConfig()
{
    return [];
}

How one could "rewrite" (use plugin) it since it is protected. I wanted to add additional data in this function because it is used in "getJsonConfig" function.

I guess if I'd want to make a plugin I would need to use getJsonConfig function because it is public. Which comes back to my question. Should _getAdditionalConfig() be used by end users or it's just for Magento core use ?

Edit I could use a preference for this block class.

Edit 2 Adding a preference for this block class doesn't seem to work

Edit 3 Made a plugin on \Magento\ConfigurableProduct\Block\Product\View\Type\Configurable. Function used is getJsonConfig.

1 Answer 1

0

If you do a preference, you will extend the class and can then rewrite _getAdditionalConfig(). A protected function is available to the class extending the initial class.

See my answer on my own question for a problem where I wanted to add an extra image in the JsonConfig object.

For a Plugin solution, you will indeed need to attack directly on getJsonConfig (a public function) and :

  1. json_decode the result
  2. array_merge with whatever you want to add
  3. json_encode the merged array

I guess that is what you did and told in your Edit 3

5
  • Sorry mate plugins don't extend directly the class. It won't work with protected functions too. Commented Oct 27, 2016 at 13:59
  • What do you mean by "do not extend directly", I did it and it works perfectly, I post you my code when I am in office.
    – Memes
    Commented Oct 27, 2016 at 22:51
  • See my edited answer, I might have made a mistake saying Plugin instead of module (well, I do not really understand the difference...). The fun part of the story here is that it is your question, the one we are discussing now, that helped me figure out my answer I linked above as before that, I was trying to rewrite getJsonConfig which was a bit more complicated. :)
    – Memes
    Commented Oct 28, 2016 at 1:51
  • after reading the nice answer there (magento.stackexchange.com/questions/85277/…), I understand more, I edited again so that it is correct.
    – Memes
    Commented Oct 28, 2016 at 2:00
  • I'd like to correct what I've stated. Actually the interceptor class from var/generation it extends the original class but it doesn't add plugin calls on protected methods. Commented Oct 30, 2016 at 15:26

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.