Magento 1.9 – How to Programmatically Add Tier Prices

magento-1.9productstierprice

I have a custom module in my magento website where I add products programmatically. I have the requirement to add tier prices to products and I'm using the following code for that.

$tierPrices = array(
                    'all_groups'    => '0',
                    'cust_group'    => '2',
                    'price_qty' => '2',
                    'price'   => $post['tprice'],
                    'website_id' => '1'
                 );
$product->setTierPrice($tierPrices);

This doesn't add tier prices as expected.

Edit

$product = Mage::getModel('catalog/product');               
$product->setWebsiteIds(array(1)) 
        ->setAttributeSetId(14) 
        ->setTypeId('simple')
        ->setWeight(0.50)
        ->setStatus(1)
        ->setTaxClassId(2)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE) 
        ->setStockData(array(
                             'use_config_manage_stock' => 0, 
                             'manage_stock'=>1, 
                             'min_sale_qty'=>1, 
                             'is_in_stock' => 1, 
                             'qty' => 10000 
                           )
                       )
        ->setCategoryIds(array(31));


        $sku =  strtolower($location)."-".str_replace(" ","-",strtolower($data))."-".$i;
        $product->setSku($sku)
                ->setName('test product')                   
                ->setPrice($post['price']) 
                ->setDescription('Test Description')
                ->setShortDescription('description');

  $tierPrices = array(
                'all_groups'    => '0',
                'cust_group'    => '2',
                'price_qty' => '2',
                'price'   => $post['tprice'],
                'website_id' => '1'
             );
  $product->setTierPrice($tierPrices);

  $product->save();

A little help on this is highly appreciated.

Best Answer

You can add several tier prices for one product, so you should provide an array of objects (or arrays) to the product.

I didn't try, but I would add an array inside your array, like that :

$tierPrices = array(
                ['cust_group'    => '2',
                'price_qty' => '2',
                'price'   => $post['tprice'],
                'website_id' => '1']
             );
$product->setTierPrice($tierPrices);

EDIT

Here is a working installer code, that created me a test product with the good tier prices. I removed the index "all_groups" from the tierPrices array, because I didn't see it in my Magento. Is it an error ? A custom module ?

<?php

$installer = $this;

Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(1);

$product = Mage::getModel('catalog/product');
$product->setAttributeSetId(4)
    ->setTypeId('simple')
    ->setWeight(0.50)
    ->setStatus(1)
    ->setTaxClassId(2)
    ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);


$sku = strtolower("SKU");
$product->setSku($sku)
    ->setName('test product')
    ->setPrice(12)
    ->setDescription('Test Description')
    ->setShortDescription('description');

$tierPrices = array(
['cust_group' => '2',
    'price_qty' => '2',
    'price' => 11,
    'website_id' => '0']
);
$product->setTierPrice($tierPrices);
$product->save();

$installer->endSetup();
Related Topic