Skip to main content

Add custom source model

Error:

Execution failed (Unable to load attribute "google_product_category": unknown source type model `YourWebshop\App\Model\CustomSourceModel`)

Solution: Add a custom attribute source model:

$magentoSystemRepository->addCustomAttributeSourceModel('YourWebshop\App\Model\CustomSourceModel');

Optionally you can set the options by passing a Magento2AttributeOptionCollection as second parameter:

$options = new \Attlaz\Adapter\Magento2\Model\Magento2AttributeOptionCollection();

$options->addAttributeOption(new \Attlaz\Adapter\Magento2\Model\Magento2AttributeOption(0, 'optionA'));
$options->addAttributeOption(new \Attlaz\Adapter\Magento2\Model\Magento2AttributeOption(1, 'optionB'));
$options->addAttributeOption(new \Attlaz\Adapter\Magento2\Model\Magento2AttributeOption(2, 'optionC'));

$magentoSystemRepository->addCustomAttributeSourceModel('YourWebshop\App\Model\CustomSourceModel', $options);

This can be done in the di.php file of your project:

Magento2SystemRepository::class => DI\decorate(function (Magento2MysqlSystemRepository $magentoSystemRepository, \Psr\Container\ContainerInterface $c) {
$magentoSystemRepository->addCustomAttributeSourceModel('Amasty\GiftCard\Model\Config\Source\EmailTemplate');
return $magentoSystemRepository;
}