Magento:添加客户属性不为现有客户设置默认值

时间:2015-11-10 08:21:39

标签: magento

我正在创建一个默认值为1的客户属性,但在创建该属性后,它不会为现有客户设置属性默认值。

这是我的安装程序脚本

let mirror = Mirror(reflecting: SomeObject)

var dictionary = [String: Any]() 
for child in mirror.children {
    guard let key = child.label else { continue }
    let value: Any = child.value

    dictionary[key] = value

    switch value {
    case is Int: print("integer = \(anyValue)")
    case is String: print("string = \(anyValue)")
    default: print("other type = \(anyValue)")
    }

    switch value {
    case let i as Int: print("• integer = \(i)")
    case let s as String: print("• string = \(s)")
    default: print("• other type = \(anyValue)")
    }

    if let i = value as? Int {
        print("•• integer = \(i)")
    }
}

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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();