【问题标题】:Magento adding attribut using install scriptMagento 使用安装脚本添加属性
【发布时间】:2014-04-19 23:58:45
【问题描述】:

我按照一些教程编写了这段代码。

这是 install.0.1.0.php:

$installer = $this;
$installer->startSetup();

$allowCP = array(
                    'group' => 'Prices', 
                    'type' => 'text',
                    'attribute_set' =>  'Default',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Allow Custom Price',
                    'input' => 'select',
                    'option' => array(
                                    'value' => array( 
                                    1 => 'Yes',
                                    0 => 'No',
                                )),
                    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => true,
                    'default' => '1',
                    'searchable' => false,
                    'filterable' => true,
                    'comparable' => false,
                    'visible_on_front' => true,
                    'visible_in_advanced_search' => true,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'apply_to' => '',  // Apply to All product type
                    'class' => ''  
                );

$installer->addAttribute('catalog_product', 'allowcustomprice', $allowCP);

$minCP = array(
                    'group' => 'Prices', 
                    'type' => 'int',
                    'attribute_set' =>  'Default',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Min Custom Price',
                    'input' => 'text',
                    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => true,
                    'searchable' => false,
                    'filterable' => true,
                    'comparable' => false,
                    'visible_on_front' => true,
                    'visible_in_advanced_search' => true,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'apply_to' => '',  // Apply to All product type
                    'class' => ''       
            );
$installer->addAttribute('catalog_product', 'mincustomprice', $minCP);
$installer->endSetup();

这是 config.xml:

<modules>
    <WebDirect_CustomPrice>
        <version>0.1.0</version>
    </WebDirect_CustomPrice>
</modules>
<!-- ******************************** -->

<global>
    <helpers>
        <customprice>
            <class>WebDirect_CustomPrice_Helper</class>
        </customprice>
    </helpers>
    <models>
        <customprice>
            <class>WebDirect_CustomPrice_Model</class>
        </customprice>
    </models>
    
    <resources>
        <customprice_setup>
          <setup>
            <module>WebDirect_CustomPrice</module>
            <class>WebDirect_CustomPrice_Model_Resource_Setup</class>
          </setup>

          <connection>
            <use>core_setup</use>
          </connection>
        </customprice_setup>

        <customprice_write>
          <connection>
            <use>core_write</use>
          </connection>
        </customprice_write>
        <customprice_read>
          <connection>
            <use>core_read</use>
          </connection>
        </customprice_read>
  </resources>

</global>

这是 Mymodule/Model/Resource/Setup.php 类:

class WebDirect_CustomPrice_Model_Resource_Setup extends Mage_Eav_Model_Entity_Setup {
}

我编译这段代码的时候,什么都没有显示,所以看不到有没有错误。

【问题讨论】:

    标签: magento magento-1.7 magento-1.8


    【解决方案1】:
    try 
    
    <?php
    
    $installer = $this;
    $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
    $installer->startSetup();
    
    
    $installer->addAttributeGroup('catalog_product', 'Default', 'Prices', 1000);
    $installer->addAttribute('catalog_product', 'custom_price', array(
            'group'             => 'Prices',
            'label'                => 'Apply Custom price',
            'type'                => 'int',
            'input'                => 'boolean',
            'source'               => 'eav/entity_attribute_source_boolean',
            'global'               => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
            'visible'              => 1,
            'required'             => 1,
            'user_defined'         => 1,
            'searchable'           => 0,
            'filterable'           => 0,
            'comparable'           => 0,
            'visible_on_front'     => 0,
            'visible_in_advanced_search'    => 0,
            'unique'            => 0,
            'default'            => 0
    ));
    
    $setup->updateAttribute('catalog_product', 'custom_price', 'is_used_for_promo_rules',1);
    $setup->updateAttribute('catalog_product', 'custom_price', 'is_used_for_price_rules',1);
    $installer->endSetup(); 
    

    我想你忘了为你的属性定义属性组

    希望有帮助

    【讨论】:

    • 那么config.xml和Setup.php有什么错误
    • 我正在寻找解决方案几个小时,我不想失去更多:(
    • 我做了所有这些并刷新缓存,重新索引索引但没有结果
    • 您是否在管理员中检查了是否已创建属性?
    • install.0.1.0.php 只执行一次。如果您使用我的代码更新了您的代码,请创建升级脚本并在您的 etc/config.xml 中更改更新的版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多