Question

Is there a method to add group prices (for one or more customer groups) to product collections?

Actually I have this ...

/** @var Mage_Catalog_Model_Resource_Product_Collection $products */
$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeTo ...

if ($groupId) {
    $products->joinTable(
        array('group_price_'.$groupId => 'catalog/product_attribute_group_price'),
        'entity_id = entity_id',
        array('group_price_'.$groupId => 'group_price_'.$groupId.'.value'),
        array('customer_group_id' => $groupId)
    );
}

It works and I can get group price with $product->getData('group_price_X'), but is there something built-in?

Was it helpful?

Solution 2

Not added to collection, but it works too ... :)

You can get a particular group price with this:

/** @var Mage_Catalog_Model_Product $product */
foreach ($products as $product) {
    var_dump($product->setCustomerGroupId($groupId)->getGroupPrice());
}

To get all group prices:

/** @var Mage_Catalog_Model_Product $product */
foreach ($products as $product) {
    $product->getGroupPrice();
    var_dump($product->getData('group_price'));
}

Or:

/** @var Mage_Catalog_Model_Product $product */
foreach ($products as $product) {
    $product->getResource()->getAttribute('group_price')->getBackend()->afterLoad($product);
    var_dump($product->getData('group_price'));
}

OTHER TIPS

Cheaper prices (as it can conceptually be group_price, per customer_group) should be joined when calling addMinimalPrice()

public function addMinimalPrice()
{
    return $this->addPriceData();
}

And so...

public function addPriceData($customerGroupId = null, $websiteId = null)
{
    $this->_productLimitationFilters['use_price_index'] = true;

    if (!isset($this->_productLimitationFilters['customer_group_id']) && is_null($customerGroupId)) {
        $customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
    }
    if (!isset($this->_productLimitationFilters['website_id']) && is_null($websiteId)) {
        $websiteId       = Mage::app()->getStore($this->getStoreId())->getWebsiteId();
    }

    if (!is_null($customerGroupId)) {
        $this->_productLimitationFilters['customer_group_id'] = $customerGroupId;
    }
    if (!is_null($websiteId)) {
        $this->_productLimitationFilters['website_id'] = $websiteId;
    }

    $this->_applyProductLimitations();

    return $this;
}

However, if you need to get all possible group prices (without caring if they are the best possible price), then I think there is no built-in method better than your approach

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top