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.
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
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)
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.
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.
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.
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.
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.
// 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']; }
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.
// 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']; }
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.