将自定义产品属性选项合并为一个

时间:2016-12-13 07:07:18

标签: magento attributes

我有许多产品自定义属性都具有品牌的相同类型下拉列表,如

brand_cosmetic
cos_acc_brand
face_brand
mobile_brand

他们有很多选择。我想创建一个新的属性名称product_brand,并希望合并上面的所有属性选项是否有任何方式在magento中以编程方式执行此操作。我搜索了很多,但没有找到任何解决方案。

2 个答案:

答案 0 :(得分:1)

在我的下面示例中,我删除了

  

brand_cosmetic,cos_acc_brand,face_brand,mobile_brand属性和   创建了一个新的test_drop属性

用于所有组合选项。

在root用户创建一个文件test.php,然后查看下面的代码并在浏览器中运行。

<?php  
require_once dirname(__FILE__) . '/app/Mage.php';
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$newArr = array();

//Possible brand_cosmetic value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'brand_cosmetic'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
    $id = $instance['value']; //id of the option
    $value = $instance['label']; //Label of the option
    $newArr[$id] = $value;
}

//Possible cos_acc_brand value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'cos_acc_brand'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
    $id = $instance['value']; //id of the option
    $value = $instance['label']; //Label of the option
    $newArr[$id] = $value;
}

//Possible face_brand value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'face_brand'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
    $id = $instance['value']; //id of the option
    $value = $instance['label']; //Label of the option
    $newArr[$id] = $value;
}

//Possible mobile_brand value
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'mobile_brand'); //"color" is the attribute_code
$allOptions = $attribute->getSource()->getAllOptions(true, true);
foreach ($allOptions as $instance) {
    $id = $instance['value']; //id of the option
    $value = $instance['label']; //Label of the option
    $newArr[$id] = $value;
}

$newArr = array_filter($newArr);

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();                   

$opionsarr = array_values($newArr);
// Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'test_drop', [
    'type'       => 'int',
    'input'      => 'select',
    'label'      => 'Test Drop',
    'sort_order' => 1000,
    'required'   => false,
    'user_defined' => true,
    'visible_on_front'  => true,
    'visible_in_advanced_search' => false,
    'global'     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'backend'    => 'eav/entity_attribute_backend_array',
    'option'     => [
        'values' => $opionsarr
    ],

]);

// To delete the existing product attribute
$installer->removeAttribute('catalog_product', 'brand_cosmetic');
$installer->removeAttribute('catalog_product', 'cos_acc_brand');
$installer->removeAttribute('catalog_product', 'face_brand');
$installer->removeAttribute('catalog_product', 'mobile_brand');

$installer->endSetup();
?>

答案 1 :(得分:0)

我已经解决了这个问题它不是最好的方法,但它解决了我的问题可能是它帮助了一个人,所以我做了什么,我做了一个功能,取得品牌属性的所有选项在我的项目中

public function mergeAttributeOtionsAction(){
    $brands = array();
    $attSet = Mage::getModel('eav/entity_type')->getCollection()->addFieldToFilter('entity_type_code','catalog_product')->getFirstItem(); // This is because the you adding the attribute to catalog_products entity ( there is different entities in magento ex : catalog_category, order,invoice... etc ) 
    $attSetCollection = Mage::getModel('eav/entity_type')->load($attSet->getId())->getAttributeSetCollection(); // this is the attribute sets associated with this entity 
  foreach ($attSetCollection as $a)
    {
        if(preg_match('/cosmetic/i', $a->getAttributeSetName())){
            $set = Mage::getModel('eav/entity_attribute_set')->load($a->getId());
            $setId = $set->getId();
            $atrr_ = Mage::getModel('catalog/product_attribute_api')->items($setId);
            foreach($atrr_ as $atrr){

                $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product',$atrr['code']);
                if(preg_match('/brand/i', $attribute->getFrontendLabel())){
                    $options = $attribute->getSource()->getAllOptions();
                    foreach ($options as $option){
                        if($option['label'] == 'Not Specified'){
                            continue;
                        }
                        $brands[] = $option['label'];
                    }
                }
            }
        }
    }
    echo '<pre>'; var_dump(implode(',', $brands));echo '</pre>';
    foreach ($brands as $brand){
        echo $brand; echo '<br>';
    }    

 }

下一步是以这种格式制作excel文件 enter image description here

而不是转到管理员&gt; “系统”&gt;导入/导出&gt; DataFlow - 高级配置文件添加新配置文件输入名称,如&#34;导入产品属性&#34;并添加此XML并保存

<action type="dataflow/convert_adapter_io" method="load">
<var name="type">file</var>
<var name="path">datafiles/import</var>
<var name="filename"><![CDATA[import_product_attributes.csv]]></var>
<var name="format"><![CDATA[csv]]></var>
</action>

<action type="dataflow/convert_parser_csv" method="parse">
<var name="delimiter"><![CDATA[;]]></var>
<var name="enclose"><![CDATA["]]></var>
<var name="fieldnames">true</var>
<var name="store"><![CDATA[0]]></var>
<var name="number_of_records">1</var>
<var name="adapter">importexportattributes/convert_adapter_product_attribute</var>
</action>

现在在此文件夹中的magento根文件夹名称数据文件上创建一个文件夹创建另一个&gt;导入,而不是将您在上面创建的XML文件放在此处。

现在转到 admin&gt; “系统”&gt;导入/导出&gt; DataFlow - 高级配置文件打开&#34;导入产品属性&#34; ,然后单击运行配置文件选项卡,然后单击运行配置文件按钮。 现在,您拥有了新的属性,其中包含您在文件中输入的名称和代码以及magento中存在的所有品牌选项。