cancel
Showing results for 
Search instead for 
Did you mean: 

Customer Image Attribute?

Customer Image Attribute?

Using Magento 2.3.5p1

 

Need to create a customer attribute (not controllable by the customer but managed by the admins) that is an image attribute. We need to be able to add certificate images to customer accounts as they are sent to us.

 

Can't seem to find a built-in or easy way to do this. Any suggestions?

 

Thanks.

11 REPLIES 11

Re: Customer Image Attribute?

Hi @SJ_Med,

 

Please follow the below link for adding a new image type customer attribute.

https://www.google.com/amp/s/meetanshi.com/blog/create-custom-image-attribute-for-customer-in-magent...

 

Skip the part in which attribute is added in frontend customer account create a page and also make 

'user_defined' => false,

 

Hope this helps you!

Problem Solved! Click Kudos &Accept as Solution!

Re: Customer Image Attribute?

Awesome! Thanks Nishu! I'll try this over the weekend and if it works, I'll mark this closed then. I appreciate the quick response!

Re: Customer Image Attribute?

I seem too be having some issues - perhaps you can guide me in creating this custom extension...

 

I named the Module "CustomerImageAtttribute" and replaced all the {Module} tags in the code with this, and used a Vendor name and replaced all the tags with that. The folders I made in app/code conformed to this.

 

I've created all the files necessary - copied code into the newly created filenames in the correct places on my system below the new folders above.

 

However when I ran the php bin/magento module:status command, I didn't see the module name. I therefore couldn't enable, nor did I see it in the list of already enabled modules, of course. And of course, the choice of an image-type attribute isn't available when creating a new Customer Attribute....

 

I'm missing something obvious. Can you help me here?

 

Thanks.

Re: Customer Image Attribute?

Hi @SJ_Med ,

 

Link i shared with you before is for adding custom attribute in customer entity.

 

But for creating it through basic module you need to create some basic files of module. Please follow below tutorial for the same.

https://devdocs.magento.com/videos/fundamentals/create-a-new-module/

 

Hope this helps you! 

Problem Solved! Click Kudos & Accept as Solution!

Re: Customer Image Attribute?

Thanks - forgot my module.xml.

 

Still having an issue with my registration.php, and can't seem to figure out why. It's even modeled correctly after the version on docs.magento.com, and yet I keep getting a namespace error on InstallData.php, line 3 which is the namespace declaration, during compile. My namespace declaration appears clean, and shouldn't't generate an error. I've pasted code here and hoping for help?

 

registration.php:

<?php

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'PragmaticProjects_CustomerImageAttribute',
    __DIR__
);

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="PragmaticProjects_CustomerImageAttribute" setup_version="1.0.0">
  </module>
</config>

installdata.php:

<?php
 
namespace PragmaticProjects\CustomerImageAttribute\Setup;
 
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
... (I omitted the code here for brevity as it's the same as in the online version, unchanged.)
}

As you can see, the namespace declaration shouldn't throw the following error during my compile:

[mysystem]$ php bin/magento setup:di:compile
Compilation was started.
Repositories code generation... 1/7 [====>-----------------------]  14% < 1 sec 76.5 MiBPHP Parse error:  syntax error, unexpected 'namespace' (T_NAMESPACE) in /chroot/home/mysystem/app/code/PragmaticProjects/CustomerImageAttribute/Setup/InstallData.php on line 3

Any ideas? I know this is basic stuff, and I appreciate the help!

Re: Customer Image Attribute?

Hi @SJ_Med,

 

Try to change your registration.php code like below 

<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',
__DIR__
);

Change the module Name according to your values.

Hope this helps you!

Problem Solved! Click Kudos & Accept as Solution! 

Re: Customer Image Attribute?

Unfortunately, same result.

 

I went a step further, and then changed all references to the vendor and module and got the same result.... same error on line 3 in the same file - namespace error......

 

It's accepting the registration - I had an earlier error where its was giving me a registration error, but this is not the case now. If it's giving me a namespace error at the declaration of:

namespace Learning\FirstUnit\Setup;

If I try to go back to basics, and do this the way it was meant to be done as you initially indicated, should I be leaving [Vendor] and [module] untouched from the code, or what should I substitute in there? And where in my magento 2 environment should I insert this?

 

Thanks again Nishu.

Re: Customer Image Attribute?

Hi @SJ_Med ,

 

Let's make it little simple. I will be creating one module and mentioning complete steps.

1. Created new folder

app/code/Learning/FirstUnit

2. Create the registration.php

<?php \Magento\Frameapp/code/Learning/FirstUnit/etcwork\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',
__DIR__
);

3. Create module.xml in below folder in 

app/code/Learning/FirstUnit/etc

<?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="Learning_FirstUnit" setup_version="1.0.0">
    </module>
</config>

4. And you must be having to upgrade schema file in below folder

app/code/Learning/FirstUnit/Setup/

namespace Leaning\FirstUnit\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;


class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $connection = $installer->getConnection();
        $tableName  = $setup->getTable('mageplaza_social_customer');
        if (version_compare($context->getVersion(), '1.1.0', '<')
            && $connection->tableColumnExists($tableName, 'status') === false
        ) {
            $connection->addColumn(
                $tableName,
                'status',
                [
                    'type'    => Table::TYPE_INTEGER,
                    'comment' => 'Status',
                ]
            );
        }

        $installer->endSetup();
    }
}

Please check your code once with the provided code. 

 

Hope this helps You!

Problem Solved! Click Kudos & Accepted as Solution!

 

 

 

 

Re: Customer Image Attribute?

Hi Nishu Jindal,

 

Apologies for the delay - some serious issues with our site that required a lot of attention.

 

I'm back at this, and after making a small change to the registration.php for my environment, your code below is in and compiled perfectly, of course. Here's the registration.php I  needed:

 

<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Learning_FirstUnit',
__DIR__
);

But the other 2 files - module.xml and Setup.php are as you dictated below.

 

I would think this should allow me to substitute the Customer Attribute code using the model registration.php and module.xml and then the files from the code if I place Learning as vendor and FirstUnit as module, keeping the folder structures correct? Or is there more to come in your example so I can understand what is next?

 

Thanks for your help and patience!