Creating Custom Product Parameters Using the Magento API

How to add custom parameters to the product, as you can in the backend using the API.

I use C #, but if you know how to do this in Php, it will be useful too.

I noticed that the product has the following:

var product = new catalogProductCreateEntity(); product.options_container = "blah"; 

And there is this:

 catalogAttributeOptionEntity optionEntity = new catalogAttributeOptionEntity(); optionEntity.value = "sds"; optionEntity.label = "ere"; 

But I don’t see a way to use them, I’m not sure how to create a container, and the AttributeOptionEntity directory does not have all the properties necessary to create a custom option.

+4
source share
3 answers

Look at the admin product controller. Yes it is possible.

 /** * 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']); } 
+2
source

This is not documented anywhere (otherwise), but at least in Magento 1.6 you can find the appropriate API methods for the product parameters in the source code. (I do not know with which version this function exists.)

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> 

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 -array is also a bit more complicated, as it partially depends on the option type selected. The base $ data array looks like this:

 $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 always conatin at least one row, with at least price , price_type and sku columns. Depending on the type, additional fields may be added (maf: ...). Types in the select group can have more than one line specified in additional_fields . Custom Types / Group Types:

  • text (maf: 'max_characters' )
    • field
    • region
  • file (maf: 'file_extension', 'image_size_x', 'image_size_y' )
    • file
  • select (maf: 'value_id', 'title', 'sort_order' )
    • DROP_DOWN
    • radio
    • check box
    • plural
  • the date
    • the date
    • date_time
    • Time

Examples for complete 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, ), ), ); 
+2
source

In the end, I decided that it was impossible to do this through the API and directly go to the database.

0
source

Source: https://habr.com/ru/post/1285951/


All Articles