skip to Main Content

When I get the product collection with :

$collection = Mage::getModel('catalog/product')->getCollection();

I get a group_price key but it’s empty. (product have group_price)

 Array
 (
    [entity_id] => 1
    [entity_type_id] => 4
    [attribute_set_id] => 2
    [type_id] => simple
    [sku] => 00000
    ......
    [group_price] => Array
        (
        )

    ......
)

And if I load the product I can get the group_price data with :

$product_id = '1';
$product = Mage::getModel('catalog/product')->load($product_id);
$group_price = $product->getData('group_price');

I tried different query with join() or joinTable() or joinLeft() with table ‘catalog_product_entity_group_price’ but without success.

How to join group_price data to the collection with a join query ?

I would like to avoid iterating the collection to load each product and get the group_price.

2

Answers


  1. Try to set the product in foreach loop and then get group price

    $collection = Mage::getModel('catalog/product')->getCollection();
    foreach($collection as $product){
        $product->setId($product->getId());
        $group_price = $product->getData('group_price');
    }
    
    Login or Signup to reply.
  2. I extended product collection class and added this function

     public function addGroupPriceData()
    {
        if ($this->getFlag('group_price_added')) {
            return $this;
        }
    
        $groupPrices = array();
        $productIds = array();
        foreach ($this->getItems() as $item) {
            $productIds[] = $item->getId();
            $groupPrices[$item->getId()] = array();
        }
        if (!$productIds) {
            return $this;
        }
    
        /** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
        $attribute = $this->getAttribute('group_price');
        if ($attribute->isScopeGlobal()) {
            $websiteId = 0;
        } else if ($this->getStoreId()) {
            $websiteId = Mage::app()->getStore($this->getStoreId())->getWebsiteId();
        }
    
        $adapter = $this->getConnection();
        $columns = array(
            'price_id' => 'value_id',
            'website_id' => 'website_id',
            'all_groups' => 'all_groups',
            'cust_group' => 'customer_group_id',
            'price' => 'value',
            'product_id' => 'entity_id'
        );
        $select = $adapter->select()
            ->from($this->getTable('catalog/product_attribute_group_price'), $columns)
            ->where('entity_id IN(?)', $productIds);
    
    
        if ($websiteId == '0') {
            $select->where('website_id = ?', $websiteId);
        } else {
            $select->where('website_id IN(?)', array('0', $websiteId));
        }
    
        foreach ($adapter->fetchAll($select) as $row) {
            $groupPrices[$row['product_id']][] = array(
                'website_id' => $row['website_id'],
                'cust_group' => $row['all_groups'] ? Mage_Customer_Model_Group::CUST_GROUP_ALL : $row['cust_group'],
                'price' => $row['price'],
                'website_price' => $row['price'],
    
            );
        }
    
        /* @var $backend Mage_Catalog_Model_Product_Attribute_Backend_Groupprice */
        $backend = $attribute->getBackend();
    
        foreach ($this->getItems() as $item) {
            $data = $groupPrices[$item->getId()];
            if (!empty($data) && $websiteId) {
                $data = $backend->preparePriceData($data, $item->getTypeId(), $websiteId);
            }
            $item->setData('group_price', $data);
        }
    
        $this->setFlag('group_price_added', true);
        return $this;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search