I want to send a weekly reminder email to customers who have bought something but not paid yet.
Using this script I can show an order:
<?php
//from http://www.exploremagento.com/magento/run-magento-code-outside-of-magento.php
require_once '../app/Mage.php';
umask(0);
Mage::app('default');
$orders = Mage::getModel('sales/order')->getCollection()
->setOrder('increment_id','DESC')
->setPageSize(1)
->setCurPage(1);
$LastOrderNr = $orders->getFirstItem()->getIncrementId() - 100000000;
$LastOrderNr=494;
$order = Mage::getModel('sales/order')->load($LastOrderNr); //put a valid order entity_id there;
print_r($order->debug());
echo "<br/>******************************<br/>";
?>
I can retrieve several useful info-blocks (i.e. customer's email address, total amount) but it won't show the items they have bought. How/where can I retrieve that kind of information?
In Magento ordered products called 'order items'. You can get order items data like this:
`
// 1. get order. you can load it by order entity id (f.e. 494)
$order = Mage::getModel('sales/order')->load($orderId);
// or by order increment id (100000494)
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
// 2. get order items collection
foreach ($order->getItemsCollection() as $item) {
Zend_Debug::dump($item->getData());
}
`
it can be much easier to get the last Order , especialy needed for the success Site
$order = Mage::getModel('sales/order')->load(Mage::getModel('sales/order')->getCollection()->getLastItem()->getEntityId());
Example: Last Order Costs
$grand_total = Mage::getModel('sales/order')->load(Mage::getModel('sales/order')->getCollection()->getLastItem()->getEntityId())->getGrandTotal();
Related
In Woocommerce, I am trying to get the last order id using the following code:
<?php $order = new WC_Order($post->ID);echo $order->id;//to escape # from order id $order_id = trim(str_replace('#', '', $order->get_order_number())); ?>
But it doesn't work as I am getting a zero value.
The purpose is to add 1 to this last order ID, to get the new usable order ID.
Any help is appreciated.
It seems that you would like to get the next usable POST ID (Order Id), to save it, for example, as new Order in database with some related data.
This is absolutely not the way to do it and you need to think it different... Now you can use one of the 3 following ways:
Using the WordPress dedicated function wp_insert_post() that returns the post ID (Order ID).
Using the Woocommerce dedicated function wc_create_order() that returns the WC_Order Object.
Then from the the order object, you can get the order ID using $order->get_id().
Using the Woocommerce empty WC_Order object instance and the save() method:
// Get an empty instance of the `WC_Order` Object
$order = new WC_Order();
// Save the order to the database
$order->save();
// Get the Order ID
$order_id = $order->get_id();
Addition - Get the last Order ID in Woocommerce:
To get and display the last order ID in woocommerce use a WC_Order_Query in this two simple line:
<?php
$last_order_id = wc_get_orders(array('limit' => 1, 'return' => 'ids')); // Get last Order ID (array)
echo (string) reset($last_order_id); // Displaying last order ID
?>
Tested and works
I am trying to place an order from multiple restaurants. When I place an order, same Order ID is adding in the database for orders. What I would like to have is, When the user places an order from multiple restaurants, I would like to have separate Order ID for them.
Here is my code:
$order_id = $this->generateNewOrderId();
foreach ($cart_content->rest_item_list as $item)
{
$td_data['order_id'] = $order_id;
$td_data['item_id'] = $item->id;
$i_details = $this->restaurant_menu_items_model->get_item_detail_by_id($item->id);
$td_data['item_name'] = $i_details->name;
$td_data['item_price'] = $i_details->price;
$td_data['item_quantity'] = $item->quantity;
}
Well this stands to reason, since you're generating an order ID only once, outside the loop, not once per order. Just move it inside the loop:
foreach ($cart_content->rest_item_list as $item) {
$order_id = $this->generateNewOrderId();
$td_data['order_id'] = $order_id;
//... etc ...
I'm building an extension that exports completed order information into a CSV file. I've been able to successfully access order and item information for the export but I'm having a problem accessing promotion data.
I'm looking to include the promotion data (catalog and cart rules) that are applied to that order in the export. Specifically I am trying to access the rule ids, names, descriptions, codes and applied dollar amount for a list of specific orders.
How would I access the promotion data in my extension?
If it helps, here is how I am currently accessing order/item data in my extension (qualifying by date frame):
$rows = array(); // array to hold data that will be exported to CSV
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$orders = $read->query("SELECT entity_id FROM sales_flat_order WHERE created_at >= '$from' AND created_at < '$today'");
while ($row = $orders->fetch()) {
$order = Mage::getModel('sales/order')->load($row['entity_id']);
$order_items = $read->query("SELECT item_id FROM sales_flat_order_item WHERE order_id={$order->getId()}");
while ($item_row = $order_items->fetch()) {
$item = Mage::getModel('sales/order_item')->load($item_row['item_id']);
$rows[] = array(
$order->getRealOrderId(),
$order->getCreatedAt(),
$item->getSky(),
$item->getQtyOrdered(),
..... );
); // end item loop
); // end order loop
Thanks in advance for any help you can provide,
-Mark
I believe this question has been answered on another question in the past. You can find the link to the original question and answer here Magento - get price rules from order
From that answer though..
//The order I want to check
$order_id = 859;
//Get the list of items for your order
$items = Mage::getModel('sales/order_item')
->getCollection()
->addFilter('order_id',array('eq'=>$order_id));
//loop through each item
foreach($items as $item){
//if the item has not had a rule applied to it skip it
if($item->getAppliedRuleIds() == '')continue;
/*
* I cant remember in the database they might be comma separated or space if multiple rules were applied
* the getAppliedRuleIds() function is the one you want
*/
foreach(explode(",",$item->getAppliedRuleIds()) as $ruleID){
//Load the rule object
$rule = Mage::getModel('catalogrule/rule')->load($ruleID);
// Throw out some information like the rule name what product it was applied to
echo "<p>".$item->getSku()." had rule ".$rule->getName()."(".$item->getAppliedRuleIds().") applied </p>";
}
}
does anyone have an idea on how I could send newsletter emails to customers based on the category that they ordered from? So for instance, i would like to send a monthly email to customers who purchased exam gloves to restock their supply.
Here is one way to go about it:
1) get all of the (recent) orders
$orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', '2012-04-16 15:56:33');
Note: replace '2012-04-16 15:56:33' with correct date and timestamp.
2) Get the product's ordered
foreach($orders as $order):
// let's get some the order info
$orderData = $order->getData();
// extract the order ID from the order objecj (so we can load the whole order)
$orderId = $orderData['entity_id'];
// extract the customer's ID (so that we can find out customer's email)
$customerId = $orderData['customer_id'];
// load the customer by ID
$customer = Mage::getModel('customer/address')->load($customerId)->getData();
// get customer's email address
$customerEmail = $customer['email'];
// load the full order (so that we can get a list of product's ordered
$fullOrder = Mage::getModel('sales/order')->load($orderId);
// extract the products from the order
$products = $fullOrder->getAllItems();
endforeach;
3) Find out what category the product comes from
foreach ($products as $product):
// let's get an object with the ordered product's data
$productInfo = $product->getData();
// extract the product ID (so that we can load the product)
$prodId = $productInfo['item_id'];
// load the product
$product = Mage::getModel('catalog/product')->load($prodId);
// get all (names of) categories that this product is associated with
$categories = $product->getCategoryCollection()->addAttributeToSelect('name');
endforeach;
4) Send out a specific template to those customers (see code in first answer of this question) Sending e-mail programmatically in Magento is failing
Hope this was helpful
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()