3

Some of our products have special prices for different groups, i have set up these prices in magento front end. I just need to know how to get a group price or check if a product has one.

I have this line which checks if a customer is logged in:

<?php
//Check if User is Logged In
$login = Mage::getSingleton('customer/session')->isLoggedIn(); 
if ($login) {

}

So i assume it will be similar but i just do not what to change or what to change it to. if i havnt explained this very well please let me know.


Added code

<?php
//Check if User is Logged In
$login = Mage::getSingleton('customer/session')->isLoggedIn();
if ($login) {
    //Get Customers Group ID
    $groupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    if ($groupId == 5) {
        $group_price = Mage::getModel('catalog/product')->getGroupPrice();
        if ($group_price) {
            ?>
            <span class="price-label"><?php echo "Special Group Price " ?></span><?php
        }
    }
} else { ?>
    <span class="price-label"><?php echo "Now" ?></span>
<?php } ?>
10

You can check group price by getGroupPrice() function of product object. It is not null that means group price exits in product.

$product = Mage::getModel('catalog/product')->load($productId);
if (!is_null($product->getGroupPrice())) {
    //group price  h
}

Magento have apply group price as price when Product price is greater then product group price

You cannot easy got group price. You need load product attribute resource value using getResource()->getAttribute('group_price'):

Edit: if you have $product object of product then you can get Group price using below code:

$groupPrices = $ProductObject->getData('group_price');
if (is_null($groupPrices)) {
    $attribute = $ProductObject->getResource()->getAttribute('group_price');
    if ($attribute) {
        $attribute->getBackend()->afterLoad($ProductObject);
        $groupPrices = $ProductObject->getData('group_price');
    }
}

if (!is_null($groupPrices) || is_array($groupPrices)) {
    foreach ($groupPrices as $groupPrice) {
        echo "<br/>";
        echo $Groupprice = $groupPrice['website_price'];
        echo "<br/>";
        echo $Groupprice = $groupPrice['cust_group'];
    }

}

Modified code is:

<?php
//Check if User is Logged In
$login = Mage::getSingleton('customer/session')->isLoggedIn(); 
if ($login && Mage::getSingleton('customer/session')->getCustomerGroupId() == 5) {

    $groupPrices = $ProductObject->getData('group_price');
    $Groupprice = $groupPrices;
    if (is_null($groupPrices)) {
        $attribute = $ProductObject->getResource()->getAttribute('group_price');
        if ($attribute) {
            $attribute->getBackend()->afterLoad($ProductObject);
            $groupPrices = $ProductObject->getData('group_price');
        }
    }
    /* check group price exit nor not */
    if (!is_null($groupPrices) || is_array($groupPrices)) {
        foreach ($groupPrices as $groupPrice) {

            if ($groupPrice['cust_group'] == Mage::getSingleton('customer/session')->getCustomerGroupId()) {
                echo $Groupprice = $groupPrice['website_price'];
                echo "<br/>";
                echo $GroupIds = $groupPrice['cust_group'];
                break;
            }
        }
    }
    /* $Groupprice  is null mean group price is not  exit*/
    if (!is_null($Groupprice)) {
        //group price eixts
        ?>
        <span class="price-label"><?php echo "Special Group Price " ?></span>
        <?php
    } else {
        //Group price is exits.
        ?>
        <span class="price-label"><?php echo "Now" ?></span>
    <?php }
    ?>

<?php } else { ?>
    <span class="price-label"><?php echo "Now" ?></span>
<?php } ?>

Issue: $group_price = Mage::getModel('catalog/product')->getGroupPrice(); is wrong code, It don't give group price.

You can product group price if you load Mage::getModel('catalog/product') by product is $group_price = Mage::getModel('catalog/product')->load($product)->getGroupPrice();

Mage::getModel('catalog/product')->getGroupPrice() did not give product object. You can get product object by:

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

or when you fetch product Collection and get each product by looped by product object id.

  • thank you! I am trying to display a string when a customer is logged in, in a certain customer group and also if the product has a group price. I have added the code to my question which im using but it isnt displaying anything. i alter you code to what i thought would fit my needs (the bottom if statement) could you point me in the right direction please. Thank you – Adam Allen Sep 17 '14 at 14:27
  • hey you got may point – Amit Bera Sep 17 '14 at 14:33
  • so would i just have to add the new code you have put up before my IF statements? – Adam Allen Sep 17 '14 at 14:45
  • I keep geting this error: Call to a member function getData() on a non-object in /home/test/public_html/app/design/frontend/theme/default/template/catalog/product/price.phtml on line 399 the line is $groupPrices = $product->getData('group_price'); – Adam Allen Sep 17 '14 at 15:19
  • ensure that $product is product object. – Amit Bera Sep 17 '14 at 15:37
3

You can get directly all assigned group prices with a script running this PHP script from commandline:

require_once __DIR__.'/../app/Mage.php';
$product = Mage::getModel('catalog/product')->load(6);
var_dump($product->getData('group_price'));

In Magento 1.9 the array of group prices is like this:

SKU1,array(4) {
  [0]=>
  array(6) {
    ["price_id"]=>
    string(6) "151560"
    ["website_id"]=>
    string(1) "0"
    ["all_groups"]=>
    string(1) "0"
    ["cust_group"]=>
    string(2) "17"
    ["price"]=>
    string(7) "52.5000"
    ["website_price"]=>
    string(7) "52.5000"
  }
  [1]=>
  array(6) {
    ["price_id"]=>
    string(6) "151561"
    ["website_id"]=>
    string(1) "0"
    ["all_groups"]=>
    string(1) "0"
    ["cust_group"]=>
    string(2) "18"
    ["price"]=>
    string(7) "52.5000"
    ["website_price"]=>
    string(7) "52.5000"
  }
  [2]=>
  array(6) {
    ["price_id"]=>
    string(6) "151562"
    ["website_id"]=>
    string(1) "0"
    ["all_groups"]=>
    string(1) "0"
    ["cust_group"]=>
    string(2) "19"
    ["price"]=>
    string(7) "52.5000"
    ["website_price"]=>
    string(7) "52.5000"
  }
}

If you use the $product->getGroupPrice() in the front-end, I think you get the product price assigned to the user is logged in the front-end. Because of this, for $product->getGroupPrice() you don't get an array, you get a string with only one price.

Hope it helps.

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.