I have order object and I need to get all shipped package tracking numbers. I tried following code, but it returns nothing even orders I tried had packages sent with tracking numbers.
$order = Mage::getModel('sales/order')->loadByIncrementId($orderid);
$trackingNumbers = $order->getTrackingNumbers();
Order object is not null, because it returns data for other methods like getShippingMethod etc.
If I click from "Shipping & Handling" link "Information Track Order", then popup shows all tracking numbers.
Try this..
$shipmentCollection = Mage::getResourceModel('sales/order_shipment_collection')
->setOrderFilter($order)
->load();
foreach ($shipmentCollection as $shipment){
$shipment->getAllTracks();
}
Edit:-
foreach ($shipmentCollection as $shipment){
foreach($shipment->getAllTracks() as $tracknum)
{
$tracknums[]=$tracknum->getNumber();
}
}
Now $tracknums will have the array of tracking numbers.
You can simply do this:
$orderIncrementId = 100000016; // YOUR ORDER INCREMENT ID;
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$trackNumber = array();
foreach ($order->getTracksCollection() as $track){
$trackNumber[] = $track->getNumber();
}
Related
I am working on a custom Magento Extension.
Here is how I take all customers in a customer group:
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*');
foreach($customers as $customer)
{
$email=$customer->getEmail();
$CustomerPhone = $customer->getPrimaryBillingAddress()->getTelephone();
$CustomerName = $customer->getName();
$CustomerEmail = $customer->getEmail();
}
This how I get information about the users in a specific customer group.
How I can get all the users ever paid or complete (for example) an order?
This is 90% of the way there:
<?php
require_once '<path_to_magento_root>/app/Mage.php';
Mage::app('default');
$customers = Mage::getResourceModel('reports/customer_collection')
->setPage(0,10)
//->addAttributeToFilter('orders_count', array('gt' => 0))
->addOrdersStatistics();
foreach ($customers as $c) {
echo $c->getId().' - '.$c->getEmail().': '.$c->getOrdersCount()." orders, average amount ".$c->getOrdersAvgAmount().", sum amount ".$c->getOrdersSumAmount().PHP_EOL;
}
The missing 10% is that this lists all customers, and their order information, rather than only ones with at least one order (which will include in-progress orders - the addOrdersStatistics() includes any orders that aren't canceled).
I have that commented line in there, //->addAttributeToFilter('orders_count', array('gt' => 0)), because I thought that should do it, but it appears to be doing nothing at all. Still, I figured I'd put this much up at least, because maybe it's at least a step in the right direction.
Of course, you also could just loop through every order, and build an array of customers that fit your criteria, but that's probably going to be much, much slower than using Magento's reports models like this. As a last resort, though, it'd work. So would querying the database directly, for that matter.
Try following solution this should work for you.
$customers = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToSelect('*');
$customers->joinTable(
array('sales/order'),
'customer_email=email',
array('*'),
null,
'right'
);
// print_r($customers->getData());
$data = $customers->getData();
foreach($data as $d)
{
//print_r($d); // print this to see the available fields
echo $d[customer_email]; //get the desired information
echo "<br>";
}
I am creating custom emails to be sent to customers wanting to share their cart with others. So far, I have each product's quantity, SKU, price, path (node/XYZ), and title. The last item I need in the email is the product's image path.
I found all the other information with the following:
$order = commerce_cart_order_load($user->uid);
foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
$sku = $line_item_wrapper->line_item_label->value();
//...
Printing out the following I was able to see a protected "data" property for the wrapper object:
print_r($line_item_wrapper->commerce_product);
Then, I tried finding the getter method for the field_image property with the following:
print_r($line_item_wrapper->commerce_product->getPropertyInfo('field_image');
I ended up here with entity_metadata_field_verbatim_get() but I don't know what parameters to pass. Also, in the last print statement above I didn't see anything else of value.
I'm wondering if I need to query for this data, and what table / columns to query for? Or maybe use something like node_load()? However, i'm not finding it too easy to find the node ID from the line item wrapper.
I was able to solve the above problem with this code:
$order = commerce_cart_order_load($user->uid);
$wrapper = entity_metadata_wrapper('commerce_order', $order);
$result = array();
foreach($wrapper->commerce_line_items as $d => $line_item_wrapper) {
$product = array();
$product['quantity'] = $line_item_wrapper->quantity->value();
$product['sku'] = $line_item_wrapper->line_item_label->value();
$product['path'] = $line_item_wrapper->commerce_display_path->value();
$product['price'] = $line_item_wrapper->commerce_unit_price->amount->value();
$product['title'] = $line_item_wrapper->commerce_product->title->value();
$product['image'] = $line_item_wrapper->commerce_product->field_image->value();
$product['image'] = $product['image'][0]['filename'];
array_push($result, $product);
}
I have to say, finding all these discrete functions was quite difficult. I did not find any clear documentation about Drupal Commerce metadata_wrappers. If anyone could provide further information I'd love to take a look.
In general, I used a lot of print_r(); to find these values.
I am trying to add conversion tracking to my magento store. I know I need to add the following code and customize it so magento will share info with CA.
<script type="text/javascript">
var _caq = _caq || [];
var products = [];
products.push({Sku: 'ProductID', UnitPrice: 'item price here', Quantity: 'quantity here'});
products.push({Sku: 'ProductID', UnitPrice: 'item price here', Quantity: 'quantity here'});
_caq.push(["Order", {OrderId: 'OrderID', Revenue: 'oVal', CurrencyCode: '3 letter currency code here', Products: products}]);
so far i have been trying to get the data from the order with the following code:
<?php $orderId = $this->getOrderId();
$order = Mage::getModel('sales/order')->load($orderId);
$items = $order->getAllItems();
$_grand = $order->getGrandTotal();
$custname = $order->getCustomerName();
$itemcount=count($items);
foreach ($items as $itemId => $item)
{
$sObject2->Item_name__c = $item->getName();
$sObject2->Unit_price__c = $item->getPrice();
$sObject2->Sku__c = $item->getSku();
$sObject2->Quantity__c = $item->getQtyToInvoice();
}
echo $_grand;
echo $custname;
?>
When i try to echo the customers name and grand total, i get a blank for the total and Guest for the customer name. Even if i make the $orderId a number of an order this happens.
When you're passing in the number of an order are you passing the entity_id or the increment_id? Usually the number you see in admin will be the increment_id. Look in the address bar of the browser to pick out the entity_id.
$order = Mage::getModel('sales/order')->load($orderId); // $orderId should be the entity_id.
Try this method of retrieving the order instead (Works on the success page):
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order = Mage::getModel("sales/order")->loadByIncrementId($orderId);
Also, make sure to call the correct method for getting the grand total:
$_grand = $order->getGrandTotal(); // Gets the grand total in the store currency.
$_grand = $order->getBaseGrandTotal(); // Gets the grand total in the base currency.
It's probably more useful for your tracking purposes to use the base currency. If you only have one store, or use one currency, it probably won't make any difference. But, if you ever use multiple currencies you'll need to get this right.
i am using these functions please take a look :
function get_all_orders($fromdate,$todate)
{
$getsales = mysql_query("SELECT * FROM `sales_flat_order` WHERE `created_at`>='$fromdate' AND `created_at`<='$todate'");
if(mysql_num_rows($getsales)> 0)
return $getsales;
else
return FALSE;
}
function num_items_under_order($order_entity_id)
{
$getorder_num = mysql_query("SELECT * FROM `sales_flat_order_item` WHERE `order_id`='$order_entity_id'");
if(mysql_num_rows($getorder_num) == 1)
return TRUE;
elseif(mysql_num_rows($getorder_num) > 1 )
return FALSE;
}
function get_order_item_details($order_entity_id)
{
$getsales = mysql_query("SELECT * FROM `sales_flat_order_item` WHERE `order_id`='$order_entity_id'");
if(mysql_num_rows($getsales) == 1)
{
return mysql_fetch_object($getsales);
}
elseif(mysql_num_rows($getsales) > 1)
{
return $getsales;
}
else
return FALSE;
}
Thanks
I want to create invoice in to magento store using magento api in php.For that I want to create invoice for particular quantity and item means If anyone wants to invoice one item in paricular quantity then It shoud be done.My code is working for array() or all quantity.
Below is pseudo code for creating invoice
$client = new Zend_XmlRpc_Client('http://127.0.0.1:8080/AndroidMagento/api/xmlrpc')
$session = $client->call('login', array('tester','tester'));
$saleorderno = '100000007';
Mage::init();
$order = Mage::getModel('sales/order')->load($saleorderno);
$orderItems = $order->getAllItems();
$invoiceItems = array();
foreach ($orderItems as $_eachItem) {
$invoiceItems[$_eachItem->getItemId()] = $_eachItem->getQtyOrdered();
}
$result = $client->call('call',array($session,'sales_order_invoice.create',array($saleorderno,array('order_item_id' => 9474, 'qty' => 1),'Invoice Created by Test',false,false)));
I have seen this link where i found somewhat idea but i can't understand exactly.I can't understand how to get value of order_item_id.???
Any idea??? Please suggest me Thanks in advance...
item_id and product_id are different id.
order or quote has item_id and product_id.
You can try this:
$order = Mage::getModel('sales/order')->load($saleorderno);
$orderItems = $order->getAllItems();
foreach ($orderItems as $item){
print_r($item->getData());
print_r($item->getItemId()); //magic function to get ['item_id']
}
You can do it in 'sales/quote' Model.
Cheers ^^
Try this,
echo "<pre>";
$result = $client->call($session, 'sales_order.info', 'orderIncrementId');
print_r($result['item_id']);
print_r($result['product_id']);
and the $result will return all info of order including item_id and product_id,
with $result['item_id'] you can pass it to call
sales_order_invoice.create
then do
$result = $client->call(
$session,
'sales_order_invoice.create',
array('orderIncrementId' => '200000008',
array('order_item_id' => $result['item_id'],
'qty' => $result['total_qty_ordered'])
)
);
and the qty, You have to get it from $result['total_qty_ordered']
First, try to print_r[$result]; then You'll get some hints from it.
^^
In our order proces it is possible to send an invoice for a partial order. So when a couple of order lines are being shipped, an invoice have to be send also.
To make this possible I use this code:
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($items);
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_ONLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$invoice->sendEmail();
$invoice->setEmailSent(true);
$invoice->save();
Where the $items variable is an array containing the order ids and the amount of products to be invoiced.
The created invoice shows the correct products to be invoiced, but somehow the totals aren't updated. The totals still are the totals of the complete order, instead of the partial invoice.
I probably have to update or recalculate the totals but can't find the right code to force the update.
Anyone around who can put me in the right direction?
Well, it seems I have found the problem. The functionality as described above works manually executing it in the administrator interface. The code as enclosed above I only got to work by changing a core file of Magento.
If you change line 103 of Mage_Sales_Model_Service_Order from continue; to $qty = 0; the functionality works.
In short, this is what happens. With continue the second row item isn't added to the invoice which the invoice makes thinks the curren item is the last item of the whole order and therefore needs to invoice the complete outstanding amount. In my case the invoice I did want to invoice and the row I didn't want to invoice.
I've submitted it as issue on the Magento issue list.
Today I faced with exactly this problem, but I found a more elegant way to solve it without editing the core. The solution is to pass the products that we don't want to invoice, with 0 quantity.
In this way, the code you changed in core will act exactly like in your solution :)
As an example if I have 2 products in my order:
array(
1234 => 1,
1235 => 2
)
passing this array:
$qtys = array(
1234 => 1,
1235 => 0
)
will force this code:
// Mage_Sales_Model_Service_Order: lines 97-103
if (isset($qtys[$orderItem->getId()])) { // here's the magic
$qty = (float) $qtys[$orderItem->getId()];
} elseif (!count($qtys)) {
$qty = $orderItem->getQtyToInvoice();
} else {
continue; // the line to edit according to previous solution
}
to act exactly like in your solution, so you don't have to edit core code.
Hope it helps :)
OK - took me a bit, but now I see how to correctly create the array.
foreach ($items as $itemId => $item) {
$itemQtyToShip = $item->getQtyToShip()*1;
if ($itemQtyToShip>0) {
$itemQtyOnHand = $stockItem->getQty()*1;
if ($itemQtyOnHand>0) {
//use the order item id as key
//set the amount to invoice for as the value
$toShip[$item->getId()] = $itemQtyToShip;
} else {
//if not shipping the item set the qty to 0
$toShip[$item->getId()] = 0;
}
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($toShip);
This creates a proper invoice.