0

I need to display a custom price for some products to a certain group of customers. I made it, but I only see the custom price in the product view page, and I need to display it in all the web. Now I see the regular price in the product list, and it should also display the custom price.

First I created the custom price attribute private_price for the products.

Then I created a module, and listen for the catalog_product_get_final_price event:

<events>
    <catalog_product_get_final_price>
        <observers>
            <catalog_product_get_final_price_handler>
                <type>singleton</type>
                <class>privatesales/observer</class>
                <method>modifyFinalPrice</method>
            </catalog_product_get_final_price_handler>
        </observers>
     </catalog_product_get_final_price>
</events>

and in the observer:

public function modifyFinalPrice(Varien_Event_Observer $observer)
{
    $isLoggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
    /* If customer is logged in */
    if($isLoggedIn) {
        /* Get the logged in customer's group ID */
        $customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        /* Check if the logged in customer's group ID matches with the ID you are after */
        if ($customerGroupId==2) {
            $product = $observer->getEvent()->getProduct();
            $privatePrice = (double) $product->getPrivatePrice();
            if ($privatePrice>0 && !empty($privatePrice) && !is_null($privatePrice)) {
                $product->setFinalPrice($privatePrice);
            }
        }  
    }
}

I'm using magento 1.9.2.4

Update: I also tried the event catalog_product_load_after with same result, price changed in product view, but price remains unchanged in product list.

2 Answers 2

1

I used event for product list catalog_product_collection_load_after

config.xml

<catalog_product_collection_load_after>
    <observers>
        <brolab_erp>
            <type>singleton</type>
            <class>brolab_erp/observer</class>
            <method>catalogProductCollectionLoadAfter</method>
        </brolab_erp>
    </observers>
</catalog_product_collection_load_after>

Observer.php

/**
 * @param Varien_Event_Observer $observer
 * @return $this
 */
public function catalogProductCollectionLoadAfter(Varien_Event_Observer $observer)
{
    $collection = $observer->getEvent()->getData('collection');
    /** @var Mage_Catalog_Model_Product $item */
    foreach ($collection->getItems() as $item) {
        /* your code */
    }
    return $this;
}
4
  • Good answer, I tried it before, but I still do not see the private_price in the product list. Maybe this custom attribute is not added to the event collection??
    – Alex
    Commented May 30, 2018 at 8:24
  • Your will need use additional method for attribute value in items iteration: $item->getResource()->getAttributeRawValue($item->getId(), 'private_price', Mage::app()->getStore())) Commented May 30, 2018 at 16:27
  • Yes! this way it works perfect. But, be careful, because there is an extra closing parenthesis. Thank you!
    – Alex
    Commented Jun 1, 2018 at 13:22
  • Thanks, I have written without check. Fixed: $item->getResource()->getAttributeRawValue($item->getId(), 'private_price', Mage::app()->getStore()) Commented Jun 1, 2018 at 13:46
0

You can set Tier Price of product with respect to customer Group. So, You can get this feature from Magento Backend by default. No need to implement extra code.

try it & let me know.

1
  • This is part of a bigger code, that I simplified. I need to control several things that I can't control with tier prices. So I have to develop it with an observer.
    – Alex
    Commented May 29, 2018 at 15:08

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.