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:
- Load an attribute object for the attribute you want to work on
- Load the collection of that attribute’s values
- Make your choices (different for Dropdown and Multiselect)
- 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();
相关推荐
Magento 2城市下拉菜单 Magento 2的城市下拉列表的创建是为了允许用户/商人在结帐过程中将城市和地区/州/省作为下拉列表包括在内,但是最近在保存新地址时,也可以在此处找到该选项。 使用此工具,您可以导入每个...
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 ...
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 "Extended Product Grid with Editor" 插件是一个针对Magento电子商务平台的专业增强工具,旨在提高管理员在后台处理产品数据的效率和便利性。该插件在Magento的官方商店售价99美元,表明其提供了高级功能和...
这篇博客“Magento:后台添加预览按钮 View product in frontend from Magento admin”主要探讨了如何通过自定义开发来实现这一功能,让管理员能够快速检查商品在网站前台的显示状态。 在Magento中,通常管理员需要...
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 常用的方法总结 Magento 是一个功能强大且灵活的电子商务平台,提供了许多实用的方法和函数来帮助开发者快速开发电子商务网站。以下是 Magento 中的一些常用方法总结: 一、获取设计目录路径 在 Magento ...
- `UPDATE catalog_product_entity_int SET value=2 WHERE attribute_id=80 AND entity_id IN (SELECT product_id FROM cataloginventory_stock_status WHERE stock_status=0);` - `SET FOREIGN_KEY_CHECKS=1;` -...
深入理解Magento-第一章 – Magento强大的配置系统 深入理解Magento-第二章 – Magento请求分发与控制器 深入理解Magento-第三章 – 布局,块和模板 深入理解Magento-第四章 – 模型和ORM基础 深入理解...
### Magento官方文档翻译超好——深入理解Magento数据层操作与数据封装 #### 一、引言 Magento是一款基于PHP的企业级电子商务平台,以其强大的功能、灵活性以及可扩展性著称。在Magento中,数据处理是一个核心组成...
### Magento - 开发与维护您的云项目 #### Magento 云平台简介 Magento Commerce Cloud 是一个为企业提供强大且灵活的电子商务解决方案的平台。该平台通过整合前端用户体验与后端管理工具来帮助商家创建个性化且...
Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。在进行Magento的二次开发时,你需要理解并掌握以下几个核心概念和技术: 1. **MVC架构**:Magento基于Model-View-Controller(MVC)设计模式...
标题:“Magento数据结构分析” 描述:“Magento数据字典”提供了对Magento系统中各种数据库表的深入理解,这对于理解和优化Magento的性能至关重要。 一、Magento数据结构解析 Magento是一款功能强大的电子商务...
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站点快速复制到另一个服务器,用于测试、备份或者创建一个新的独立站点。这个过程涉及到数据库的备份与还原、文件系统的复制以及配置的调整等多个步骤。 ...
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 需要的所有表,包括 `catalog_product_entity`(产品信息)、`sales_flat_order`(订单数据)、`customer_entity`(客户信息)等。 4. **加载样本数据**:除了基础架构,这个包可能还包含一些示例...
演示地址:http://olegnax.com/product/athlete-responsive-magento-theme/livepreview/ Athlete Magento主题,兼容Magento 1.9.x, 1.8.x, 1.7.x ,特点:完全自适应(响应式布局),自定义子主题,无限颜色,完全...