Magento 将所有客户的自定义属性保存到客户实体

Magento 将所有客户的自定义属性保存到客户实体,magento,magento-1.9,Magento,Magento 1.9,我在客户实体中创建了一个自定义属性。我可以在我的eav\u属性表中看到自定义属性 但是,在从管理面板保存之前,我无法在代码中获取属性 创建自定义属性的我的脚本: <?php $installer = $this; $installer->startSetup(); $entityTypeId = (int)$installer->getEntityTypeId('customer'); $attributeSetId = (int)$installer->g

我在客户实体中创建了一个自定义属性。我可以在我的
eav\u属性
表中看到自定义属性

但是,在从管理面板保存之前,我无法在代码中获取属性

创建自定义属性的我的脚本:

<?php

$installer = $this;
$installer->startSetup();
$entityTypeId     = (int)$installer->getEntityTypeId('customer');
$attributeSetId   = (int)$installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = (int)$installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute( $entityTypeId, 'whatsapp_status', array(
    'type'               => 'int',
    'label'              => 'Whatsapp Status',
    'input'              => 'select',
    'forms'              => array('adminhtml_customer'),
    'source'             => 'dbaux_customer/source_whatsappOption',
    'required'           => false,
    'visible'            => 1,
    'position'           => 110,
    'default'            => 'Inactive',
));

$installer->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, 'whatsapp_status', 100);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'whatsapp_status');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();

$installer->endSetup();
但是如果我在管理面板中保存该值,那么我将得到正确的值,而不是null


那么,是否有一种方法可以向所有客户添加一个带有默认值的新属性,而无需从管理面板保存字段

这里有一些代码,您可以浏览下面的代码并相应地构建属性

/app/code/local/Your/Customattribute/sql/Your\u Customattribute\u setup/install-0.1.0.php

<?php
$installer = $this;

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId,     $attributeSetId);

$setup->addAttribute("customer", "customattribute",  array(
"type"     => "varchar",
"backend"  => "",
"label"    => "Custom Attribute",
"input"    => "text",
"source"   => "",
"visible"  => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique"     => false,
"note"       => "Custom Attribute"
));

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

$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'customattribute',
'999'  //sort_order
);

$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();

希望此代码对您有所帮助。

它只创建了
eav\u属性
,但在专门保存之前不会为所有客户添加值。我实际上在寻找的是为所有具有默认值的客户创建属性。一旦有了属性,您可以转到“设置”并选中“为您的商店使用默认值”
<?php
$installer = $this;

$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId,     $attributeSetId);

$setup->addAttribute("customer", "customattribute",  array(
"type"     => "varchar",
"backend"  => "",
"label"    => "Custom Attribute",
"input"    => "text",
"source"   => "",
"visible"  => true,
"required" => false,
"default" => "",
"frontend" => "",
"unique"     => false,
"note"       => "Custom Attribute"
));

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

$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'customattribute',
'999'  //sort_order
);

$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();
<?xml version="1.0"?>
    <config>
    <modules>
        <Your_Customattribute>
            <version>0.1.0</version>
        </Your_Customattribute>
    </modules>
    <global>

        <resources>
            <Your_Customattribute_setup>
                <setup>
                    <module>Your_Customattribute</module>
                    <class>Mage_Customer_Model_Entity_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </Your_Customattribute_setup>
            <Your_Customattribute_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </Your_Customattribute_write>
            <Your_Customattribute_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </Your_Customattribute_read>
        </resources>
    </global>

</config>
<?xml version="1.0"?>
    <config>
        <modules>
            <Your_Customattribute>
            <active>true</active>
            <codePool>local</codePool>
            <version>0.1.0</version>
        </Your_Customattribute>
    </modules>
</config>
$customer = Mage::getModel('customer/customer')->load($custid);
$customer->getCustomattribute();
$customer->setCustomattribute($yourjson);