7

We are working on trying to create customer address attribute as input type is File, we have downloaded the code from silksoftware by selecting "Need to Add Customer Attribute" -> "Entity Type" -> "Customer Address", but the below code is not working/saving, can any one, please help us on this issue.

<?php
namespace Module\Vendor\Setup;

use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Setup\Context;
use Magento\Eav\Setup\EavSetup;
use Magento\Framework\App\CacheInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory;

class CustomerSetup extends EavSetup {

    protected $eavConfig;

    public function __construct(
        ModuleDataSetupInterface $setup,
        Context $context,
        CacheInterface $cache,
        CollectionFactory $attrGroupCollectionFactory,
        Config $eavConfig
        ) {
        $this -> eavConfig = $eavConfig;
        parent :: __construct($setup, $context, $cache, $attrGroupCollectionFactory);
    } 

    public function installAttributes($customerSetup) {
        $this -> installCustomerAttributes($customerSetup);
        $this -> installCustomerAddressAttributes($customerSetup);
    } 

    public function installCustomerAttributes($customerSetup) {

    } 

    public function installCustomerAddressAttributes($customerSetup) {


        $customerSetup -> addAttribute('customer_address',
            'customeridproof',
            [
            'label' => 'Customeridproof',
            'system' => 0,
            'user_defined' => true,
            'position' => 100,
            'sort_order' =>100,
            'visible' =>  true,
            'default_value' => '',
            'note' => '',

                        'type' => 'varchar',
                        'input' => 'file',
            ]
            );

        $customerSetup -> getEavConfig() -> getAttribute('customer_address', 'customeridproof')->setData('is_user_defined',1)->setData('default_value','')-> setData('used_in_forms', ['adminhtml_customer_address', 'customer_register_address', 'customer_address_edit']) -> save();


    } 

    public function getEavConfig() {
        return $this -> eavConfig;
    } 
} 
  • I also facing this issue. please let me know if you found any solution for this. I am using same code as you using through silksoftware – Purushotam Sharma Mar 23 '18 at 5:00
1

If I were you I should use Mage2Gen to develop de basics of your Magento 2 Modules

https://mage2gen.com/

It is possible to create Customer and Customer Address Attributes through an easy interface.

Besides generating modules you could take a look at this Code Snippet to easily create Custom Customer Attributes https://mage2gen.com/snippets/customerattribute/

Warning. Not all template files are setup to load customer or customer address attributes dynamically.

It is actually pretty easy to add the address fields to the Registration Page for more information how to add the Address Fields to the Registration page take a look at one of my articles https://codeblog.experius.nl/magento-2-address-fields-customer-registration/

And if you then want to add your custom field to this template then overwrite template file in your Theme. This is the core file path:

vendor/magento/module-customer/view/frontend/templates/form/register.phtml

  • Thanks for answer, but this solution can not be fulfilled for input type file or image. – Krishna ijjada Jun 29 '17 at 14:29
0

Looks like you're missing the 'backend' type.

the backend attribute tells Magento what Attribute class to use to handle the store/save of the field once it hits the backend.

Try this :

    $customerSetup -> addAttribute('customer_address',
        'customeridproof',
        [
            'label' => 'Customeridproof',
            'system' => 0,
            'user_defined' => true,
            'position' => 100,
            'sort_order' =>100,
            'visible' =>  true,
            'default_value' => '',
            'note' => '',
            'type' => 'varchar',
            'input' => 'file',
            'backend' => 'customer/attribute_data_file'
        ]
        );

This code is essentually saying use Mage_Customer_Model_Attribute_Data_File to handle the file upload. This class is an empty extension of Mage_Eav_Model_Attribute_Data_File which is where most the logic is if you need to review.

I believe for file types you need this as things like image/file post-processing, where to save the upload and how to represent/load the varchar underlying DB type are all handled in the attribute class.

If its not suitable (I realise I'm using a customer attribute on the address but it should be ok) for whatever reason, simply create a new Model in a module and have it inherit from Mage_Eav_Model_Attribute_Data_File, for example :

class My_Module_Model_Attribute_File extends Mage_Eav_Model_Attribute_Data_File
{
}

And get to work overloading any of the handling methods you'll need, and set your backend to my_module/attribute_file (don't forget to delare your model in the config too).

Disclaimer : While I write this solution I am technically looking at a Magento 1.9 site and had my team members who are working on Magento 2 double check class exists and suchlike, should work or at least indicate the way forward.

Hope it helps!

PS> I've seen enough custom address attributes this week to last a lifetime, thought I'd make this suggestion as I've rolled a bunch of attributes in the last couple of weeks!! So fingers crossed it advances your cause.

  • Thanks for the answer, after i spent some time on this issue, i realized that i have to set [ 'attribute_set_id' => $attributeSetId, 'attribute_group_id' => $attributeGroupId, ] to my attribute, then everything seems to be working in admin. when i am going to add any image in checkout page customer address, the data is not passing or getting saved, which seems strange to me. In this case input type 'text' is working fine but input type 'file' is not working anymore. Please suggest. – Krishna ijjada Jun 23 '17 at 16:03
  • Do you mean that the image can be saved via the adminhtml panels but not via the frontend? Because its an attribute of address does it work in My Account or Registration (assuming you gather addresses at registration)? I ask as Checkout can be a funny beast that is largely overridden or extended (for instance onestepcheckout or SagePay) Did you try setting the backend model?? – cygnus digital Jun 26 '17 at 8:30
  • One technique I use is to debug/log inside the actual attribute (if you do this on the core attribute make sure you roll it back afterwards!) as there are routines that are triggered on Validation and Save, you can at least debug and see that admin hits them and if the front end also does. Also compare your admin input fields to the ones on the frontend. – cygnus digital Jun 26 '17 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.