使用Magento API在产品上创建自定义选项 [英] Creating Custom Options on a Product using the Magento API
问题描述
如何使用API向后端中的产品添加自定义选项.
How do I add custom options to a product like you can in the backend, using the API.
我正在使用C#,但是如果您知道如何在Php中做到这一点,那也将有所帮助.
Im using C# but if you know how do to this in Php, that would be helpful too.
我注意到该产品具有以下功能:
I noticed that product has this:
var product = new catalogProductCreateEntity();
product.options_container = "blah";
有这个:
catalogAttributeOptionEntity optionEntity = new catalogAttributeOptionEntity();
optionEntity.value = "sds";
optionEntity.label = "ere";
但是我看不到一种利用它们的方法,我不确定如何制作一个容器,并且catalogAttributeOptionEntity不具有制作自定义选项所需的所有属性.
But I cant see a way of utilizing them, im not sure how to make a container, and the catalogAttributeOptionEntity does not have all the properties needed to make a custom option.
推荐答案
在任何地方(其他地方)都没有记录,但是至少在Magento 1.6中,人们可以在源代码中找到适用于产品选项的API方法. (我不知道该功能存在哪个版本.)
This is not documented anywhere (else), but at least in Magento 1.6 one can find the appropriate API methods for product options in the source code. (I don 't know since which version that feature exists.)
API本身定义在:app/code/core/Mage/Catalog/etc/api.xml
The API itself is defined in: app/code/core/Mage/Catalog/etc/api.xml
<catalog_product_custom_option translate="title" module="catalog">
<title>Catalog product custom options API</title>
<model>catalog/product_option_api</model>
<acl>catalog/product/option</acl>
<methods>
<add translate="title" module="catalog">
<title>Add new custom option into product</title>
<acl>catalog/product/option/add</acl>
</add>
<update translate="title" module="catalog">
<title>Update custom option of product</title>
<acl>catalog/product/option/update</acl>
</update>
<types translate="title" module="catalog">
<title>Get list of available custom option types</title>
<acl>catalog/product/option/types</acl>
</types>
<info translate="title" module="catalog">
<title>Get full information about custom option in product</title>
<acl>catalog/product/option/info</acl>
</info>
<list translate="title" module="catalog">
<title>Retrieve list of product custom options</title>
<acl>catalog/product/option/list</acl>
<method>items</method>
</list>
<remove translate="title" module="catalog">
<title>Remove custom option</title>
<acl>catalog/product/option/remove</acl>
</remove>
</methods>
</catalog_product_custom_option>
被调用的函数在以下位置定义:app/code/core/Mage/Catalog/Model/Product/Option/Api.php
The called functions are defined in: app/code/core/Mage/Catalog/Model/Product/Option/Api.php
class Mage_Catalog_Model_Product_Option_Api extends Mage_Catalog_Model_Api_Resource
{
/**
* Add custom option to product
*
* @param string $productId
* @param array $data
* @param int|string|null $store
* @return bool $isAdded
*/
public function add( $productId, $data, $store = null )
/**
* Update product custom option data
*
* @param string $optionId
* @param array $data
* @param int|string|null $store
* @return bool
*/
public function update( $optionId, $data, $store = null )
/**
* Read list of possible custom option types from module config
*
* @return array
*/
public function types()
/**
* Get full information about custom option in product
*
* @param int|string $optionId
* @param int|string|null $store
* @return array
*/
public function info( $optionId, $store = null )
/**
* Retrieve list of product custom options
*
* @param string $productId
* @param int|string|null $store
* @return array
*/
public function items( $productId, $store = null )
/**
* Remove product custom option
*
* @param string $optionId
* @return boolean
*/
public function remove( $optionId )
/**
* Check is type in allowed set
*
* @param string $type
* @return bool
*/
protected function _isTypeAllowed( $type )
}
$data
数组也有些棘手,因为它的键部分取决于所选的选项类型.基本的$ data-array看起来像:
The $data
-array also is a bit tricky, since it's keys partly depend on the selected option type. The basic $data-array looks like:
$data = array (
'is_delete' => 0,
'title' => 'Custom Option Label',
'type' => 'text',
'is_require' => 0,
'sort_order' => 1,
'additional_fields' => array (
0 => array (
'price' => '10.0000',
'price_type' => 'fixed', // 'fixed' or 'percent'
'sku' => '',
),
),
);
additional_fields
总是至少与price
,price_type
和sku
列构成至少一行.根据类型,可能会添加更多其他字段(maf:…).组select
中的类型可能有多个在additional_fields
中指定的行.自定义选项类型/类型组为:
The additional_fields
always conatin at least one row with at least the columns price
, price_type
and sku
. More additional fields (maf: …) may be added depending on the type. The types in the group select
may have more than one row specified in additional_fields
. The custom option types/type-groups are:
- 文本(maf:
'max_characters'
)- 字段
- 区域
- 文件
- drop_down
- 收音机
- 复选框
- 多个
- 日期
- date_time
- 时间
完整的选项数据数组的示例:
Examples for complete option data arrays:
// type-group: select, type: checkbox $data = array ( 'is_delete' => 0, 'title' => 'extra Option for that product', 'type' => 'checkbox', 'is_require' => 0, 'sort_order' => 1, 'additional_fields' => array ( 0 => array ( 'value_id' => '3', 'title' => 'Yes', 'price' => 10.00, 'price_type' => 'fixed', 'sku' => NULL, 'sort_order' => 1, ), 1 => array ( 'value_id' => 3, 'title' => 'No', 'price' => 0.00, 'price_type' => 'fixed', 'sku' => NULL, 'sort_order' => 2, ), ), ); // type-group: text, type: field $data = array ( 'is_delete' => 0, 'title' => 'Custom Option Label', 'type' => 'text', 'is_require' => 0, 'sort_order' => 1, 'additional_fields' => array ( 0 => array ( 'price' => 10.00, 'price_type' => 'fixed', 'sku' => NULL, 'max_characters' => 150, ), ), );
这篇关于使用Magento API在产品上创建自定义选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!