Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

[SOLVED] Add all product prices together for sum total
 
melvynanderson
Jr. Member
 
Total Posts:  20
Joined:  2012-12-24
 

I’m trying to add a bunch of prices together, but getPrice doesn’t seem to be working.  Am I using the foreach and $sum parts wrong?

I’ve been trying to figure this out for a month but can’t seem to find any help.

<?php  
    $collection 
Mage::getModel('catalog/product')->getCollection()
->
joinField(
    
'qty',
    
'cataloginventory/stock_item',
    
'qty',
    
'product_id=entity_id',
    
'{{table}}.stock_id=1',
    
'left'
)
->
addAttributeToFilter('qty', array('eq' <= 0));
    
$_coreHelper $this->helper('core');


    
$sum 0

        foreach (
$collection as $product)
    $sum 
+= $product->getPrice();
    
}
?>

As of today our inventory consists of <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo $collection->count(); ?></spanitems with a total value of <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo $_coreHelper->currency($sum); ?></span>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Siggi_Schmitz
Enthusiast
 
Avatar
Total Posts:  811
Joined:  2011-07-03
 

Is the product object correct? When you try to print $product->getId(), is there a correct output?
Make $product->getData() to see all product data and the price fields.

 
Magento Community Magento Community
Magento Community
Magento Community
 
melvynanderson
Jr. Member
 
Total Posts:  20
Joined:  2012-12-24
 

No, the way it is now it just prints 0.00.

I did a quick test with $product->getId() and it displays nothing, so I’m assuming I’m not getting the data I need correctly.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Siggi_Schmitz
Enthusiast
 
Avatar
Total Posts:  811
Joined:  2011-07-03
 

Did you try to var_dump or log $product->getData() ? Then you see all attributes.

 
Magento Community Magento Community
Magento Community
Magento Community
 
melvynanderson
Jr. Member
 
Total Posts:  20
Joined:  2012-12-24
 

Actually, I put in a

<?php echo $product->getId(); ?>
and it displays a number, 207.

I’m very new to all of this.  Changing $product-> from getPrice to getData gives me an error “Unsupported operand types” for that line.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Siggi_Schmitz
Enthusiast
 
Avatar
Total Posts:  811
Joined:  2011-07-03
 

try

<?php var_dump($product->getData()); ?>

When you have a output, then open the source code from the site in your browser to see the output more formatted.

 
Magento Community Magento Community
Magento Community
Magento Community
 
melvynanderson
Jr. Member
 
Total Posts:  20
Joined:  2012-12-24
 
Siggi_Schmitz - 22 February 2013 07:29 AM

try

<?php var_dump($product->getData()); ?>

When you have a output, then open the source code from the site in your browser to see the output more formatted.

That spits out this on my site

array(12{ ["entity_id"]=> string(3"207" ["entity_type_id"]=> string(1"4" ["attribute_set_id"]=> string(1"4" ["type_id"]=> string(6"simple" ["sku"]=> string(3"271" ["has_options"]=> string(1"0" ["required_options"]=> string(1"0" ["created_at"]=> string(19"2013-02-21 21:03:59" ["updated_at"]=> string(19"2013-02-21 22:38:21" ["qty"]=> string(6"1.0000" ["is_salable"]=> string(1"1" ["stock_item"]=> object(Varien_Object)#1014 (7) { ["_data:protected"]=> array(1) { ["is_in_stock"]=> string(1) "1" } ["_hasDataChanges:protected"]=> bool(false) ["_origData:protected"]=> NULL ["_idFieldName:protected"]=> NULL ["_isDeleted:protected"]=> bool(false) ["_oldFieldsMap:protected"]=> array(0) { } ["_syncFieldsMap:protected"]=> array(0) { } } }
 
Magento Community Magento Community
Magento Community
Magento Community
 
Siggi_Schmitz
Enthusiast
 
Avatar
Total Posts:  811
Joined:  2011-07-03
 

Okay, so make this changes:

$collection = Mage::getModel(’catalog/product’)->getCollection()
->addAttributeToSelect(’price’)

 
Magento Community Magento Community
Magento Community
Magento Community
 
melvynanderson
Jr. Member
 
Total Posts:  20
Joined:  2012-12-24
 

Well that worked, but I ended up having to remove the section that filtered out sold items.  Was getting “PHP Parse error:  syntax error, unexpected T_OBJECT_OPERATOR” when both were active.

I might be able to figure that part out on my own, but at least I’m actually getting prices added up and displaying a number now.

Thank you very much for your help.

Here’s what my code looks like at the moment:

<?php  
    $collection 
Mage::getModel('catalog/product')->getCollection()
    ->
addAttributeToSelect('price');
   
    
$stocktotal $collection->count();
        
    
$sum 0
                 
          foreach (
$collection as $product)
    $sum
+=$product->getData('price');
    
}
?>

<!--<?php var_dump($product->getData()); ?>-->
As 
of today our inventory consists of <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo $stocktotal?></spanitemstotal value $ <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo number_format($sum,2); ?></span>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Siggi_Schmitz
Enthusiast
 
Avatar
Total Posts:  811
Joined:  2011-07-03
 

okay, here`s your code with my changes (without syntax errors wink).

<?php  
    $collection 
Mage::getModel('catalog/product')->getCollection()
->
joinField(
    
'qty',
    
'cataloginventory/stock_item',
    
'qty',
    
'product_id=entity_id',
    
'{{table}}.stock_id=1',
    
'left'
)
->
addAttributeToSelect('price')
->
addAttributeToFilter('qty', array('eq' <= 0));
    
$_coreHelper $this->helper('core');


    
$sum 0

        foreach (
$collection as $product)
    $sum 
+= $product->getPrice();
    
}
?>

As of today our inventory consists of <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo $collection->count(); ?></spanitems with a total value of <span style="color: black; font-size:16px; font-family: Helvetica, sans-serif; font-weight: 400; border: 1px solid black; padding: 0px 2px 0px 2px;"><?php echo $_coreHelper->currency($sum); ?></span>
 
Magento Community Magento Community
Magento Community
Magento Community
 
melvynanderson
Jr. Member
 
Total Posts:  20
Joined:  2012-12-24
 

Ah, I see what I was doing wrong.

Thanks again for the help!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top