2015-11-10 45 views
0

我正在創建默認值爲1的客戶屬性,但在創建屬性後,它不會爲現有客戶設置屬性默認值。Magento:添加客戶屬性不爲現有客戶設置默認值

這是我的安裝腳本

<?php 
$installer = $this; 

$installer->startSetup(); 
$setup = Mage::getModel('customer/entity_setup', 'core_setup'); 

$setup->addAttribute('customer', 'is_activated', array(
    'type'   => 'int', 
    'input'   => 'select', 
    'label'   => 'Activated', 
    'global'  => 1, 
    'visible'  => 1, 
    'required'  => 0, 
    'user_defined' => 0, 
    'default'  => 1, 
    'visible_on_front' => 0, 
    'source'  => 'eav/entity_attribute_source_boolean', 
)); 

$installer->endSetup(); 

回答

1

請嘗試:

<?php 
$installer = $this; 
$installer->startSetup(); 


$installer->addAttribute("customer", "is_activated", array(
    "type"  => "int", 
    "backend" => "", 
    "label" => "Is Activated", 
    "input" => "select", 
    "source" => "eav/entity_attribute_source_boolean", 
    "visible" => true, 
    "required" => false, 
    "default" => "Yes", 
    "frontend" => "", 
    "unique"  => false, 
    "note"  => "" 

    )); 

     $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "is_activated"); 


$used_in_forms=array(); 

$used_in_forms[]="adminhtml_customer"; 
$used_in_forms[]="checkout_register"; 
$used_in_forms[]="customer_account_create"; 
$used_in_forms[]="customer_account_edit"; 
$used_in_forms[]="adminhtml_checkout"; 
     $attribute->setData("used_in_forms", $used_in_forms) 
     ->setData("is_used_for_customer_segment", true) 
     ->setData("is_system", 0) 
     ->setData("is_user_defined", 1) 
     ->setData("is_visible", 1) 
     ->setData("sort_order", 100) 
     ; 
     $attribute->save(); 



$installer->endSetup(); 
    ?> 

同時確保安裝類資源config.xml會Mage_Customer_Model_Entity_Setup

0

謝謝@Blastfreak,我嘗試了你的方式,但它對我不起作用。所以我更新了我的代碼,在安裝腳本的底部添加了此代碼,以手動更新新屬性的值。

function callback($args) 
{ 
    $customer = Mage::getModel('customer/customer'); 
    $customer->setData($args['row']); 
    $customer->setExtraStatus('active'); 
    $customer->getResource()->saveAttribute($customer, 'extra_status'); // is_validated be change to extra_status 
} 

$installer->startSetup(); 

$customers = Mage::getModel('customer/customer')->getCollection(); 
Mage::getSingleton('core/resource_iterator')->walk($customers->getSelect(), array('callback')); 

$installer->endSetup();