【问题标题】:Default select for product attribute - Magento产品属性的默认选择 - Magento
【发布时间】:2012-08-14 04:59:59
【问题描述】:

我在 Magento 中为产品创建了一个自定义 Yes/No 属性,在升级文件中使用此代码:

$model = Mage::getModel('catalog/resource_eav_attribute');

$attributeData = array(
    'attribute_code' => 'custom_attribute',
    'frontend_label' => 'My custom attribute',
    'is_global' => '1',
    'frontend_input' => 'boolean',
    'backend_type' => 'int',
    'default_value' => null,
    'is_unique' => '0',
    'is_required' => '0',
    'is_configurable' => '1',
    'is_searchable' => '1',
    'is_visible_in_advanced_search' => '1',
    'is_comparable' => '1',
    'is_used_for_price_rules' => '0',
    'is_wysiwyg_enabled' => '1',
    'is_html_allowed_on_front' => '1',
    'is_visible_on_front' => '1',
    'used_in_product_listing' => '1',
    'used_for_sort_by' => '1',
    'is_filterable_in_search' => 1,
    'is_filterable' => 1,
    'source_model' => null,
    'option' => array(),
    'apply_to' => 'simple,configurable,bundle,grouped',
    'is_user_defined' => true,
    'global' => true,
);

$model->addData($attributeData);
$model->setEntityTypeId(
    Mage::getModel('eav/entity')->setType(Mage_Catalog_Model_Product::ENTITY)->getTypeId()
);
$model->save();

代码有效。但我希望在创建新产品或尝试编辑产品时默认选择是选项。现在,只有 No 首先显示。我尝试将 defult_value 更改为 true 或 1,但它不起作用。

【问题讨论】:

    标签: magento attributes default product


    【解决方案1】:

    我发现 default_value 是用于创建新产品时的选项值,而不是用于没有设置属性的产品。因此,我认为为了在所有产品上显示“是”,我应该编写一个脚本来更新具有此属性的所有产品。

    【讨论】:

    • 一直在为这个问题挠头好几个小时,感谢您解决这个问题!
    • 嗨,你能分享一下表名吗,这样我就可以从sql查询中更新所有属性值为YES的产品。
    【解决方案2】:

    要添加属性,您应该转到文件夹:

    /var/www/html/<YOUR APP>/app/code/core/Mage/Catalog/etc
    

    并编辑文件:config.xml,转到该部分:

    <product> 
    <collection> 
    <attributes>
    

    并添加所需的属性。接下来,进入文件夹:

    /var/www/html/<YOUR APP>/app/design/frontend/base/<YOUR THEME>/template/catalog/product
    

    并编辑文件new.phtml,并在您需要使用属性调用时:

    <?php echo $_product→getAttributeName();?>
    

    例如,如果您的属性名为size,请使用:

    <?php echo $_product→getSize();?>
    

    【讨论】:

    • 这不是我问的。我的问题不是如何创建属性,而是如何在他没有设置该属性时显示产品的默认值,以及如何在创建/编辑产品时显示是而不是否
    【解决方案3】:

    我认为您需要更新属性。假设“custom_attribute”是您的新属性的属性代码,“默认值”是您要更改/更新的代码,然后您将以下代码添加为新的升级脚本。

        $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
        $installer->startSetup();
    
        $aAttribute = $installer->getAttribute('catalog_product', 'custom_attribute');
        $aAttribute['default_value'] = '1';
    
        $installer->updateAttribute('catalog_product', 'custom_attribute', $aAttribute);
        $installer->endSetup();
    

    我希望这行得通。

    【讨论】:

    • 问题是数据库中设置了默认值。在eav_attribute 表中,在我的 default_value 列上的属性中显示 1。但是当我转到前端并使用 $product->getMyAttribute() 时,它没有显示。只有在我点击产品编辑的保存后才会显示该属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 2017-03-17
    • 2019-03-01
    • 2017-03-03
    • 1970-01-01
    相关资源
    最近更新 更多