`
kirenenko04
  • 浏览: 146389 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

Magento – Set Product Dropdown and Multiselect Values Programmatically

 
阅读更多

Here’s how to programmatically, meaning through code, set a product’s attribute value when the attribute is of type Dropdown or Multiselect.  For dropdown, we’ll be interested in setting only a single value.  And obviously for multiselect, we’ll be interested in setting multiple values.

With Text attributes such as Name and Description, you can do something like:

$product->setName( 'My Sweet Shirt' );
$product->setDescription( 'This shirt will make you look good, thus impressing girls.' );

Unfortunately, this won’t work for attributes whose values are predefined.  Instead of cursing Magento, think of it as a data-integrity/validation measure.

Anyway, assuming you already have a $product object, our overall process will be to:

  1. Load an attribute object for the attribute you want to work on
  2. Load the collection of that attribute’s values
  3. Make your choices (different for Dropdown and Multiselect)
  4. Save the product

1. Load an attribute object for the attribute you want to work on

$attribute = Mage::getModel('eav/entity_attribute');
$attribute->loadByCode( 4, 'color' );

2. Load the collection of that attribute’s values

$values = array();
$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
	->setAttributeFilter( $attribute->getId() )
	->setStoreFilter( Mage_Core_Model_App::ADMIN_STORE_ID, false)
	->load();

foreach ($valuesCollection as $item) {
	$values[$item->getValue()] = $item->getId();
}

3. Make your choice – DROPDOWN

$product->setColor( $values['Blue'] ); // just do whatever you need to code 'Blue' instead of hard-coding it

3. Make your choices – MULTISELECT

$product->addData( array(
	'color' => $values['Blue'] .','. $values['Red'] .','. $values['Black']  // just putting together a comma-separated list of values
) );

4. Save the product

$product->save();
分享到:
评论

相关推荐

    Magento2-City-Dropdown:Magento 2扩展程序的City Dropdown

    Magento 2城市下拉菜单 Magento 2的城市下拉列表的创建是为了允许用户/商人在结帐过程中将城市和地区/州/省作为下拉列表包括在内,但是最近在保存新地址时,也可以在此处找到该选项。 使用此工具,您可以导入每个...

    Magento 2 Beginners Guide

    You’ll start by getting a general understanding of what Magento is, why and how you should use it, and whether it is possible and feasible to migrate from an old web store to Magento 2. As you work ...

    Magento 2 Cookbook

    Chapter 1, Installing Magento 2 on Apache and NGINX, is a totally different ballgame compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the...

    Magento2 CookBook

    Chapter 1, Installing Magento 2 on Apache and NGINX, is a totally different ballgame compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the...

    Magento - Developing and Maintaining your Cloud Project

    Magento cloud - Developing and Maintaining your Cloud Project.

    Magento-中文开发教程

    深入理解Magento-第一章 – Magento强大的配置系统 深入理解Magento-第二章 – Magento请求分发与控制器 深入理解Magento-第三章 – 布局,块和模板 深入理解Magento-第四章 – 模型和ORM基础 深入理解...

    magento 常用的方法总结

    magento 常用的方法 与 总结, 对于起步学习magento 的非常有帮助

    m2_belvg_product-tabs-2.0.0.zip

    magento2在产品详情添加自定义tab,1. Enable module with php bin/magento module:enable BelVG_ProductTabs 2. Run php bin/magento setup:upgrade 3. Recompile DI php bin/magento setup:di:compile 4. Recompile...

    [Magento] Magento 扩展开发入门教程 (英文版)

    Understand Magento extensions, and build your own from scratch! Overview Get a detailed insight into the structure of Magento Learn about event/observer-driven modules Get a hands-on introduction to...

    magento数据结构分析

    magento数据结构分析 magento数据字典

    Athlete Magento免费模板

    演示地址:http://olegnax.com/product/athlete-responsive-magento-theme/livepreview/ Athlete Magento主题,兼容Magento 1.9.x, 1.8.x, 1.7.x ,特点:完全自适应(响应式布局),自定义子主题,无限颜色,完全...

    Magento-SMTP-Email

    magento的SMTP插件magento的SMTP插件magento的SMTP插件

    magik shoes magento 模板, magento 1.7 模板

    magik shoes magento 模板, magento 1.7 模板

    magento-set-price-precision:允许更改价格精度(小数位数)

    Magento-设定价格精度扩展 概述 由于某些原因,您可能需要更改在商店前台显示价格时使用的小数位数(精度)。 此扩展名允许您设置所需的精度。 兼容性 在Magento CE 1.6-1.9上测试 笔记 免费和开源 完全可配置 附带...

    magento Extended Product Grid with Editor插件

    官网卖99美元的强大插件Extended Product Grid with Editor本人已经在1.7版本上安装测试过能用

    Magento深入理解Magento

    深入理解Magento 由精东博客下载下来制作成pdf

    Magento插件开发手册 Magento Extension Developers Guide

    Magento插件开发手册 Magento Extension Developers Guide

    magento static block

    New GUI added 10 predefined block positions on each page Ability to place any block in any position by easily ... Meets Magento programming practices for most versions and customizations compatibility

    Magento1.9 超好用积分插件Points and Rewards

    Magento1社区版,1.9可用,官网售价299刀 前台演示地址:http://points.demo.aheadworks.com/index.php/points/reward/ 后台演示地址:...

Global site tag (gtag.js) - Google Analytics