Getting previous order id in an Observer - php

I'm writing an observer for the *checkout_submit_all_after* event and it works fine. When you edit an order, for example the #1001, magento creates a new one with #1001-1 and cancel the previous order.
The admin panel tells me that #1001-1 is linked to the #1001, so how can I know if the current order is actually a modified version?
Is there some function/variable for this purpose?
thanks

I did it!
$current_id = $order->getRealOrderId(); // #1001-2
$previous_id = $order->getRelationParentRealId(); // #1001-1
$older_id = $order->getOriginalIncrementId(); // #1001

Related

Get virtuemart order status after chage

My problem is, that second parameter in plgVmOnUpdateOrderPayment event is actually old status code before change.
Did anybody knows, how to get new status letter after changing order status via e.g. paypal plugin or in administration?
Aleš Pázner, yes the second parameter is always the old order status.
But you can use this piece of code:
function plgVmOnUpdateOrderPayment($virtuemart_order,$old_status) {
// getting the new status
// $virtuemart_order->order_status
return;
}
Source: Plugin event methods in Virtuemart for order status

Prestashop 1.6 unable to find Order via Cart ID

I am using a payment module with Prestashop 1.6, everything was working fine until an upgrade happened. The payment server return the validation data to an url like "http://..../validation.php".
This file tries to get the order using the cart id that is returned by the server and obviously cant find it, hence the cart is not emptied and the order not created.
I added logs into that file to see what is received. The CartID (data['reference']) is correct.
writeMessage("Trying to get the order id using the cart:".$data['reference']);
writeMessage(Order::getOrderByCartId((int)($data['reference'])));
if ($id_order = intval(Order::getOrderByCartId((int)($data['reference']))))
{
writeMessage("Got the order by cart id.");
writeMessage("Got the order by cart id:".$id_order);
$order = new Order($id_order);
...
I am wondering if the call to Order::getOrderByCartId is correct. Is it the right way to call this function?
Any idea?
I can answer my question. The issue was not on the call to getOrderByCartId as it is normal that at this moment the order is not created yet.
The issue is actually on the validation function of the module, second part of the if.
$module->validateOrder(intval($data['reference']), $orderStatus, $amount, $module->displayName, $orderMessage, NULL, $id_currency, true, $customer->secure_key);

Magento sending SMS upon creating shipment

Banging my head for the last two days but unable to achieve this. Help!!
Whenever a shipment email is communicated I want to trigger a code which will send a SMS to the customer informing him/her that his order has been dispatched and also communicate the tracking number.
The code will be something like this:
<?php
$url = "http://www.abcde.in/binapi/pushsms.php?usr=xyz&pwd=abc&sndr=MEGYTR&ph=8888829554&text=This is test. MegaYtr&rpt=1";
$result = file_get_contents($url);
?>
Questions:
1) From where do I run this code?
2) How do I get additional info like Order Number, Customer Name, Grand Total & Tracking Number. I had done something similar for sending SMS when customer places order at that I used this code:
$order_id = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order_details = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$shipping_address_data = $order_details->getShippingAddress();
$first_name = $shipping_address_data['firstname'];
$telephone = $shipping_address_data['telephone'];
$amount_paid = $order_details->total_paid;
$message = 'Dear '.$first_name.' thank you for shopping at abc.com. Your order'.$this->getOrderId().' amounting to Rs.'.$amount_paid.'is being processed.';
echo '<b>'.$message.' Please check your email for further details.</b>';
I am using Magento Community 1.7.0.1.
try this
i would like to give you an simple solution without need of modifying core files.
for that you need to create an observer for SuccessAction below is the event that will trigger your code when an order is successfull
checkout_onepage_controller_success_action
this will help you in creating observer for the above event create observer using this
one more thing i would like to add is in the controller at location Mage/Checkout/OnepageController search for successAction, this is the Action which is processed on the succes of order. Here on line 240 if you comment $session->clear(); than you would not require to place order again and again, just by refreshing the page you can check your changes.
And lastly FYI above event will dispatch orderId by using that you can load the order object, for doing that the below is the code
//load order object
$_order = Mage::getModel('sales/order')->loadByIncrementId($order_id_from_observer);

Magento - Programmatically reorder

