1

I am trying to create a global product attribute that is used for configurable product.

This is how I would do it using the admin backend:

enter image description here

With the following options (for example):

enter image description here

So, I tried to do the above using the SOAP API (v1) like this:

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

When I execute this script, I get the following error:

Invalid request parameters.

enter image description here

Any ideas what I am doing wrong here?

2

Here is the sample code. This may help you.

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

?>
  • 1
    Welcome to Magento SE. While this code might solve the problem, a big bunch of code without explanation does not really help to understand the problem. If you can point out, what was wrong in the original code, what you did differently and why, this answer gains a lot more value, also for future visitors. – Fabian Schmengler Sep 20 '15 at 20:19
  • Thanks Milan for providing this example.....I were searching for this & after visiting many links....I got this link & your answer. Just superb :) Thanks a lot :) ........... Yes fschmengler you are right.....let me give a very little explanation of this....actually for select type attributes we need to create the attribute first as usual but then programatically or through api you need to get options of that attribute of source magento & add them one by one through product_attribute.addOption api method. Thanks again Milan for helping us :) – KA9 Mar 29 '16 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.