136 days to get to Reacticon (Netherlands)

Yireo - Trainings & extensions

Open main menu

Connecting to Magento with SOAP (part 3)

This tutorial is the third in a series of four to explain you how you can use SOAP to connect to Magento. In the first two tutorials we have explained the basics of SOAP and how to connect a SOAP-client to the Magento API. This time we will go a step further - we are going to modify data.

Changing data through the API

    In the previous tutorials we have learned how we can setup a SOAP client and use it retrieve data from the Magento API. Changing data is done in exactly the same way as fetching data. The difference is that the arguments now hold not only the indicator "what we want to change" but also the data that contains the change:

    $product_id = 166;
    $product_data = array( 'status' => 2 );
    $arguments = array( $product_id, $product_data );
    $boolean = $soap->call( $session_id, 'catalog_product.update', $arguments );

    The $product_data above only contains the value that needs to be changed - the "status" of the product is in this case set to "disabled". But it could well have been an exact copy of the $product-array of the previous chapter. For performance reasons it's better to include the data that actually need to be changed.

    Another example: Let's change the price of a certain product with ID 166 by setting a discount of 10% off the normal price between december 1st and december 31th of this year.

    First we fetch the product data from the Magento API - just like we did before:

    $product_id = 166; 
    $product = $soap->call( $session_id, 'catalog_product.info', $product_id );

    Next we initialize the input data for the method "setSpecialPrice()" in the Product API. The Magento Wiki gives us the right arguments, but not the syntax of these arguments. We actually just filled in the data in the Magento Admin Panel, saved the form and looked up the saved values in the HTML-source code. The price actually does not have to include 4 zeros in the end, but as this was the value found in Magento we just use the same syntax.

    $from_date = '01/12/'.date( 'Y' ); 
    $to_date = '31/12/'.date( 'Y' );
    $special_price = number_format($product['price'] * 0.9, 4);

    And finally we put these values in an argument-array and add make the appropriate API-call.

    $arguments = array( $product_id, special_price, $from_date, $to_date );
    $soap->call( $session_id, 'catalog_product.setSpecialPrice', $arguments );

    As you can see the general usage of the API is the same everytime. The difference is made by the arguments a certain API-method receives.

    Using lists and filters

      If we want to collect a list of products the Product API gives us the "list()" method:

      $products = $soap->call( $session_id, 'catalog_product.list' );

      This API-method has an extra argument $filters which allows for very cool possibilities. For instance the following adds a filter to show only products with the status set to 1, in other words products which are enabled:

      $filters = array( 'status' => 1 );
      $products = $soap->call( $session_id, 'catalog_product.list', array($filters) );

      But the $filters can be made more advanced as well. The following filters select all enabled products that are of type "simple" (so neither configurable products nor grouped products) which have a SKU starting with the letters "ma".

      $filters = array( 
      'status' => array( '=' => 1 ),
      'type_id' => array( '=' => 'simple' ),
      'sku' => array( 'like' => 'ma%' ),

      The filter-construct uses an array of WHERE-segments that are in the end added to a WHERE-statement within the database query. Each WHERE-segments has an array-name (for instance "status") and an array-value which contains again a name-value pair. The following name-value pairs can be used:



      SQL example


      integer or string

      `status` = 1


      integer or string

      `status` = 1


      integer or string

      `status` > 1


      integer or string

      `status` < 1


      integer or string

      `status` => 1


      integer or string

      `status` =< 1


      integer or string

      `status` != "2"


      integer or string

      `sku` LIKE "ma%"


      integer or string

      `name` NOT LIKE "%glass%"



      `product_id` IN ( 19, 20, 25 )



      `product_id` NOT IN ( 3, 44 )

      What's next

      So in this third tutorial you have learned how to modify data through the Magento API. There are still some more tips and tricks that will be reveiled in the fourth tutorial. What about caching? Can you make multiple calls at once? How do you use XML-RPC, if you're not a SOAP fan? Stay tuned.

      Posted on 26 April 2009

      Looking for a training in-house?

      Let's get to it!

      Do not miss out on what we say

      This will be the most interesting spam you've ever read

      We don't write too commercial stuff, we focus on the technology (which we love) and we regularly come up with innovative solutions. Via our newsletter, you can keep yourself up to date on all of this coolness. Subscribing only takes seconds.

      Sign up for the Yireo newsletter


      Legal information

      Other Yireo sites

      Get Social

      About Yireo