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

MY CART - Adding calorie calculator - Possible? 
 
Marc O'Neill
Jr. Member
 
Total Posts:  12
Joined:  2008-10-06
UK
 

Good morning all,

I am looking at various e-commerce solutions to find the one that is most flexible.

I will be selling various food items, however, i want every item to have it’s calorie value associated with it. Every time an item is added to the cart, as well as the price calculation i would like to show a total calories calculation within the cart.

At this stage i’m trying to determine whether this is possible using Magento.

Thankyou

Marc

 
Magento Community Magento Community
Magento Community
Magento Community
 
crius
Guru
 
Avatar
Total Posts:  623
Joined:  2007-10-16
Denmark
 

That’s definitely possible. Magento has an attribute system that allows you to define your own attributes for products. In your case, you can make an attribute called “calories”. Then you need to make a small function to calculate the total calories of products in the cart, of course, but that should be easy.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Marc O'Neill
Jr. Member
 
Total Posts:  12
Joined:  2008-10-06
UK
 

Hi,

It has been a while since your reply, but i found it very useful. I decided to use Magento for my store.

I managed to set attributes etc but have two problems;

1. In a grid listing i.e. a short product description, i cannot get the attribute to show, however, if i click on a product the new attribute is shown within. Can i show an attribute within the initial product listing

2. I understood your post regarding writing a small function to calculate the total calories. I’m assuming it’s possible to display that in the cart? I need some help in learning how to write functions, i’m a quick learner, can you point me in the right direction? I take it i create the function in cart.phtml?

Thankyou for your help.

Marc O’Neill

 
Magento Community Magento Community
Magento Community
Magento Community
 
crius
Guru
 
Avatar
Total Posts:  623
Joined:  2007-10-16
Denmark
 

1. http://www.google.com/search?hl=en&q;=site:www.magentocommerce.com+show+attribute+on+category+page&btnG;=Google+Search&aq;=f&oq;=

2. The right way would be to create your own block or model class that extends a Magento class, e.g. Mage_Checkout_Block_Cart_Totals, and add the function there. However, if you don’t know a lot about object-oriented programming in PHP, Magento’s MVC architecture and how to make a custom module, you can also do a quick and dirty solution by making the calculation directly in cart.phtml, as you suggests.

In pseudo code, the calculation should be something like this:

totalcalories 0;
for 
each line in cart:
    
get product from line
    get calories from product
    totalcalories 
totalcalories productcalories quantity in cart line
end
 
Magento Community Magento Community
Magento Community
Magento Community
 
Marc O'Neill
Jr. Member
 
Total Posts:  12
Joined:  2008-10-06
UK
 

Hi, thanks again for the reply, referring back to question 1, as you know i’ve created the calories attribute called “calories”. I followed the tutorial to add the attribute to the category/product list, i did the following;

1. Opened catalog.xml and added

<action method="addAttribute"><attribute>calories</attribute></action>

just underneath

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
in both the “Category layered navigation layout” and “Category default layout”

2. Opened list.phtml and added

<?php echo $_product->getcaloriesAttribute()?><br />
within the product description. From the tutorial as far as i can see i have done everything correctly, however nothing appears in the product listing apart from the line break within the code. I’m sure i’m missing something simple here!

To give you more detail the product description code now looks like this;

<?php // Product description ?>
        
<div class="product-shop">
            <
h5><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName())?></a></h5>
            
<?php if($_product->getRatingSummary()): ?>
            <?php 
echo $this->getReviewsSummaryHtml($_product?>
            <?php 
endif; ?>
            <?php 
echo $_product->getcalories()?><br />
            
<?php echo $this->getPriceHtml($_producttrue?>
            <?php 
if($_product->isSaleable()): ?>

and the catalog layout;

<catalog_category_default>
        <
reference name="left">
            <
block type="catalog/navigation" name="catalog.leftnav" after="currency" template="catalog/navigation/left.phtml"/>
        </
reference>
        <
reference name="content">
            <
block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
                <
block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
                        <
action method="addAttribute"><attribute>calories</attribute></action>

                   <
block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
                        <!-- 
The following code shows how to set your own pager increments -->
                        <!--
                            <
action method="setDefaultListPerPage"><limit>4</limit></action>
                            <
action method="setDefaultGridPerPage"><limit>9</limit></action>
                            <
action method="addPagerLimit"><mode>list</mode><limit>2</limit></action>
                            <
action method="addPagerLimit"><mode>list</mode><limit>4</limit></action>
                            <
action method="addPagerLimit"><mode>list</mode><limit>6</limit></action>
                            <
action method="addPagerLimit"><mode>list</mode><limit>8</limit></action>
                            <
action method="addPagerLimit" translate="label"><mode>list</mode><limit>all</limit><label>All</label></action>
                        -->
                    </
block>
                    <
action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
                </
block>
            </
block>
        </
reference>
    </
catalog_category_default>

Thank you for your help about this, i may need to talk to you about the calorie calculator, i am not yet competent with the kind of coding we are talking about and may look to pay someone to do it on a job by job basis.

Thanks

Marc

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