Magento Image Information via SOAP API

Here’s a little snippet that’ll dump out image information for … well, as written here, every active SKU in your Magento Installation.

The reason for the for loop breaking SKUs down based on the first letter is to keep from blowing up my PHP memory with too many products. Your mileage may vary.

try {
    $proxy = new SoapClient('http://magento.store.url/index.php/api/?wsdl');
    $sid = $proxy->login('username', '********');
}
catch( Exception $e ) {
    echo "Can't establish connection: " . $e->getMessage() . "\n";
    exit(-3);
}
 
for( $cat = 'A', $i=0; $i < 26; $i++, $cat++ ) {     $filter = array(         'status' => array( '='=>'1' ),
        'sku' => array( 'like' => $cat.'%' )
    );
    echo $cat.".";
    try {
        $productList = $proxy->call( $sid, 'product.list', array($filter) );
    }
    catch( Exception $e ) {
        echo "Can't retrieve product listing: " . $e->getMessage() . "\n";
        exit(-4);
    }
    foreach( $productList as $prodInfo ) {
        echo $prodInfo['sku'] . "\t" . $prodInfo['name'] . "\n";
        $media = $proxy->call($sid, 'product_media.list', $prodInfo['sku']);
        print_r($media);
    }
}
exit(0);

Magento API: Select Orders by Date Range

Here’s snippet of code I was able to use to get all of yesterday’s orders. You can see more variations for the use of DateTime here. I also wrote a front end to allow a selection of dates, but you can figure that out. This was the hard part to me. How the heck do I pass the from and to dates to Magento’s sales_order.list call?

Also note that Magento internally store dates and times in UTC. So I have to convert to that offset to get my orders in my local time. time_local_to_utc is a little function I wrote to do that. Conversely, time_utc_to_local converts it back. (Code later in this post).

Remember, much of the code I publish here is very specific for a particular implementation. This code is designed to be able to pull back all payment information into a spreadsheet for an accounting department. This is not a generic solution to a generic Magento problem. Rather, I intended this to help other folks get a start on pulling back orders by date!

$day = new DateTime();
$day->modify( "-1 day" );
 
$fromDate = $day->format("Y-m-d 00:00:00");
$toDate = $day->format("Y-m-d 23:59:59" );
 
$utc_from=time_local_to_utc($fromDate);
$utc_to=time_local_to_utc($toDate);
 
$proxy = new SoapClient('YOURSITE/api/soap/?wsdl');
$sessionId = $proxy->login('YUOUR_LOGIN', 'YOUR_PASS');
$order_headers = $proxy->call($sessionId, 'sales_order.list',
        array(
            array(
                'created_at'=>
                    array(
                        'from'=>$utc_from,
                        'to'=>$utc_to
                    )
                )
            )
        );
$howManyOrders = count($order_headers);

Here are the 2 time functions. They could certainly be made more general and really should be refactored (pass in “to” and “from” zones…), but you get the idea. And maybe there’s something to be said for the inline main code being simple to read.

function
time_local_to_utc($date) {
    $dt = new DateTime($date, new DateTimeZone("America/New_York"));
    $dt->setTimezone(new DateTimeZone("UTC"));
    return( $dt->format("Y-m-d H:i:s") );
}
 
function
time_utc_to_local($date) {
    $dt = new DateTime($date, new DateTimeZone("UTC"));
    $dt->setTimezone(new DateTimeZone("America/New_York"));
    return( $dt->format("Y-m-d H:i:s") );
}

Magento Product Create API: use_config_min_sale_qty (and max) attributes

In a site I maintain I have a separate web application for adding products. The reason for this is that products need to be added to both a backend warehousing system as well as magento.

I’m posting this because the documentation on the magento site isn’t clear about how one goes about setting these attributes:

min_sale_qty
use_config_min_sale_qty
use_config_max_sale_qty
use_config_manage_stock

Although these attributes are exported to a csv file via Admin->Profiles, they are not so simply assigned via the API. Here’s the snippet given as an example on magento’s site:

1
2
3
4
5
6
7
8
9
10
11
$newProductData = array(
    'name'              => 'name of product',
    'websites'          => array(1), // array(1,2,3,...)
    'short_description' => 'short description',
    'description'       => 'description',
    'price'             => 12.05
);
 
// Create new product
$proxy->call($sessionId, 'product.create', array('simple', $set['set_id'],
     'sku_of_product', $newProductData));

What I cannot do is set min_sale_qty, use_config_min_sale_qty etc in the $newProductData array – that is, this will not work:

1
2
3
4
5
6
7
8
9
10
11
$newProductData = array(
    'name'              => 'name of product',
    'websites'          => array(1), // array(1,2,3,...)
    'short_description' => 'short description',
    'description'       => 'description',
    'price'             => 12.05,
    'min_sale_qty'            => 1,
    'use_config_min_sale_qty' => 1,
    'use_config_max_sale_qty' => 1,
    'use_config_manage_stock' => 1
);

Rather, these attributes must be set within a double secret probation array called stock_data! I divined this from this page on magento’s forum. I wrote my code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$stock_data = array (
    'min_sale_qty'          => 1,
    'use_config_min_sale_qty' => 1,
    'use_config_max_sale_qty' => 1,
    'use_config_manage_stock' => 1
);
 
$newProductData = array(
    'name'              => 'name of product',
    'websites'          => array(1), // array(1,2,3,...)
    'short_description' => 'short description',
    'description'       => 'description',
    'price'             => 12.05,
    'stock_data'         => $stock_data
);
 
$proxy->call($sessionId, 'product.create', array('simple', $set['set_id'],
     'sku_of_product', $newProductData));

For the record, I chose to separately declare $stock_data to make the code easier to read and understand. I’m not a big fan of array with array within array etc. Gets too clunky.