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 } ?>
7ochem
  • 7,532
  • 14
  • 51
  • 80
Adam Allen
  • 2,205
  • 7
  • 37
  • 68

2 Answers2

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.

Siarhey Uchukhlebau
  • 15,957
  • 11
  • 54
  • 83
Amit Bera
  • 77,456
  • 20
  • 123
  • 237
  • 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
  • I change the $product to $_produt as that what is was thorughout the rest of the code but now im getting this error:Fatal error: Call to a member function getStoreId() on a non-object in /home/test/public_html/app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Groupprice/Abstract.php on line 219 , Any ideas what this means i have check the file and the line is: $storeId = $object->getStoreId(); – Adam Allen Sep 18 '14 at 08:24
  • ok ,i will check shortly – Amit Bera Sep 18 '14 at 08:28
  • after looking through your edits i have added the new code and it is no saying this line $groupPrices = $ProductObject->getData('group_price'); is calling on a non object. Also thank you for all your help! – Adam Allen Sep 18 '14 at 13:50
  • how to get product id on your code? i need to product id – Amit Bera Sep 18 '14 at 13:58
  • $ProductObject->getId() give any product id? – Amit Bera Sep 18 '14 at 14:01
  • $_id = $_product->getId(); gets the ID doesnt it? – Adam Allen Sep 18 '14 at 14:06
  • yes then remove

    $ProductObject by $_product

    – Amit Bera Sep 18 '14 at 14:08
  • thank you so much! there are a few little niggles but i think i can sort them out. The main thing is it now display a different message :) thank you so much for all your help and sorry for my lack of knowledge!!! – Adam Allen Sep 18 '14 at 14:12
  • could you have a look at this post as it is similar to this one and you really helped me out last time. http://magento.stackexchange.com/questions/38334/want-to-display-an-image-if-a-customer-group-has-a-special-price – Adam Allen Oct 08 '14 at 09:31
  • k... i will check – Amit Bera Oct 08 '14 at 09:32
  • @AmitBera thanks.. It works fine. But this way will work on template side right? I mean while fetching the product it gets all products but while rendering the template it eliminates the product which doesn't have group price. Is there anyway not get fetch the product if no product price updated for current users group? – Anto S Dec 14 '15 at 10:27
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.

Fabian Schmengler
  • 65,791
  • 25
  • 187
  • 421