Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 3
Adding new attributes to Customer with backend model in module. 
 
mongolito404
Jr. Member
 
Avatar
Total Posts:  11
Joined:  2008-01-28
 

Hi,

I’m trying to add attributes to the Customer model with one a “backend model” to add some custom beforeSave behavior (data validation).

I did add my attributes to the eav_attribute table and set the “backend_model” for one of them. Like this:

INSERT INTO `eav_attribute` (`entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`)
VALUES (1'vatnumber'NULL'customer_entity/customer_attribute_backend_vatnumber''varchar'NULLNULL'text''VAT Number'NULLNULL1110NULL1000001);
INSERT INTO `eav_attribute` ( `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`)
VALUES (1'vatcountry'NULLNULL'varchar'NULLNULL'select''VAT Country'NULL'vatcustomer_entity/customer_attribute_source_vatcountry'1110'BE'1111001);
INSERT INTO `eav_attribute` ( `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`)
VALUES (1'vatvalid'NULLNULL'int'NULLNULLNULL'VAT Number is valided by VIES'NULLNULL1100NULL1100001);
INSERT INTO `eav_attribute` (`entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`)
VALUES (1'vatlastcheck'NULLNULL'datetime'NULLNULLNULL'Date of the last VAT Number validity check.'NULLNULL1100NULL0000001);

When testing I directly add the file in the app/code/core/Mage/Customer/Model/Entity/Customer/Attribute/Backend directory and changed the lat part of the name to my class/filename. Now I want to put it in a Module and I can’t get it to work.
I created the following files with no success:
app/etc/modules/VatCustomer.xml

<config>
     <
modules>
         <
Mage_VatCustomer>
             <
active>true</active>
             <
codePool>local</codePool>
         </
Mage_VatCustomer>
     </
modules>   
</
config>
app/code/local/Mage/Vatcustomer/etc/config.xml
<?xml version="1.0"?>
<config>
  <
modules>
    <
Mage_VatCustomer>
      <
version>0.1.0</version>
    </
Mage_VatCustomer>
  </
modules>
  <global>
    <
models>
      <
vatcustomer>
          <class>
Mage_VatCustomer_Model</class>
      </
vatcustomer>
    </
models>
  </global>
</
config>
app/code/local/Mage/Vatcustomer/Model/Entity/Customer/Attribute/Backend/Vatnumber.php
class Mage_VatCustomer_Model_Entity_Customer_Attribute_Backend_Vatnumber extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
    
public function beforeSave($object)
    