I am currently making a module that requires me to take an order object and make it reorder itself.. thus, creating a new order in the backend with the exact same items and credentials.
This is the code that i have thus far… it doesn’t seem to reorder the item or create and add another backend order.
$personsOrder = Mage::getModel(’sales/order’);
$personsOrder->loadByIncrementId($order[’model_order_id’]);
$order_model = Mage::getSingleton(’adminhtml/sales_order_create’);
$personsOrder->setReordered(true);
$order_model->initFromOrder($personsOrder);
/*
$order_model->save();
$order_model->place();
$order_model->sendNewOrderEmail();
*/
Any help is greatly appreciated!!
$orderId= $YOUR_ORDER_NUMBER;
$personsOrder = Mage::getModel('sales/order')->load($orderId);
$order_model = Mage::getSingleton('adminhtml/sales_order_create');
$personsOrder->setReordered(true);
$order_model->initFromOrder($personsOrder);
$order_model->createOrder();
My first thought is that you should be using $order->getIncrementId() on line 2 rather than $order['model_order_id'], but I'm not sure where you're getting $order from in the first place. Have you checked that $order['model_order_id'] is actually returning a valid increment ID? I don't see model_order_id as a field in the database anywhere...
I'd be suggesting that you getting your IDE and XDebug working so that you can inspect the objects as you work with them and understand what's going on.
Cheers,
JD
If the order that you have placed the first time around is also created through coding and not from store front then you need to make sure that you have added an entry in the sales_flat_quote_item table. Otherwise that order cannot be reordered. So make sure it's not the case with your order creation.

Get Order Increment Id in Magento

I'm trying to get the Order Increment Id in Magento, on the success.phtml page so that I can use this for affiliate tracking.
I'm using the following code, but it is giving an error on the second line;
$order = Mage::getSingleton('sales/order')->getLastOrderId();
$lastOrderId = $order->getIncrementId();
The error reads:
Fatal error: Call to a member function getIncrementId() on a non-object on line 34: $LastOrderId = $order->getIncrementId();
I was wondering if anyone has any ideas on how to get the Order Increment Id? This is the reference number seen in the admin, usually something like: #1000123
If you're specifically doing this on the checkout success page - in success.phtml - then the code to get the order increment ID is already available in the template, since it is displayed to the customer.
You just need the following:
$orderId = $this->getOrderId();
Note that this won't work on other pages so, for those, you'd need to use:
$orderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
$order in your code is the last order ID...as the function name implies. If this isn't the value you want, then use it to load an order, and then use the getter on that:
$order = Mage::getModel('sales/order');
$order->load(Mage::getSingleton('sales/order')->getLastOrderId());
$lastOrderId = $order->getIncrementId();
This will work perfectly, I m running this one in my module now.
$last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
Hope it helps thanks. :)
Your call to
Mage::getSingleton('sales/order')
isn't returning an object. Try
var_dump(Mage::getSingleton('sales/order'));
to confirm.
I haven't dived into the checkout code recently, but I'm pretty sure that's because sales/order will get you the order in progress. Once the order's been placed it's no longer in progress.
The "right" way to do this would be to create an observer for one of the events that Magento fires during checkout. The
checkout_onepage_controller_success_action
event should be sufficient, assuming you haven't done too much customization of the checkout process.
There's a terse explaination of how to do this on the Wiki (for a different event)
Once you get your event setup and responding, do a
$event = $observer->getEvent();
var_dump($event->getData());
to see what kind of information you have available. Chances are there's an order object in there which will let you get the ID you're after.
I had to use...
$_order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
While in the success.phtml template. Instead of load() I used loadByIncrementId - then my order object was no longer empty.
If you are in admin mode - try this:
$orderModel = Mage::getModel('sales/order');
$orders = $orderModel->getCollection()->setOrder('increment_id', 'DESC')->setPageSize(1)->setCurPage(1);
$orderId = $orders->getFirstItem()->getIncrementId();
getRealOrderId() appears to return the order number as presented in data grids. getId() will return the internal id of row in the database, which you probably don't want.
You can get the increment id using this code snippet:
$orderId = 12;
$order = Mage::getModel('sales/order')->load($orderId);
$Incrementid = $order->getIncrementId();
Now you can do an echo to the $Incrementid variable and see the increment id.
I hope this helps.
$lastOrderIncrementId = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
$shipmentID = $shipment->increment_id;
$order = $shipment->getOrder();
$orderID = $order->increment_id;

Categories