![]()
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 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
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.
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
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
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
Managed to do it..
THANK YOU SO MUCH…
working but it only gets the last tier price
@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