Question

i want do this How do I change the default value of the "position" category attribute? But i noob in magento, i try search in google how do it, but in all guides standart models stay in Mage folder, but my model in vendor

mag.ru\vendor\magento\module-catalog\Model\ResourceModel\Product.php

need rewrite this method _saveCategories

what i need write into config.xml and what name in MyModule folder i need create?

P.S. sry for bad eng

Was it helpful?

Solution

I think you have two options. Plugins (http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html) are not suitable as this is a protected method, so it ain't public.

The two options left:

  • Using a replacement strategy (replaces everywhere)
  • Inject a different class (replaces only in that specific point)

Replacement example

Set a preference in the di.xml:

<preference for="Magento\Catalog\Model\ResourceModel\Product"
type="Your\Module\Catalog\Model\ResourceModel\Product" />

And create a class like this:

namespace Your\Module\Catalog\Model\ResourceModel;

class Product extends Magento\Catalog\Model\ResourceModel\Product 
{
    protected function _saveCategories(\Magento\Framework\DataObject $object)
    {
        ....
    }
}

A tutorial from Alan Storm on this subject: http://alanstorm.com/magento_2_object_manager_preferences.

Different class injected

Create a class like above, but only inject it in the place where you need it adjusted. So you figure out in which class this class is injected that you want adjusted, and make a di.xml replacement. I think this is a good tutorial to follow for this part: http://alanstorm.com/magento_2_object_manager_argument_replacement.

More backgrounds on Magento 2 Dependency Injection: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/depend-inj.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top