2012-03-05 52 views
0

我正在使用基于某些规则切换客户组的扩展。 我使用此代码向客户添加了属性作为EAV。获取在Magento中为客户设置的属性的默认值

$setup->addAttribute('customer', 'autoswitchgroup', array(
    'type' => 'varchar', 
    'input' => 'select', 
    'label' => 'Autoswitch Group', 
    'global' => 1, 
    'visible' => 1, 
    'required' => 0, 
    'user_defined' => 1, 
    'default' => 'yes', 
    'visible_on_front' => 1, 
    'source' => 'bitstream_all/entity_autoswitchgroup', 
)); 

纵观扩展代码,我看到它使用此代码获取顾客属性:

$customerAttributeValue = $this->_getCustomer()->getDataUsingMethod($attribute); 

其中_getCustomer()被定义为一个对象。 (@return Varien_Object)。

如果我把客户对象上一看我看到这个信息:

[_resourceCollectionName:protected] => customer/customer_collection 
    [_cacheTag:protected] => 
    [_dataSaveAllowed:protected] => 1 
    [_isObjectNew:protected] => 
    [_data:protected] => Array 
     (
      [entity_id] => 1 
      [entity_type_id] => 1 
      [attribute_set_id] => 0 
      [website_id] => 1 
      [email] => **** 
      [group_id] => 11 
      [increment_id] => 000000001 
      [store_id] => 1 
      [created_at] => 2011-06-04 15:11:50 
      [updated_at] => 2012-03-05 14:04:54 
      [is_active] => 1 
      [created_in] => Lenjerii 
      [prefix] => 
      [firstname] => Ovidiu 
      [middlename] => 
      [lastname] => Ungureanu 
      [suffix] => 
      [password_hash] => **** 
      [taxvat] => 
      [facebook_uid] => **** 
      [gender] => 1 
      [dob] => 
      [autoswitchgroup] => yes 
      [is_subscribed] => 1 
      [parent_id] => 0 
      [dob_is_formated] => 1 
      [confirmation] => 
     ) 

    [_hasDataChanges:protected] => 1 
    [_origData:protected] => Array 
     (
      [entity_id] => 1 
      [entity_type_id] => 1 
      [attribute_set_id] => 0 
      [website_id] => 1 
      [email] => **** 
      [group_id] => 11 
      [increment_id] => 000000001 
      [store_id] => 1 
      [created_at] => 2011-06-04 15:11:50 
      [updated_at] => 2012-03-05 13:49:46 
      [is_active] => 1 
      [created_in] => Lenjerii 
      [prefix] => 
      [firstname] => Ovidiu 
      [middlename] => 
      [lastname] => Ungureanu 
      [suffix] => 
      [password_hash] => **** 
      [taxvat] => 
      [facebook_uid] => **** 
      [gender] => 1 
     ) 

    [_idFieldName:protected] => entity_id 
    [_isDeleted:protected] => 

在前端我想同样的信息,得到它使用此: $客户=法师:: getSingleton('客户/会话') - > GETCUSTOMER();

下面是结果:

[_resourceCollectionName:protected] => customer/customer_collection 
    [_cacheTag:protected] => 
    [_dataSaveAllowed:protected] => 1 
    [_isObjectNew:protected] => 
    [_data:protected] => Array 
     (
      [website_id] => 1 
      [entity_id] => 1 
      [entity_type_id] => 1 
      [attribute_set_id] => 0 
      [email] => *** 
      [group_id] => 11 
      [increment_id] => 000000001 
      [store_id] => 1 
      [created_at] => 2011-06-04 15:11:50 
      [updated_at] => 2012-03-05 13:49:46 
      [is_active] => 1 
      [created_in] => Lenjerii 
      [prefix] => 
      [firstname] => Ovidiu 
      [middlename] => 
      [lastname] => Ungureanu 
      [suffix] => 
      [password_hash] => *** 
      [taxvat] => 
      [facebook_uid] => *** 
      [gender] => 1 
      [tax_class_id] => 3 
     ) 

    [_hasDataChanges:protected] => 1 
    [_origData:protected] => Array 
     (
      [website_id] => 1 
      [entity_id] => 1 
      [entity_type_id] => 1 
      [attribute_set_id] => 0 
      [email] => *** 
      [group_id] => 11 
      [increment_id] => 000000001 
      [store_id] => 1 
      [created_at] => 2011-06-04 15:11:50 
      [updated_at] => 2012-03-05 13:49:46 
      [is_active] => 1 
      [created_in] => Lenjerii 
      [prefix] => 
      [firstname] => Ovidiu 
      [middlename] => 
      [lastname] => Ungureanu 
      [suffix] => 
      [password_hash] => **** 
      [taxvat] => 
      [facebook_uid] => **** 
      [gender] => 1 
     ) 

我有什么兴趣把它[autoswitchgroup] - 属性创建。这个不会保存在数据库的所有用户中,这就是为什么我有一个默认值。 现在我不明白的是模块上如何获取客户数据,但前端我没有看到它。

更多的是,模块出现在[_data:protected]中,但不在[_origData:protected]中。

在这两个地方的对象是_resourceCollectionName:保护] =>客户/ customer_collection ...

但愿这不是太长时间阅读。

+1

可能有一个仅为adminhtml区域注册的'customer_load_after'事件观察者。在该观察者方法中,可以设置默认值。只是一个想法。 – Vinai 2012-03-05 15:36:34

+0

难道你不能使用魔法获得值吗?像$ customer-> getAutoswitchgroup()? – seanbreeden 2012-03-05 20:17:31

+0

@seanbreeden不,没有工作。 – Ovidiu 2012-03-06 09:42:05

回答

0

我知道这不是一个真正的答案,但这是我所做的工作。

$attribute = 'autoswitchgroup'; 
$customer->getDataSetDefault($attribute, 'yes'); 

所以,我得到的属性名称和使用getDataSetDefault该属性将设置一个默认值,如果没有值。