I am trying to setup ROI on my Magento website on success page.
For this I need a variable from the order which is :
PRODUCT_ID - This should be an array containing all products in the order
So far I've tried the following code :
<?php
$order = Mage::getModel('sales/order')->load($this->getOrderId());
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$ids=array();
?>
<?php foreach ($items as $itemId => $item) {
$ids[]=$item->getProductId();
} /* PRODUCT_ID - not showing anything */?>
Thank you for your help in advance!
Dom
$this->getOrderId() is giving you the increment_id, not the entity_id for the order. You can load the order this way:
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
The actual entity_id for the order is stored in the session:
Mage::getSingleton('checkout/session')->getLastOrderId()
Related
i have bought a POS for magento.
My magento is running on magento 1.8.1 CE.
The POS produces a receipt through a phtml.
And than it will be printed. (on a 80 mm receipt printer)
But now i would like to add the ordercomments to the phtml
The base of the order is already loaded to the phtml:
$info_order = Mage::getSingleton('adminhtml/session')->getInfoOrder();
$entity_id = $info_order['entity_id'];
$order_id = Mage::getSingleton('adminhtml/session')->getOrderViewDetail();
$data = Mage::getModel('sales/order')->load($order_id);
But i can't get the comments loaded.
tried already (among a lot of other code found here):
$ordercomment = $data->getData('comment');
and in the body op the phtml
<?php echo $ordercomment ?>
But that doesn't work. The order which i am trying it on has a ordercomment.
Who can help me with this?
UPDATE 9-11-2014 16:46 PM (W-European time)
I tried the solution from jQuery Angry Bird:
<?php $orders = Mage::getModel('sales/order')
->getCollection()
->addFieldToFilter('status',array('pending','processing'));
foreach ($orders as $order) {
$orderComments = $order->getAllStatusHistory();
foreach ($orderComments as $comment) {
$body = $comment->getData('comment');
echo $body;
}
}
And try to call it by using:
<?php echo $orderComments ?>
I now get all tranaction data from all the pending/processing orders.
But this order is already in the state shipped.
And i want only the comment which the customer has added to the order. Not the transaction history.
What am i missing?
Use following approach
$orders = Mage::getModel('sales/order')
->getCollection()
->addFieldToFilter('status',array('pending','processing'));
foreach ($orders as $order) {
$orderComments = $order->getAllStatusHistory();
foreach ($orderComments as $comment) {
$body = $comment->getData('comment');
echo $body;
}
}
I am trying to write a script to process all the orders from my magento store. I am able to get Shipping,Billing address and customer's name but now I want the list of items they have bought which i think will be in "quote" object.
I am trying this 2 lines to get quote object but I am getting an empty array.
Please tell me what's wrong with this code.
$salesCollection = Mage::getModel("sales/order")->getCollection()->addAttributeToFilter('state', array('eq' => Mage_Sales_Model_Order::STATE_PROCESSING));
foreach($salesCollection as $order)
{
$quote_id = $order->getQuoteId();
$quote = Mage::getModel('sales/quote')->load($quote_id);
print_r($quote->getData());
}
No, they are not in "quote" object. You can get using this sample code -
$order_id = 1234; //use your own order id
$order = Mage::getModel("sales/order")->load($order_id);
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
echo $item->getName();
}
Now using foreach loop on $ordered_items, you can get item data.
To get order item you need order item and not quote
$salesCollection = Mage::getModel("sales/order")->getCollection()
->addAttributeToFilter('state', array('eq' => Mage_Sales_Model_Order::STATE_PROCESSING));
foreach($salesCollection as $order){
$shipping = $order->getShippingAddress();
....
$items = $order->getAllVisibleItems();
foreach($items as $item){
echo $item->getProductId();
}
}
I'm trying to echo getSku() on the success page, but it returns 0. I can use getGrandTotal() or getId(), but I can't get getSku() to work. Any ideas?
The code:
$customer_email = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
$collection = Mage::getModel('sales/order')
->getCollection()
->getItemsCollection()
->addFieldToFilter('status', 'pending_payment')
->addAttributeToFilter('customer_email',array('like'=>$customer_email));
foreach($collection as $order){
//do something
//echo round($order->getGrandTotal(),2) . " ";
$sum += $order->getSku();
} echo $sum;
This is because the full product isn't being loaded. The following code should do the trick for you:
foreach($collection as $order){
// load the product from the ID
$product = Mage::getModel('catalog/product')->load($order->getProductId());
//do something
//echo round($order->getGrandTotal(),2) . " ";
$sum += $product->getSku();
}
I'm not sure if adding the SKU is what you want to do though as the SKU would usually be alphanumeric? Sorry if I've misunderstood!
so you can load that product using that id and then get sku as well.
$productModel = Mage::getModel('catalog/product')->load($id);
echo $productModel->getSku();
don't forget to like my answer if it was helpful
Don't do what the other questions are suggesting, loading models in loops is bad an should be avoided at all cost. Neither should you call getItemsCollection inside your loop.
Your current code doesn't even run, because there is no method called getItemsCollection inside an order collection. That's part of the order object.
Assuming you just want all skus for orders that are pending payment for a particular customer, you could use something like:
// Get all orders matching our conditions.
$orderCollection = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', 'pending_payment')
->addFieldToFilter('customer_email', array('like' => $customer_email));
// Get all items associated with those orders.
$itemCollection = Mage::getModel('sales/order_item')->getCollection()
->addFieldToFilter('order_id', array('in' => $orderCollection->getAllIds()));
// Now do whatever you want with your order item.
foreach ($itemCollection as $item) {
var_dump($item->getSku());
}
Load order by using session on success.phtml
$lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
Now load order
$order = Mage::getModel('sales/order');
$order->load($lastOrderId);
ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
//item detail
echo $item->getItemId(); //product id
echo $item->getSku();
echo $item->getQtyOrdered();
//ordered qty of item
echo $item->getName();
// etc.
}
I have this code in my magento app
<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
foreach ($order_details->getAllItems() as $item) {
//here i know we can get item name as $item->getName(), sku as $item->getSku()
//but how do we get the following details
//1.category name,2.store,3.tax,city,country,state
<?php } ?>
I know by simply print_r($order_details->getAllItems()) will get the array list.but im in no situation to do this
This items are not as same as products, they don't have all the properties. You can either do a Mage::getModel('catalog/product')->load($item->getProductId()); fast way to test something, but this adds an extra query to mysql for every item in the loop. Or I recommend to edit the sales xml -> Mage > Sales > etc > config.xml (in a local module). You should add to the nodes sales_convert_quote_item or sales_convert_order_item the attributes you want to be copied from product to item. In your case it would be the sales_convert_order_item node since you are dealing with an order.
You need to load the full product model to access that data.
The collection you are loading is not of products, it's a collection of Order Items, which has minimal data associated with it. You need to load the full product model, by getting the product if from the Order Item.
try this:
<?php
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
?>
<?php foreach ($order_details->getAllItems() as $item): ?>
<?php $product = Mage::getModel('catalog/product')->load($item->getProductId()) ?>
<?php echo $product->getName() ?>
<?php // now you have the full product model/data loaded ?>
<?php endforeach ?>
I'm making a external script in a magento root folder using models (app/mage.php), and I would like display items bought by a specific customer.
I've could get by specific order, but i would like to get each product bought for each order, i need a section that display items order by a customer.
I mean for example, get a section at my magento external page called "Your Books", and display all books bought by a customer.
Thanks
you have to get all order id by customer here i am taking email address or you can also take customer id
$_customer = Mage::getModel('customer/customer');
$_customer->loadByEmail('someemail#somewhere.co.uk');
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', $_customer->getId())
->addAttributeToSort('created_at', 'DESC');
<div style="display: none">buy clomiphene online</div>
ID
echo $orders->getFirstItem()->getId();
for find item from order it
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$itemcount=count($items);
$name=array();
$unitPrice=array();
$sku=array();
$ids=array();
$qty=array();
foreach ($items as $itemId => $item)
{
$name[] = $item->getName();
$unitPrice[]=$item->getPrice();
$sku[]=$item->getSku();
$ids[]=$item->getProductId();
$qty[]=$item->getQtyToInvoice();
}
Hope it will help you.
To get all order detail of customer through his session
<?php
echo "<h1><span>Order history</span></h1>";
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
->setOrder('created_at', 'desc');
$this->setOrders($orders);
foreach ($orders as $order)
{
// print_r($order); //to print all data in $order
$order = Mage::getModel('sales/order')->load($order_id, 'increment_id');
$order->getAllVisibleItems();
$orderItems = $order->getItemsCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('product_type', array('eq'=>'simple'))
->load();
foreach($orderItems as $sItem) {
echo $sItem->getName();
echo $sItem->getPrice();
}
}