0

I am working on Source Magento to Destination Magento migration. I have created an attribute like other attributes so that is not the issue but I am having a big issue or say I don't know how to do....that....if drop down type attribute is already there in destination magento then how to know that which options are missing & insert those options only.

I got a very nice solution in Milan's answer here..... https://stackoverflow.com/questions/32679416/create-a-new-dropdown-select-product-attribute-with-options-in-magento-via-s

What he is doing...creating attribute first & then looping through the options & one by one inserting options in that attribute but here comes the point....that if this attribute is already there in destination magento then how to find that....these....these....options are available in destination magento & we only need to insert these....these....options.

Here is what I am doing to check whether the attribute is already there in destination magento or not....

$check_magento_attribute_already_available_ornot = $dProxy->call($dSessionid,'product_attribute.info',$attribute_code);

This will give true or error & according to that I create or update the attribute.

So finally the questions are 2 :- 1) How to check if options...already there in destination magento's attribute using soap api 2) How to insert that drop down type attribute's value in product at the time of product creation or updation? - Whether to use multi_data or single_data?

Thanks.

1

Try bellow code

<?php 
require_once('app/Mage.php');
Mage::app();
class APIS 
{
    public function index()
    {   

        try {
            $optionLable = 'Red';
            $optionValue = '';
            $client = new SoapClient('http://magento.com/index.php/api/?wsdl'); // api url    
            $sessionId = $client->login('api_user', 'api_key'); // API user name & key 

            // get attribute options 

            $attributeId = 'color'; // Existing selectable attribute ID OR Code
            $options = $client->call(
                $sessionId,
                "product_attribute.options",
                array($attributeId)
            );      

            foreach($options as $option) {
                if(isset($option['label']) && $option['label']) {
                    if($optionLable==$option['label']) {
                        $optionValue = $option['value'];
                    }
                }
            }
            if(!$optionValue) {

                // add attribute options
                $optionToAdd = array(
                    "label" => array(
                        array(
                            "store_id" => 0,
                            "value" => $optionLable
                            )
                    ),
                    "order" => 0,
                    "is_default" => 0
                );
                $addOption = $client->call(
                    $sessionId,
                    "product_attribute.addOption",
                    array(
                         $attributeId,
                         $optionToAdd
                    )
                );
                if($addOption) {
                    // get attribute options 
                    $options = $client->call(
                        $sessionId,
                        "product_attribute.options",
                        array($attributeId)
                    );      
                    foreach($options as $option) {
                        if(isset($option['label']) && $option['label']) {
                            if($optionLable==$option['label']) {
                                $optionValue = $option['value'];
                            }
                        }
                    }   
                }
            }   
            $optionValue;

            // get attribute set
            $attributeSets = $client->call($sessionId, 'product_attribute_set.list');
            $attributeSet = current($attributeSets);

            // create product

            $result = $client->call($sessionId, 'catalog_product.create', array('simple', $attributeSet['set_id'], 'product_sku', array(
                'categories' => array(2),
                'websites' => array(1),
                'name' => 'Product name',
                'description' => 'Product description',
                'short_description' => 'Product short description',
                'color' => $optionValue,
                'weight' => '10',
                'status' => '1',
                'url_key' => 'product-url-key',
                'url_path' => 'product-url-path',
                'visibility' => '4',
                'price' => '100',
                'tax_class_id' => 1,
                'meta_title' => 'Product meta title',
                'meta_keyword' => 'Product meta keyword',
                'meta_description' => 'Product meta description'
            )));

            // update product

            $result = $client->call($session, 'catalog_product.update', array('product_sku', array(
                'categories' => array(2),
                'websites' => array(1),
                'name' => 'Product name new 2',
                'color' => $optionValue,
                'description' => 'Product description',
                'short_description' => 'Product short description',
                'weight' => '10',
                'status' => '1',
                'url_key' => 'product-url-key',
                'url_path' => 'product-url-path',
                'visibility' => '4',
                'price' => '100',
                'tax_class_id' => 1,
                'meta_title' => 'Product meta title',
                'meta_keyword' => 'Product meta keyword',
                'meta_description' => 'Product meta description'
            )));
        }
        catch (Exception $e) {
           echo $e->getMessage();
        }
        print_r($result);

    }


}

$obj = new APIS();
$obj->index();
?>  

Note: We have tried above code with attribute code color for product create & update for only soap api v1. If you are need soap v2 then follow link

  • Hi Abdul, As previously, Many thanks for this :) I am going to try this code & will give you update in an hour. I quickly gone through your given code & seen these lines :- // get attribute set $attributeSets = $client->call($sessionId, 'product_attribute_set.list'); $attributeSet = current($attributeSets); Umm, can you please tell, what is the usage of that? - I got the thing that you are getting attribute set list but can you please tell what this line does? - $attributeSet = current($attributeSets); Once again many thanks for your code :) You the great :) – KA9 Mar 30 '16 at 7:56
  • 1
    // get attribute set $attributeSets = $client->call($sessionId, 'product_attribute_set.list'); $attributeSet = current($attributeSets); Allows you to retrieve the list of product attribute sets. – Abdul Mar 30 '16 at 8:52
  • 1
    or you will send manually pass attribute set id like 4 – Abdul Mar 30 '16 at 8:55
  • 1
    make sure attribute set id is required to create new product if you are not send then got error – Abdul Mar 30 '16 at 8:58
  • 1
    and above code get current attribute set for the product. see here devdocs.magento.com/guides/m1x/api/soap/catalog/catalogProduct/… – Abdul Mar 30 '16 at 8:59

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.