2012-03-05 50 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該屬性將設置一個默認值,如果沒有值。