4

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.

4
  • as expected means ? any error ?
    – Keyur Shah
    Commented May 18, 2015 at 6:19
  • no.. no errors.. the product is created but without the tier prices
    – DB-Admin
    Commented May 18, 2015 at 6:28
  • ca you post your full code here ?
    – Keyur Shah
    Commented May 18, 2015 at 6:29
  • I have posted the full code
    – DB-Admin
    Commented May 18, 2015 at 6:50

3 Answers 3

5

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();
2
  • this I have tried, but didn't work for me.
    – DB-Admin
    Commented May 18, 2015 at 6:38
  • What is this "all_groups" that you are giving ? I removed it, and tried adding a product, and it worked. See my answer for a working code.
    – hellimac
    Commented May 18, 2015 at 7:16
4

I placed an answer to a similar question on the main Stack site, but to avoid a link answer I'll copy it here too. The most efficient,non-direct-SQL method I found was to use Magento's Tier Price API (Magento 1.X) as follows:

// Build an array of tier level objects
foreach ($tierLevels as $tierLevel) {
    $tiers[] = (object) array(
        'website' => 'all',
        'customer_group_id' => 'all',
        'qty' => $tierLevel['min_qty'],
        'price' => $tierLevel['unit_price']
    );
}

// Use the API to do the update
try {
    $tierPriceApi = Mage::getSingleton('catalog/product_attribute_tierprice_api_v2');
    $tierPriceApi->update($productId, $tiers);
}
catch (Exception $e) {
    // Handle the exception
}
0

Depending your needs, here's my solution to update tier price without loading every product. To avoid exception while saving the product, you'll need to set the orig_data of the product before.

$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('tier_price')->addAttributeToFilter('entity_id', array('eq'=> 1));

    $productsIds = array();

    foreach ($collection->getItems() as $product) {
        $productIds[] = $product->getId();
        $tierPrices[$product->getId()] = array();
    }

    $adapter   = $collection->getConnection();
    $columns   = array(
        'price_id'      => 'value_id',
        'website_id'    => 'website_id',
        'all_groups'    => 'all_groups',
        'cust_group'    => 'customer_group_id',
        'price_qty'     => 'qty',
        'price'         => 'value',
        'product_id'    => 'entity_id'
    );

    $select  = $adapter->select()
        ->from($collection->getTable('catalog/product_attribute_tier_price'), $columns)
        ->where('entity_id IN(?)', $productIds)
        ->order(array('entity_id','qty'));

    foreach ($adapter->fetchAll($select) as $row) {
        $tierPrices[$row['product_id']][] = $row;
    }        


    foreach ($collection->getItems() as $product) {

        $origTierPrice = $tierPrices[$product->getId()];
        unset($origTierPrice['product_id']);
        $product->setOrigData('tier_price', $origTierPrice);

        $product->setTierPrice(array(
            array(
                'website_id'    => '0',
                'cust_group'    => '4',
                'price_qty'     => '24',
                'price'         => '2',
                'delete'    => ''
            )        
        ));

        $product->save();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.