2015-09-20 52 views
2

我試圖創建用於可配置產品的全局產品屬性。通過SOAP API在magento中創建新的DropDown(選項)產品屬性v1

這是我將如何使用管理後臺做:

enter image description here

用下面的選項(例如):

enter image description here

所以,我試圖做的以上使用SOAP API(v1)如下:

$client = new SoapClient('http://domain.com/api/soap?wsdl'); 
$session = $client->login('apiUser', 'apiPass'); 

$attributeData = [ 
    'attribute_code' => 'test', 
    'scope' => 'global', 
    'frontend_input' => 'select', 
    'options' => [ 
     'values' => [ 
      0 => 'Red', 
      1 => 'Green', 
      2 => 'Blue' 
     ] 
    ], 
    'default_value' => '', 
    'is_configurable' => 1, 
    'used_in_product_listing' => 1, 
    'is_visible_on_front' => 0, 
    'apply_to' => '', 
    'is_comparable' => 0, 
    'is_used_for_promo_rules' => 0, 
    'is_required' => 0, 
    'is_unique' => 0, 
    'is_searchable' => 0, 
    'is_visible_in_advanced_search' => 0, 
    'frontend_label' => [[ 
     'store_id' => 0, 
     'label' => 'Test' 
    ]] 
]; 

try 
{ 
    $result = $client->call($session, 'product_attribute.create', $attributeData); 
    var_dump($result); 
} 
catch (SoapFault $sf) 
{ 
    var_dump($sf); 
} 

$client->endSession($session); 

當我執行此腳本時,出現以下錯誤:

無效的請求參數。

enter image description here

任何想法,我做錯了什麼?

回答

2

以下是示例代碼。你可以試試。

<?php 

$user = 'user'; 
$password = '123456789'; 

$client = new SoapClient('http://domain.com/api/soap/?wsdl'); 

$session = $client->login($user, $password); 

// Create new attribute 
$attributeToCreate = array(
    "attribute_code" => "test_attribute", 
    "scope" => "global", 
    "frontend_input" => "select", 
    "is_unique" => 0, 
    "is_required" => 0, 
    "is_configurable" => 1, 
    "is_searchable" => 0, 
    "is_visible_in_advanced_search" => 0, 
    "used_in_product_listing" => 0, 
    "additional_fields" => array(
     "is_filterable" => 1, 
     "is_filterable_in_search" => 1, 
     "position" => 1, 
     "used_for_sort_by" => 1 
    ), 
    "frontend_label" => array(
     array(
      "store_id" => 0, 
      "label" => "A test attribute" 
     ) 
    ) 
); 
$attributeId = $client->call(
    $session, 
    "product_attribute.create", 
    array(
     $attributeToCreate 
    ) 
); 

// add options 
$attributeCode = $attributeToCreate['attribute_code']; 
$selectOptions = array('Value 1','Value 2','Value 3','Value 4'); 
foreach ($selectOptions as $opt) { 
    $client->call(
     $session, 
     "product_attribute.addOption", 
     array(
      $attributeCode, 
      array(
       "label" => array(
        array(
         "store_id" => 0, 
         "value" => $opt 
        ) 
       ), 
       "order" => 0, 
       "is_default" => 0 
      ) 
     ) 
    ); 
} 

// add attribute to a attribute set 
$setId = 4; //attribute set id 
$result = $client->call(
    $session, 
    "product_attribute_set.attributeAdd", 
    array(
     $attributeId, // created attribute id 
     $setId 
    ) 
); 

var_dump($attributeId); 

$client->endSession($session); 

?> 
相關問題