Try the Demo

Magento

eCommerce Software for Online Growth

Magento Forum

Our new hosted solution for small & emerging businesses
   
Retrieving order info in success.phtml
 
piquet
Jr. Member
 
Total Posts:  10
Joined:  2009-12-08
 

Sorry, very new to Magento and finding my way round an already developed site.

The site uses onepage for checkout.  I want to be able to retrieve a few aspects of an order by the time I reach success - particularly, the total cost of the order just completed and the method used for payment.

I know I can get to the Order number
with $this->getOrderId()
but I am not strong on the whole magento model.  I would like to be able to do something similar: $this->getOrderTotal()

Please advise how to go about the steps that would allow this.

Also, I have read much but do not have not managed a strong understanding of the whole model.  Any pointers to good beginner-developer tutorials in magento would be appreciated.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Total Posts:  1300
Joined:  2009-11-18
Bucharest, Romania
 

You can retrieve the order details like this

$order Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId());
After this you can get various data from the order
$subtotal $order->getSubtotal();
$order->getId(); //the id of the order
$order->getIncrementId();//the increment id of the order
$order->getGrandTotal();//grand total of the order

 Signature 

It’s not a bug. It’s a feature.
http://marius-strajeru.blogspot.com/
E-commerce solutions and much more: Anais-IT

 
Magento Community Magento Community
Magento Community
Magento Community
 
piquet
Jr. Member
 
Total Posts:  10
Joined:  2009-12-08
 

Thanks.  Just what I wanted.

Just a re-ask if anyone can share links to good beginner-developer tutorials in magento.

 
Magento Community Magento Community
Magento Community
Magento Community
 
piquet
Jr. Member
 
Total Posts:  10
Joined:  2009-12-08
 

@tzyganu thanks again for your response.

I wanted to use that method because our virtual product can be a free download, or paid version.  At the moment the free download is going through checkout for two reasons:
- how the site was delivered - not ideal
- we get extra info after checkout success that we store against the downloaded product, tied to the user registration.

However, I need to be flexible because I now have to be able to determine whether the product was not only free or paid, but, IF free whether trial (with limitations in use) or fully usable but (only for a certain time period).

So, I have started looking at adding a custom product attribute via the Magento Admin (say, downloadType) and setting that with a value that I can detect against the product at checkout success.

My question is, like with the order example that was a workable approach before, can I working with the last order;
$order = Mage::getModel(’sales/order’)->load(Mage::getSingleton(’checkout/session’)->getLastOrderId());

do something like :
retrieve the product from the order // $product = $order->getProduct() :-(

look up my custom attribute from the product // $product->getAttribute(’downloadType)

and work with that.

Advice very much appreciated.
PK

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Total Posts:  1300
Joined:  2009-11-18
Bucharest, Romania
 

I’m not sure I fully understand your problem
Here is how you can get the ordered products. Maybe this helps

$order Mage::getModel('sales/order')->load(Mage::getSingleton('checkout/session')->getLastOrderId()); 
$items $order->getAllItems();
foreach (
$items as $item){
$product 
$item->getProduct();
//DO WHAT EVER YOU WANT WITH THE PRODUCT
//for example
$downloadType $product->getData('downloadType');
}
One other advice. Don’t use capital letters in your attribute codes.
Use download_type instead of downloadType.

I hope this helps.

 Signature 

It’s not a bug. It’s a feature.
http://marius-strajeru.blogspot.com/
E-commerce solutions and much more: Anais-IT

 
Magento Community Magento Community
Magento Community
Magento Community
 
piquet
Jr. Member
 
Total Posts:  10
Joined:  2009-12-08
 

Thanks.  This certainly helps.  I feel I was on the right track.

Your code snippet is the essence of what I want to do but, using it, I get the error:

Call to a member function getData() on a non-object

for the line:

$product->getData('download_type')

echoing $product is an empty string but $items IS an array of count()=1.  Other information such as the $order->getGrandTotal() also provides the expected result.

So, I am at a loss as to why the $items returns nothing for getProduct() as I am certainly adding a product to the shopping cart.

Suggestions appreciated.

PK

PS:
I feel that this is a bit of “buy a man a fish and he will eat for a day, teach a man to fish and he will eat forever”.  Perhaps I can be quickly taught how to fish!

Being new to Magento - and not fully comprehending the MVC framework yet (reading madly) I don’t know where/how to reference what methods are available to me when I use the calls like: 

$order Mage::getModel('sales/order')....
My understanding is that this provides me with an order object instantiated against lastOrderId.  Where do I reference what methods are available for my order object. ... like getAllItems so that I know what getAllItems returns and then what I can do with that.

 
Magento Community Magento Community
Magento Community
Magento Community
 
tzyganu
Mentor
 
Total Posts:  1300
Joined:  2009-11-18
Bucharest, Romania
 

Sorry about that.
I’ve rewritten the order item model and I have a method called getProduct.
Try it like this. instead of

$product $item->getProduct();
use this
$product Mage::getModel('catalog/product')->setStoreId($order->getStoreId())->load($item->getProductId());
This was the fish.

