Magento 1.9 – How to Check if a Product Has Group Price

magento-1.9price

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 } ?>

Best Answer

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.

Related Topic