Magento: Import Tier Pricing using API

Magento Icon

Yet another thing that would be nice if Magento allowed you to import through the admin interface. But, at least there is the API to utilize, and fortunately importing Tier Pricing for products is quite easy. I will provide you with a script and CSV format to get started quickly.

Important Note: Be sure that wherever you are running the script, you have SOAP installed/enabled. You can run it from anywhere that has it, as you are providing the script with a URL of a Magento installation. If your server doesn’t have it, you can install PHP and SOAP on your local machine and run it off of there if you’d like.

First thing you are going to need to do, if you haven’t done it already, is to setup an API user in your installation of Magento. Go to System >> Web Services >> Users and setup an account. Be sure that the role you give that user has access to tier pricing.

Next, create a PHP file where ever you want. I chose to put it in /api-scripts/ and I called it “tier-pricing-import.php”. Be sure to replace the four values I’ve highlighted with your own values:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
 
$proxy = new SoapClient('http://[YOUR-WEBSITE-URL]/api/soap/?wsdl');
$sessionId = $proxy->login('[YOUR-WEBSERVICE-USERNAME]', '[YOUR-WEBSERVICE-PASSWORD]');
 
$row = 1;
 
$handle = fopen('[YOUR-CSV-FILENAME].csv', 'r');
 
while (($data = fgetcsv($handle, 30, ',')) !== false) {
    if ($row != 1) {
        $tierPrices = '';
        $sku = $data[2];
 
        $tierPrices[] = array(
            'website'           => $data[0],
            'customer_group_id' => $data[1],
            'qty'               => $data[3],
            'price'             => $data[4]
        );
 
        try {
            $proxy->call($sessionId, 'product_tier_price.update', array($sku, $tierPrices));
        } catch (Exception $e) {
            $errors .= 'Error for SKU ' . $sku . ': ' . $e->getMessage() . "\n";
        }
    }
    $row++;
}

Finally, create your CSV. You’ll want 5 columns:

  • website_id: Integer value for your website id. Use ‘all’ for all websites
  • cust_group: Integer value for the customer group it will be applied to. Use ‘all’ to apply it to all groups
  • sku: The product sku that the tier pricing will be applied to
  • price_qty: The quantity the price is applied to (x and above)
  • price: The price

Be sure to include the header row. Here is an example of how the csv should look:

website_id cust_group sku price_qty price
1 3 XB60 10 25.99
1 3 XB60 50 20.99
all all RDX300 15 8

Just include the csv in the same directory as your php file, then in your browser, type in the URL of the php file and run it. It will import your tier pricing very quickly. I haven’t built in any sort of messages that let you know when it’s done, or if there were any errors or anything. This is as bare-bones as it gets, but it works as needed. I hope you find it useful.

This entry was posted in Uncategorized. Bookmark the permalink.

12 Responses to Magento: Import Tier Pricing using API

  1. Gianny says:

    Hi,
    the script works and I do not get errors, but why is inserted only one (the last) tier price?

    Example csv file is as follows:

    “website_id”,”cust_group”,”sku”,”price_qty”,”price”
    all,all,BVPF03,10,10.99
    all,all,BVPF03,20,25.99
    all,all,BVPF03,30,35.99
    all,all,BVPF03,40,45.99
    all,all,BVPF03,50,55.99

    but is inserted only: “all,all,BVPF03,50,55.99”

    Thank you!

  2. Mathis says:

    If someone looking for alternate method for API, I can recommend import using Store Manager for Magento, it save us loot of time when we start, it make bulk update stock level and status for huge amount of products. SM for Magento offers you flexible mechanism of tier price arranging. You won’t face any difficulty when adding discounted price to some products in your catalog or accomplishing Magento update price operation. Find out more about it in article

  3. Russ says:

    I am not able to get this to work. I do have a list of 35k wholesale products but have only entered about 15 into the retail magento. I can’t see that any imported. Do I need to have only the list that has been entered? Will this ignore or stop when it finds a SKU that hasn’t been entered? Please advise.

  4. Stephane says:

    @chris Luke & Bruce, I ran into the same problem with only the last tier price being updated. Commenting out the variable $tierPrices = ”; declaration in the while statement will fix this :
    // $tierPrices = ”;
    $sku = $data[2];
    $tierPrices[] = array(
    ‘website’=> $data[0],
    ‘customer_group_id’=> $data[1],
    ‘qty’=> $data[3],
    ‘price’=> $data[4]
    );

    However I feel the API solution is very, very slow. I have a such a large product base to deal with (+20K products each with tier pricing), that need to be updated several times a day via cron it might not work for me …

    Hope this will help someone
    Cheers

  5. sha says:

    working but it only gets the last tier price

  6. sha says:

    Managed to do it..
    THANK YOU SO MUCH…

  7. Bruce says:

    ok so I’ve now been running it as root via ssh (to hopefully rule out timeout issues, no idea if that helps though!) and watching the db closely as the script runs.

    what it looks like is each tier is overwriting the last. so in my case I have 4 tiers for each product, the script runs perfectly but I only end up with the 4th tier in the db when it is done. which makes sense as to why I was only getting 50 out of my 200 added.

    as I mentioned I’m a bit of a noob on this stuff but I think I’ve ruled out everything I know to try to rule out.

    any help you can provide would be greatly appreciated as it is so close to doing exactly what I need

  8. Luke says:

    i’ve this problem too.

    I can import only the last tier :(

    i’ve also a problem with the CSV file. I created it like:

    “website_id”,”cust_group”,”sku”,”price_qty”,”price” (and i also tried with “, ‘ or nothing) but i received a lot of error , like:

    Notice: Undefined offset: 2 in /home/test/public_html/site/scripts/price.php on line 15

    Notice: Undefined offset: 3 in /home/test/public_html/site/scripts/price.php on line 20

    Notice: Undefined offset: 4 in /home/test/public_html/site/scripts/price.php on line 21

    Notice: Undefined variable: errors in /home/test/public_html/site/scripts/price.php on line 27

    It’s strange couse the script works and in the first two times that i’ve running it i haven’t see this errors.

    Please, help :(

  9. Chris says:

    ok so I’ve now been running it as root via ssh (to hopefully rule out timeout issues, no idea if that helps though!) and watching the db closely as the script runs.

    what it looks like is each tier is overwriting the last. so in my case I have 4 tiers for each product, the script runs perfectly but I only end up with the 4th tier in the db when it is done. which makes sense as to why I was only getting 50 out of my 200 added.

    as I mentioned I’m a bit of a noob on this stuff but I think I’ve ruled out everything I know to try to rule out.

    any help you can provide would be greatly appreciated as it is so close to doing exactly what I need

  10. Chris says:

    this sounds like just what I need!

    I’m pretty noobish with this stuff though — would a good process be to import the products with normal pricing and then use the script to update the tiers?

    or can I add fields to this and import everything at once?

    any thoughts on process would be very helpful, thanks

    • Josh Pratt says:

      Chris,

      Yes, this script requires you have have the products already in Magento. You can’t import everything at once. You’ll have to first import the products, then the tier pricing.