1

I am trying to add a new Customer Group and copy the price of one of the Group Price's products into this new customer group for all products. For example, I have a customer group Group1 which has price x, and I want to create a script which adds the new group (Group2) and copies the price from Group1 (x) to all products. Has anyone done this before/can point me in the right direction?

I'm starting by trying to load group prices, but it returns empty:

    function productCallback($args)
{
    $product = Mage::getModel('catalog/product');
    $product->setData($args['row']);
    echo $args['arg1'] . "\n";  
    echo $product->getSku() . ': ' . $product->getName() . "<br/>";

    $groupPrices = $product->getData('group_price');
    foreach($groupPrices as $key)
    {
        foreach($key as $subkey => $value)
        {
            echo $subkey . " ~ " . $value . "<br/>";
        }
    }

    echo "<br/><br/>";
}

$products = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('*');

Mage::getSingleton('core/resource_iterator')->walk($products->getSelect(), array('productCallback'), array('arg1' => '===='));
1

I fixed it like this:

require_once("./app/Mage.php");
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

function productCallback($args)
{
    $product = Mage::getModel('catalog/product');
    echo $product->getSku() . ': ' . $product->getGroupPrice() . "<br/>";

    $groupPrices = $product->getData('group_price');
    $standardWholesalePrice = 0;
    foreach($groupPrices as $key=>$value)
    {
        foreach($value as $subkey => $subvalue)
        {
            if ($subkey == "cust_group" && $subvalue == 2) 
            {
                $standardWholesalePrice = $value['price'];
                $newGroupPrice = array(array ( 'website_id'=>1,
                'cust_group'=>5,
                'price'=>$standardWholesalePrice,
                'website_price'=>$standardWholesalePrice,
                'all_groups'=>0));

                $newGroupPrices = array_merge($groupPrices,$newGroupPrice);

                $product->setData('group_price',$newGroupPrices);
                $product->save();
                break;
            }
        }
    }

    echo "<br/><br/>";
}

$products = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('sku')                        
                    ->addAttributeToSelect(array('group_price'));

Mage::getSingleton('core/resource_iterator')->walk($products->getSelect(), array('productCallback'), array('arg1' => '===='));
|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.