0

UPDATE

My Magento installation was not happy with me trying out all kinds of stuff, so I restarted the server and I got this to work.

$groupProperties[] = [
    'website_id' => '0',
    'cust_group' => $group->getId(),
    'price' => $itemPrice
];

$product->setGroupPrice($groupProperties);
$product->save();

I will test later if the answer provided by Paj has a performance gain.

Original post

I have now searched the whole internet thin and I'm about to go crazy. I can not seem to find any good documentation (please link if you know any good sites)

I have an array with custom price group names, the prices for the user added to this price group and a SKU for finding the product in Magento.

I should probably say, that it's an import from Microsoft Navision, so I don't have the id of the product available, only the SKU.

The array structure:

$prices[$sku][$customGroupName] = $priceForOneItem;

Ex.: $prices['T5329786']['NAV-568245'] = '120.00';

I have tried this:

foreach($prices as $sku => $groupAndPrice){

    $catalog = Mage::getModel('catalog/product');
    $productId = $catalog->getIdBySku($sku);
    $product = Mage::getModel('catalog/product')->load($productId);

    foreach($groupAndPrice as $magentoGroupName => $itemPrice){

        $group = Mage::getModel('customer/group')->load($magentoGroupName, 'customer_group_code');

        $store_id = 0;
        Mage::getSingleton('catalog/product_action')->updateAttributes(
            [$productId],
            [
                'cust_group'    => $group->getId(),
                'price'         => $itemPrice
            ],
            $store_id
        );

    }
}

This sadly doesn't work. I have also tried $product->setGroupPrice() and I can not get that to work either. I'm using Magento 1.9.0 by the way.

1 Answer 1

0

I haven't had time to test this, but assuming you are traversing your array data correctly, this should work.

foreach($prices as $sku => $groupAndPrice){

$store_id = 0;
$catalog = Mage::getModel('catalog/product');
$productId = $catalog->getIdBySku($sku);
$product = Mage::getModel('catalog/product')->load($productId);

foreach($groupAndPrice as $magentoGroupName => $itemPrice){

    $group = Mage::getModel('customer/group')->load($magentoGroupName, 'customer_group_code');

    $product->setData('group_price',array (
        array (
            "website_id" => $store_id,
            "cust_group" => $group->getId(),
            "price" => $itemPrice
        )));

    $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.