Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update all Tier prices for a product

Without directly querying the Magento database. How can i remove all the tier prices for a certain product based on quantity and customer group?

Then add new tier prices for this product.

Example:
Remove all tier prices for a product with the SKU: FD001
where the customer group id is: 4

PHP 5.3 Magento 1.4.2

like image 722
Chris Whittington Avatar asked Dec 13 '11 16:12

Chris Whittington


2 Answers

You first have to unset the tier prices and save the product, then add the tier price(s) and save again.

Mage::getModel("catalog/product")->load(123)
    ->unsTierPrice()
    ->save()
    ->setTierPrice(array(
        array(
            'website_id'    => 0,
            'all_groups'    => 0,
            'cust_group'    => 4,
            'price'         => 99.99,
            'price_qty'     => 1
        ),
        array() // another tier, etc
    ))
    ->save();
like image 175
Steve Robbins Avatar answered Nov 05 '22 00:11

Steve Robbins


I ended up solving this by using direct database queries.

As always I'm looking for a better answer.

My Hacky solution:

$product = Mage::getModel('catalog/product')->load(9999);
$dbc = Mage::getSingleton('core/resource')->getConnection('core_write');
$dbc->query('DELETE FROM `catalog_product_entity_tier_price` WHERE `entity_id` = ' . intval($product->getId()) . ' AND `customer_group_id` IN(3,4,6,7) AND qty = 1');
$dbc->query(
    'INSERT INTO `catalog_product_entity_tier_price` (`entity_id`,`all_groups`,`customer_group_id`,`qty`,`value`,`website_id`)
    VALUES ('. intval($product->getId()) . ',0,' . intval($id) . ',1,' . floatval($price) . ',0)'
    );
like image 31
Chris Whittington Avatar answered Nov 05 '22 00:11

Chris Whittington