0

I want to update some tier prices of a product based on a calculation I run. Basically it is printing costs, depending on a price per page, compared to some fixed prices.

I am using the following code to retrieve the old tier prices, do my calculations and replace some prices with the new price:

# price for digital printing: 0,066 Euro/per page
define('PPP', 0.066);

# $id is set outside of this, it's a valid product ID
$product = Mage::getModel("catalog/product")->load($id);
$data = $product->getData();
$_tierPrices = Mage::getResourceModel('catalog/product_attribute_backend_tierprice')->loadPriceData($id, Mage::app()->getWebsite()->getId());

$dpp = $data['page_count'] * PPP;

$_tierPricesNew = array();

foreach ($_tierPrices as $price) {
    if ($price['price'] > $dpp) {
        $price['price'] = $dpp;
    }

    $price['website'] = '0';
    $price['customer_group_id'] = '0';

    $_tierPricesNew[] = $price;
}

echo '<pre>';
print_r($_tierPricesNew);

The result of this, the content of $_tierPricesNew, looks fine to me:

Array
(
    [0] => Array
        (
            [price_id] => 2342
            [website_id] => 0
            [all_groups] => 1
            [cust_group] => 0
            [price] => 26.4
            [price_qty] => 1.0000
            [website] => 0
            [customer_group_id] => 0
        )

    [1] => Array
        (
            [price_id] => 2343
            [website_id] => 0
            [all_groups] => 1
            [cust_group] => 0
            [price] => 26.4
            [price_qty] => 5.0000
            [website] => 0
            [customer_group_id] => 0
        )

[...]

I am not sure about the additional parameters "website" and "customer_group_id" but they will probably not hurt if present.

When trying to update the prices I run this code:

$soap = new SoapClient('https://magento.host/api/v2_soap/?wsdl=1');
$session = $soap->login('my_user', 'api_key');

$res = $soap->catalogProductAttributeTierPriceUpdate($session, $id, $price);

$soap->endSession($session);

This is basically the example from the documentation.

And while I can obtain a session ID, so login seems to work, but the result of $res is always empty.

Any ideas why this happens? Or how I can debug this?

0

Got this running now. I got rid of the API call and instead used a solution commonly found all over StackOverflow.

My call is now:

$product->setTierPrice($_tierPricesNew);
$product->save();

I found this to be working without having to run this first:

$product->unsTierPrice();
$product->save();
$product->load();

Maybe this is no problem for me because I just want to replace some existing prices and not delete (all) the others. So try for yourself if this works for you, too.

I also found something very important here. You might need to set:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

At least this was for me the point where it started to work although I did not change anything else to the code. Did not find this in the other code snippets I saw but it was important for my system.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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