Posted on

Magento2: Custom source model – Class does not exist

I have created a multi-select EAV Attribute for my customers with a custom source model. However, whenever I browse to the customers management page or call getSource on the attribute, I get a class does not exist error for the source model. Am I missing something?

Sample exception:

Exception #0 (ReflectionException): Class Wizbusiness/CustomerSubscriptions/Model/Config/Source/ProductTags does not exist

Exception #0 (ReflectionException): Class Wizbusiness/CustomerSubscriptions/Model/Config/Source/ProductTags does not exist
#0 <path>/vendor/magento/framework/Code/Reader/ClassReader.php(19): ReflectionClass->__construct(‘Wizbusiness/Cus…’)
#1 <path>/vendor/magento/framework/ObjectManager/Definition/Runtime.php(44): MagentoFrameworkCodeReaderClassReader->getConstructor(‘Wizbusiness/Cus…’)
#2 <path>/vendor/magento/framework/ObjectManager/Factory/Dynamic/Developer.php(71): MagentoFrameworkObjectManagerDefinitionRuntime->getParameters(‘Wizbusiness/Cus…’)
#3 <path>/vendor/magento/framework/ObjectManager/ObjectManager.php(57): MagentoFrameworkObjectManagerFactoryDynamicDeveloper->create(‘Wizbusiness/Cus…’, Array)
#4 <path>/vendor/magento/framework/Validator/UniversalFactory.php(36): MagentoFrameworkObjectManagerObjectManager->create(‘Wizbusiness/Cus…’, Array)
#5 <path>/vendor/magento/module-eav/Model/Entity/Attribute/AbstractAttribute.php(538): MagentoFrameworkValidatorUniversalFactory->create(‘Wizbusiness/Cus…’)
#6 <path>/vendor/magento/module-customer/Model/AttributeMetadataConverter.php(66): MagentoEavModelEntityAttributeAbstractAttribute->getSource()

For your reference module files:
app/code/Wizbusiness/CustomerSubscriptions/Model/Config/Source/ProductTags.php:

<?php
namespace WizbusinessCustomerSubscriptionsModel;

/**
* Tags copied from products
*
* @author Wizbusiness
*/
class ProductTags extends MagentoEavModelEntityAttributeSourceAbstractSource
{

public function toArray()
{
return [];
}

/**
* Options getter
* @return array
*/
final public function toOptionArray()
{
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$model = $objectManager->get(‘MagentoCatalogModelProduct’);
$attribute = $model->getAttribute(‘tags’);
return $attribute->getAllOptions();
}

/**
* @return array
*/
public function getAllOptions()
{
return $this->toOptionArray();
}

/**
* Get a text for option value
*
* @param string|integer $value
* @return string|bool
*/
public function getOptionText($value)
{
foreach ($this->getAllOptions() as $option) {
if ($option[‘value’] == $value) {
return $option[‘label’];
}
}
return false;
}

/**
* Retrieve flat column definition
*
* @return array
*/
public function getFlatColumns()
{
$attributeCode = $this->getAttribute()->getAttributeCode();
return [
$attributeCode => [
‘unsigned’ => false,
‘default’ => null,
‘extra’ => null,
‘type’ => Table::TYPE_INTEGER,
‘nullable’ => true,
‘comment’ => ‘Custom Attribute Options ‘ . $attributeCode . ‘ column’,
],
];
}
}

app/code/Wizbusiness/CustomerSubscriptions/Setup/InstallData.php:

<?php
namespace WizbusinessCustomerSubscriptionsSetup;

use MagentoCustomerModelCustomer;
use MagentoCustomerSetupCustomerSetup;
use MagentoCustomerSetupCustomerSetupFactory;
use MagentoEavModelEntityAttributeBackendArrayBackend;
use MagentoFrameworkSetupInstallDataInterface;
use MagentoFrameworkSetupModuleContextInterface;
use MagentoFrameworkSetupModuleDataSetupInterface;
use MagentoCatalogModelResourceEavAttribute;

class InstallData implements InstallDataInterface
{

/**
* @var CustomerSetupFactory
*/
private $customerSetupFactory;

public function __construct(CustomerSetupFactory $customerSetupFactory)
{
$this->customerSetupFactory = $customerSetupFactory;
}

/**
* Upgrades data for a module
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

$customerSetup = $this->customerSetupFactory->create([‘setup’ => $setup]);

/** @var CustomerSetup $customerSetup */
$customerSetup->addAttribute(
Customer::ENTITY,
‘subscribedtags’,
[
‘type’ => ‘text’,
‘input’ => ‘multiselect’,
‘label’ => ‘Subscribed Tags’,
‘required’ => 0,
‘system’ => 0, // <– important, otherwise values aren’t saved.
// @see MagentoCustomerModelMetadataCustomerMetadata::getCustomAttributesMetadata()
‘position’ => 100,
‘filterable’ => true,
‘source’ => ‘Wizbusiness/CustomerSubscriptions/Model/Config/Source/ProductTags’,
‘global’ => MagentoEavModelEntityAttributeScopedAttributeInterface::SCOPE_GLOBAL,
]
);

$used_in_forms=array();
$used_in_forms[]=”adminhtml_customer”;
$used_in_forms[]=”checkout_register”;
$used_in_forms[]=”customer_account_create”;
$used_in_forms[]=”customer_account_edit”;
$used_in_forms[]=”adminhtml_checkout”;
$customerSetup->getEavConfig()->getAttribute(‘customer’, ‘subscribedtags’)
->setData(‘used_in_forms’, $used_in_forms)
->setData(“is_used_for_customer_segment”, true)
->setData(“is_user_defined”, 1)
->save();
$setup->endSetup();
}
}

app/code/Wizbusiness/CustomerSubscriptions/etc/module.xml:

<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”>
<module name=”Wizbusiness_CustomerSubscriptions” setup_version=”0.0.11″ />
</config>

app/code/Wizbusiness/CustomerSubscriptions/registration.php:

<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
‘Wizbusiness_CustomerSubscriptions’,
__DIR__
);

Leave a Reply

Your email address will not be published.