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;
Related
Ok I want to display the latest Order id as a unique string without the #
I have used this code to attempt to display on checkout page but it displays nothing.
<?php print $order->get_id();?>
Also i would like a function that allows the place order button on the checkout page to run my custom gateway when its pressed currently I am doing this using html.
If i'm understanding correctly your function get_id() is returning something like "#1234";
In this case you could just do a str_replace(). For example:
$id = "#1234";
$id = (int) str_replace("#", "", $id);
If not, please edit your question.
So I have to send variables to an affiliate website and they need the order id, price, 2-letter state abbriviation, and the country abbreviation. I have already got the price and country abbr. but I still need the order id and the 2-letter state abbreviation. The code I have right now is as follows:
$order = Mage::getSingleton('checkout/session')->getLastRealOrderId();//doesnt work
$amount = Mage::getModel('checkout/cart')->getQuote()->getSubtotal();//works
$stateId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('region');//Gives full name of State
$countryId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('country_id');//works
echo " display: $order $amount $stateId $countryId";//prints out the variables
I have been looking on all the code on here for the order id but nothing has returned anything. So I'm wondering what I am doing wrong with that/why it is not printing out anything.
The second thing is that I am wondering if there is an easy way to get the 2-leter state abbreviation? I have also tried 'region_id" instead of 'region' but that just gives me the number code (not the letter code).
Answers to either of these problems would be greatly appreciated.
Once you have the order loaded you can call 'region_code' or getRegionCode(). I'm using magento 1.9. Not sure if it is available in previous versions.
The best way is to send this information through the success.phtml file as only in that file you will get that info and avoid sending order ids for uncompleted orders (failed paypal transactions etc.)
1) depending on your checkout method, you may or may not have the state code in your data.
So if you get the name of states by
$stateId = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress()->getData('region');
then add this code afterwards:
$states = Mage::getModel('directory/country')->load('US')->getRegions();//get list of states
foreach($states as $state){
if($state[default_name] == $stateId){ //check if the names match
echo $state[code]; //get code
}
}
2) On success page
$orderId = $this->getOrderId();
should work.
I'm using woocommerce and installed a print delivery note plugin. What I am trying to do is edit the print file so if someone selects a certain 'additional shipping rate', it will print the relevant printed postage impression - 1st class vs 2nd class
I have in my head an IF statement based on the value of the shipping (FREE vs £1) which I can I do.
BUT, I'm having problems trying to extract the value so I can check it.
Digging deep it looks like I need to use the get_shipping() method in the WC_Order class so I can check the value - http://docs.woothemes.com/wc-apidocs/class-WC_Order.html - and this is where I fail.... miserably.
Looking at this page - http ://docs.woothemes.com/document/class-reference/#listofclassesinwoocommerce - I can copy and paste the WC_Cart example directly into the .php file that is used to print from and echo the value successfully, but when I change it to what I think is correct to use get_shipping(), I get "Fatal error: Call to a member function get_shipping() on a non-object in....." instead.
Here's what I put
<?php global $woocommerce;
$order_shipping_total = $woocommerce->order->get_shipping();
echo $order_shipping_total ;
?>
Now I am guessing, as I have reached the limit of my ability, I need to initialise the WC_Order class but I'm stumped at how to do this.
Any help would be greatly received.
Cheers
You were almost there! You need to initialise the order indeed. $woocommerce has no idea which order you are talking about. So you create an object from the order:
$order = new WC_Order( $order_id);
Now all information from this order (including your shipping value) is accessible via $order. The only thing you need to know is the appropriate $order_id.
Since you are in the print delivery notes environment, you can access that value via
$wcdn->print->order_id.
this will do what you need:
<?php
global $wcdn;
$order = new WC_Order($wcdn->print->order_id);
$order_shipping_total = $order->get_total_shipping();
echo $order_shipping_total;
?>
from > WC 3.you can get the total shipping cost of the current order with
<?php
$current_shipping_cost = WC()->cart->get_cart_shipping_total();
echo $current_shipping_cost;
?>
[2018 Update]
You can use
<?php
$order = wc_get_order( $order_id );
$order_shipping_total = $order->get_shipping_total();
echo $order_shipping_total;
?>
I hope someone can help me with this, I am trying to extract the product prices from magento based on customer group.
I am not using tier pricing, I simply have a product with a standard price and I have specified different prices for each customer group. For some reason I have been unable to extract this information.
I can see that the price mappings seems to be held in the table 'catalog_product_index_group_price' so I guess I could write direct SQL to extract these but I would much rather use the PHP Mage model to do this, or the V2 SOAP API.
I have tried many methods, currently im using something like below, but without success the price variable is always empty.
$rules = Mage::getResourceModel('catalogrule/rule');
$price = $rules->getRulePrice($now, $websiteId, $customer_group_id, $productID);
Please try the following
$product = Mage::getModel('catalog/product')->load($productId);
$groupPrices = $product->getData('group_price')
$groupPrices should now be an array with the data you are looking for.
the code didnt format well in the comment so here it is again!
<?php
include_once '../App/Mage.php';
Mage::app();
$productID = $_GET["id"];
$pd = Mage::getModel('catalog/product')->load($productID);
$groupPrices = $pd->getData('group_price');
echo json_encode($groupPrices);
?>
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