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 ...
Related
I'm trying to exclude/ignore a specific item from a 'count' in an array which generates a CSV file.
I generate a daily CSV file of orders from Woocommerce;
Each Order is made up of a quantity of varying single products;
I define the CSV Headers manually -eg:
$csv_header = array('Order ID','Company Name','Contact Name','Address 1','Address 2','Suburb','Product 1', 'Product 2', 'Product 3','#Items')
I pull the metadata based on Order IDs:
$result = $wpdb->get_results('SELECT * FROM `'.$wpdb->prefix.'postmeta` WHERE post_id = '. (int)$order_id->ID);
I create an instance of each order and get the items:
$order = new WC_Order((int)$order_id->ID);
$items = $order->get_items();
After getting the details and setting the column for those - eg:
if( $res->meta_key == '_shipping_company'){
$order_line[2] = $res->meta_value;
I then loop to create the invidual item product count and the #items total
foreach ($items as $item ) {
$_count = array_search( $item['name'], $csv_header );
$order_line[$_count] = $item['item_meta']['_qty'][0];
$row_items += $order_line[$_count];
}
$order_line[10] = $row_items;
and add blanks to products not in the order:
for ($i = 0; $i < 37; $i++) {
if( !isset($order_line[$i]) && empty($order_line[$i]) ) {
$order_line[$i] = '';
}
This all works well, but now I need to EXCLUDE a product NOT defined in the $csv_header from the count - bearing in mind that the Order will contain defined products. eg: count products 1-3 but not Product 4.
At the moment if a Product is not defined in $csv-header, it will put a number in my first column in place of the order-id instead of ignoring it.
Any suggestions appreciated.
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>";
}
}
I would like to get all items that has not been shipped for a given order.
Suppose that I have a partial shipped order I would like to retrieve all items that are pending to be shipped.
Something like:
$collection = Mage::getModel('...')->getCollection()
->Add...()
To get all items of the order, use the item collection:
$order = Mage::getModel('sales/order')->load($orderId);
$orderItems = $order->getItemsCollection();
And then use the shipment model to get all the items of the order:
$shippedItems = Mage::getModel('sales/order_shipment')
->setOrder($order)->getItemsCollection();
Edit2: Turns out that setting the order on the shipment does not work to get the items collection, since there can be several shipments for an order. Use the code snippet provided below, instead.
Edit: I forgot that there could be several shipments, so here is the full working code to get all Ids of the order items and all Ids of the shipped ones:
$orderId = 15;
$order = Mage::getModel('sales/order')->load($orderId);
$orderItems = $order->getItemsCollection();
$allOrderItemIds = $orderItems->getAllIds();
$shipments = $order->getShipmentsCollection();
$shippedItemIds = array();
foreach ($shipments as $shipment) {
$shippedItems = $shipment->getItemsCollection();
foreach ($shippedItems as $item) {
$shippedItemIds[] = $item->getOrderItemId();
}
}
var_dump($allOrderItemIds);
var_dump($shippedItemIds);
$orderId = 382;
$order = Mage::getModel('sales/order')->load($orderId);
// OR if you load the order with increment id ...
// $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$orderItems = $order->getItemsCollection();
foreach ($orderItems as $item) {
$qtyToBeShipped = $item->getQtyOrdered() - $item->getQtyShipped();
if ($qtyToBeShipped)
echo $item->getId() . ' - ' . $qtyToBeShipped . '</br>';
}
I want to retrieve all product IDs that has a specific (custom) option value (provided by the customer in the frontend) from all orders - regardless of store/website.
The option is a text field (Mage_Catalog_Model_Product_Option_Type_Text), the SKU (and title) is test_option and the value I'm looking for is green.
I would prefer to do this with a raw MySQL query, but a solution using Magentos models/collections would definitely suffice.
try with below code
<?php $collection = Mage::getResourceModel('sales/order_collection');
$productIds = array();
foreach($collection as $order) {
$order = Mage::getModel('sales/order')->load($order->getId());
$items = $order->getAllItems();
foreach ($items as $item){
$proOptions = $item->getProductOptions();
if($proOptions['options']){
$productIds[] = $item->getProductId();
}
}
}
?>
$productIds variable returns all product ids which has a specific (custom) option value.
May it helps you !
For direct sql query you can try this. If you want to put some condition, you can add according to your requirement.
SELECT sfoi.product_id FROM sales_flat_order e
LEFT JOIN
sales_flat_order_item sfoi
ON
( e.entity_id = sfoi.order_id )
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();