{
        
// doing some stuff here...
    
}
}
If I go to the /admin/customer/ page, I get
Warning: include(Mage/Customer/Model/Entity/Customer/Attribute/Backend/Vatnumber.php[function.include]failed to open streamNo such file or directory in /home/pbuyle/public_html/magento/app/code/core/Mage/Core/functions.php on line 44
. When I put my file directly in app/code/local/Mage/Customer/Model/Entity/Customer/Attribute/Backend/, with the proper prefix in the DB, it worked.

What did I miss ?

Also, has you can see in my eav_attributes addition, I’m trying to store boolean as int value. But I have to manually convert boolean to int (0 and 1) when using “generated” accessor (ie. $customer->setValid(false) doesn’t work, I’ve to do $customer->setValid(0)). Did I also miss something here ?

Finally, I added a “backend_source” to the vat_country attribute. It works to generate the dropdown in the admin’s “edit customer” page, but how do I re-use it to generate dropdown in templates (such as customer register and edit). Where can I put “helper” code. How can this code access the “source class” ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Based on your code samples:

Backend model for ‘vatnumber’ should be ‘vatcustomer/entity_customer_attribute_backend_vatnumber’
Source model for ‘vatcountry’ should be ‘vatcustomer/entity_customer_attribute_source_vatcountry’ (if Mage_Vatcustomer_Model_Entity_Customer_Attribute_Source_Vatcountry file exists)

 
Magento Community Magento Community
Magento Community
Magento Community
 
mongolito404
Jr. Member
 
Avatar
Total Posts:  11
Joined:  2008-01-28
 

It works, thank you.

Also, I was wrong. The setValid(b) method works when b is a boolean. But getValue() return a string ("0" for false, “1” for true).

The vatlastcheck attribute doesn’t work. Everytime I do a setVatlastcheck(...) a row is added to `customer_entity_datetime` with the correct value, but getVatlastcheck return NULL.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mongolito404
Jr. Member
 
Avatar
Total Posts:  11
Joined:  2008-01-28
 

Any idea what’s wrong with the vatlastcheck attribute and why it’s loaded from database even if it’s saved ?

I also wanted to extends the Mage_Customer_Model_Entity_Customer to override getVatvalid() and setVatvalid() to return boolean value (instead of bool castable string). I tried to add

<global>
    <
models>
      <
vatcustomer>
          <class>
Mage_VatCustomer_Model</class>
      </
vatcustomer>
      <
customer_entity>
        <
rewrite>
          <
customer>>Mage_VatCustomer_Model_Entity_Customer</customer>
        </
rewrite>
      <
customer_entity>
    </
models>
  </global>
in my module etc/config.xml but it did not works, the extended class __construct() is never called.
 
Magento Community Magento Community
Magento Community
Magento Community
 
aimos
Jr. Member
 
Total Posts:  18
Joined:  2007-12-28
 

Hmmm .. you can’t set up customer custom attributes in the admin area? strange I thought everything is so complicated because you could do that in the admin area.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Rafal
Member
 
Total Posts:  42
Joined:  2007-11-22
Munich
 
aimos - 01 March 2008 01:50 AM

Hmmm .. you can’t set up customer custom attributes in the admin area? strange I thought everything is so complicated because you could do that in the admin area.

Hi,

it’s impossible to make it through admin (at least I don’t see such possibility). I’d need also such feature.

with best regards,
Rafa

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 
Rafal - 03 March 2008 06:41 AM

aimos - 01 March 2008 01:50 AM
Hmmm .. you can’t set up customer custom attributes in the admin area? strange I thought everything is so complicated because you could do that in the admin area.

Hi,

it’s impossible to make it through admin (at least I don’t see such possibility). I’d need also such feature.

with best regards,
Rafa

Well, the problem is two-fold, first you have to make the custom attributes, then you have to make them do something.  The first requires programming/direct SQL manipulation, and the second requires programming.  So, even if you could add attributes with a nice point and click interface, you’d still be left with the problem of making them do something.

I can see why Magento doesn’t have such a point and click interface at this point, it would only solve half the problem.

This type of VAT trick should be collected and made into wiki pages for the “Gateway UK” magento group, or another group.  We all need to utilize the groups more.  Anyone can join any group and start collecting info in a wiki page.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mongolito404
Jr. Member
 
Avatar
Total Posts:  11
Joined:  2008-01-28
 

If somebody is interested, I can share my VAT/VIES integration module. I would like to publish the code somewhere, but I need to clean up license and write the minimum documentation before.

The only feature of my module is to add two required fields to the customer: VAT country and number. The fields are syntax checked and then validated with VIES (http://ec.europa.eu/taxation_customs/vies/). The result of the VIES validation is stored in a third field with the validation date in a fourth one. These are showed and can be edited in the admin customer list and in the customer dashboard.

Currently (running on 0.8.17240), validation works on customer profile edition (by customer or by admin) but not on registration. With the same code running on 0.7.something, the validation only work when editing from the admin interface. So I suspect some bug prevents proper validation on registration and hope for a bug fix in a future release. Also, in the admin interface, form entries for the VAT country and code only appear if the fields are valuated. Since they are required on registration, this is not a big deal, but I would prefer magento to behave like if their value was null or the empty string.

Finally, database installation code is missing and a some SQL queries must be run manually (see first thread post) in order to setup the db.

Once this work, the plan is to
- disable customer without a valid VAT number (prevent them to checkout orders)
- periodically re-validate customers VAT numbers and inform newly invalidated customers of their status change
- allow admin to say “I know VIES told you this VAT number is invalid, but let’s accept it for x days before disabling the account
- automatically add a customer billing address using the address returned by VIES (will be tricky since VIES returns only a badly structured string for the address)

 
Magento Community Magento Community
Magento Community
Magento Community
 
SeL_
Magento Team
 
Avatar
Total Posts:  1140
Joined:  2007-10-10
Paris, France
 

Hi mongolito404,

I am interested in your module when it’s available.
Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mongolito404
Jr. Member
 
Avatar
Total Posts:  11
Joined:  2008-01-28
 

Hi,

I finally took the time to publish my code… everything is on http://code.google.com/p/mangentovies/

As you will see, it’s a little messy. I modified some Magento core files because I couldn’t figure how to rewrite existing controllers, blocks and default templates. However, I did made a little Ant script to generate and archive with the modified files.The current code is base on Magento SVN 0.8.16100. I used piston to maintain a vendor branch so update to newer 0.8 or even 0.9 should be easy.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tberthe
Jr. Member
 
Total Posts:  1
Joined:  2008-04-01
 

Hi,

I tried to add “civility"(or what ever) to the customer’s fields but I don’t succes.
I began to add the required database’s lines, but in the code I don’ know what to do…
Do you know an easy way to do that ?
Why isn’t it possible to find a way to do as easy as the adding fields in “product” ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
rolade
Jr. Member
 
Total Posts:  15
Joined:  2008-03-16
 

Is there a way Varien can supply a Magento-extension from this work… Guys…
I’ also interested in this function…

 
Magento Community Magento Community
Magento Community
Magento Community
 
magnet0
Jr. Member
 
Total Posts:  29
Joined:  2007-10-23
Heraklion, Crete
 

I have prepped the code for magentocommerce ver.1.0, I will post the files as soon as I can.
I also have added an other field. The VAT Office, as in Greece we need this kind of info.

Thank you all,

Yiannis

P.S. There is a little problem on the Country Field on Administration Side, but from the Front End side it works OK.

 
Magento Community Magento Community
Magento Community
Magento Community
 
rolade
Jr. Member
 
Total Posts:  15
Joined:  2008-03-16
 

Nice, surprise us...!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Pukos
Sr. Member
 
Avatar
Total Posts:  108
Joined:  2008-02-15
 
magnet0 - 19 April 2008 02:49 AM

I have prepped the code for magentocommerce ver.1.0, I will post the files as soon as I can.
I also have added an other field. The VAT Office, as in Greece we need this kind of info.

Thank you all,

Yiannis


P.S. There is a little problem on the Country Field on Administration Side, but from the Front End side it works OK.

I’m exited because i’m really need it!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Midimarcus
Jr. Member
 
Avatar
Total Posts:  13
Joined:  2007-12-03
Perugia
 

How can I install and try your module?
Thanks a lot!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 3