melvynanderson
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 (); ?> </ span > items 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 >
Posted: February 21 2013
| top
melvynanderson
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.
Posted: February 22 2013
| top
| # 2
melvynanderson
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.
Posted: February 22 2013
| top
| # 4
melvynanderson
Total Posts: 20
Joined: 2012-12-24
Siggi_Schmitz - 22 February 2013 08: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) { } } }
Posted: February 22 2013
| top
| # 6
melvynanderson
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 ; ?> </ span > items , total 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 >
Posted: February 22 2013
| top
| # 8
Siggi_Schmitz
Total Posts: 562
Joined: 2011-07-03
okay, here`s your code with my changes (without syntax errors ).
<?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 (); ?> </ span > items 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 >
Signature
Magento Freelancer.
Need an extension ? Feel free to contact me!
Web: http://www.sig-tec.de
E-Mail:
Skype: sig-tec
Xing: https://www.xing.com/profile/Siegfried_Schmitz2
Profil auf freelance.de
Profil auf freelancermap.de
Posted: February 22 2013
| top
| # 9
melvynanderson
Total Posts: 20
Joined: 2012-12-24
Ah, I see what I was doing wrong.
Thanks again for the help!
Posted: February 22 2013
| top
| # 10