如何使用API为后端添加自定义选项。
我正在使用C#,但如果你知道如何在Php中做到这一点,那也会有所帮助。
我注意到该产品有这个:
var product = new catalogProductCreateEntity();
product.options_container = "blah";
有这个:
catalogAttributeOptionEntity optionEntity = new catalogAttributeOptionEntity();
optionEntity.value = "sds";
optionEntity.label = "ere";
但我无法看到使用它们的方法,我不知道如何制作容器,而catalogAttributeOptionEntity没有制作自定义选项所需的所有属性。
答案 0 :(得分:2)
查看管理产品控制器。是的,这是可能的。
/**
* Initialize data for configurable product
*/
if (($data = $this->getRequest()->getPost('configurable_products_data')) && !$product->getConfigurableReadonly()) {
$product->setConfigurableProductsData(Zend_Json::decode($data));
}
if (($data = $this->getRequest()->getPost('configurable_attributes_data')) && !$product->getConfigurableReadonly()) {
$product->setConfigurableAttributesData(Zend_Json::decode($data));
}
$product->setCanSaveConfigurableAttributes((bool)$this->getRequest()->getPost('affect_configurable_product_attributes') && !$product->getConfigurableReadonly());
/**
* Initialize product options
*/
if (isset($productData['options']) && !$product->getOptionsReadonly()) {
$product->setProductOptions($productData['options']);
}
答案 1 :(得分:2)
这在任何地方都没有记录(否则),但至少在Magento 1.6中,可以在源代码中找到适用于产品选项的API方法。 (我不知道该功能存在哪个版本。)
API本身定义于: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
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看起来像:
$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
中指定了多个行。自定义选项类型/类型组是:
'max_characters'
)
'file_extension', 'image_size_x', 'image_size_y'
)
'value_id', 'title', 'sort_order'
)
完整选项数据数组的示例:
// 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,
),
),
);
答案 2 :(得分:0)
最后我决定不能通过API完成并直接进入数据库。