Now this is how you catch fish.
Magento has a nice system that allows you to overwrite models, blocks helpers and controllers.
For more details see this http://magedev.com/2009/06/03/magento-overriding-model-block-or-helper/

The method getModel() create an instance of a model
Te parameter looks like this ‘package/object_subobject’
What’s before the slash (/) is usually the name of the directory in app/code/core/Mage where the class is located.
Slash means the directory ‘Model’
and what is after slash , replace ‘_’ with ‘/’ and you find the full path.
For example if you call Mage::getModel(’catalog/product’) you will get an instance of app/code/core/Mage/Catalog/Model/Product.php
If you call Mage::getModel(’sales/order_item’) you will get an instance of app/code/core/Mage/Sales/Model/Order/Item.php

This also works for custom models. For those the path does not start with app/code/core. It starts with app/code/local.
Magento knows where to get the class. I have a small idea on how it works but I’m afraid I’ll say something stupid.
how it works is not important. The important think is that it works.

Maybe this helps a little http://www.magentocommerce.com/wiki/doc/magento-architecture

I hope this helps. This is just a small part. In order to learn how to fish you must try and try again.

 Signature 

It’s not a bug. It’s a feature.
http://marius-strajeru.blogspot.com/
E-commerce solutions and much more: Anais-IT

 
Magento Community Magento Community
Magento Community
Magento Community
 
piquet
Jr. Member
 
Total Posts:  10
Joined:  2009-12-08
 

Thanks for the rod and line - and all the fish.

I now am able to do what I set out for.  Very much appreciated.

with my fishing skills there is still this one that got away:

I set the attribute up as a drop down list - want to prevent shop keeper error.  Fine.
So I have values in my list like “Free”, “Annual”, “Month” and that’s what I want to make my decision on.

but

$product->getData('download_code')
returns an integer value.  Okay, that’s cool, I expect magento keeps my text values stored somehow.

So, with my fishing I figure that the public function in getAttributeText($code) in /app/code/core/Mage/Catalog/Model/Product.php should be applicable for getting my text value back.  I’d build a case on the integer except that - undoubtedly - the enumeration will be different when setting the attribute up in production environment.  So, I want the text I prescribed.

I try

$attrCode $product->getData('download_type');
$attrText $product->getAttributeText($attrCode)
which I can tell isn’t quite right and throws the error about calling getResource on a non-object in the catalog/product.php function:
public function getAttributeText($attributeCode)
    
{
        
return $this->getResource()
            ->
getAttribute($attributeCode)
                ->
getSource()   // error thrown here
                    
->getOptionText($this->getData($attributeCode));
    
}

There is no explanation on what value/type $attributeCode is expected to be.  Not here ... but possibly somewhere?  More bait for the hooks.

Meanwhile, thanks so much for all the assistance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
drew.gillson
Jr. Member
 
Total Posts:  14
Joined:  2009-07-30
 

For anyone still struggling with how to get the text values of an EAV attribute option id, here is a code snippet for you:

$db Mage::getSingleton('core/resource')->getConnection('core_read');

// Get the correct option ids (because these differ from staging to live)
$select $db->select()->from('eav_attribute_option_value')->where('value = ?''5-7 days');
$rows $db->fetchAll($select);
if (isset(
$rows[0])) {
    $fivetoseven 
$rows[0]['option_id'];
}

$select 
$db->select()->from('eav_attribute_option_value')->where('value = ?''10-14 days');
$rows $db->fetchAll($select);
if (isset(
$rows[0])) {
    $tentofourteen 
$rows[0]['option_id'];
}

This only checks for a specific value - you should probably also match against the specific ID that you’re looking to compare the value of. No idea if there is an easier way but this will get you by.

 
Magento Community Magento Community
Magento Community
Magento Community
 
drew.gillson
Jr. Member
 
Total Posts:  14
Joined:  2009-07-30
 

For anyone still struggling with how to get the text values of an EAV attribute option id, here is a code snippet for you:

$db Mage::getSingleton('core/resource')->getConnection('core_read');

// Get the correct option ids (because these differ from staging to live)
$select $db->select()->from('eav_attribute_option_value')->where('value = ?''5-7 days');
$rows $db->fetchAll($select);
if (isset(
$rows[0])) {
    $fivetoseven 
$rows[0]['option_id'];
}

$select 
$db->select()->from('eav_attribute_option_value')->where('value = ?''10-14 days');
$rows $db->fetchAll($select);
if (isset(
$rows[0])) {
    $tentofourteen 
$rows[0]['option_id'];
}

This only checks for a specific value - you should probably also match against the specific ID that you’re looking to compare the value of. No idea if there is an easier way but this will get you by.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mauricehon
Jr. Member
 
Total Posts:  2
Joined:  2010-10-12
 

May I know how to get the value of product ide and quantity? do I need a loop for this?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
© Copyright 2012 Magento Inc.
Privacy Policy|Terms of Service
Magento Community Count
701238 users|769 users currently online|497070 forum posts