php - 通过 SOAP API v1 在 magento 中创建新的 DropDown(选择)产品属性(带有选项)

标签 php magento soap

我正在尝试创建一个用于可配置产品的全局产品属性。<​​/p>

这就是我使用管理后端执行此操作的方法:

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

知道我在这里做错了什么吗?

最佳答案

这是示例代码。你可以尝试一下。

<?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);

?>

关于php - 通过 SOAP API v1 在 magento 中创建新的 DropDown(选择)产品属性(带有选项),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32679416/

相关文章:

magento - 禁用特定 block 的整页缓存

Magento 关联产品不会显示

php - 在codeigniter中将mysql查询转换为Active record

javascript - 我如何使用 javascript 和 php 在 $_GET 获取数组?

php - 以当前语言环境和货币格式化价格

c++ - 将 gSOAP 用于 VS 2003/C++ 访问具有 WS-Security 的 SOAP Web 服务?

rest - 谁需要 (SOAP) 可靠的消息传递?

php - 如何使用 native joomla 2.5 php 对象显示 Joomla 数据库表

生成格式错误的 XML 提要的 PHP DOM 文档

.net - 如何使用 SOAP 从经典 ASP 调用 .Net Web 服务方法