get data from sales order and sales order item in magento - magento

I am working on a magento php code to get the Customer Name, email, product description, sku code, order number and customer id. However I have been feeling a bit stuck when trying to join the "sales/order" and "sales/order_item" models in order to print out the data through a foreach loop and also get only the now date. This is what I got so far:
<?php
set_time_limit(0);
require_once '../app/Mage.php';
Mage::app('uk');
$collection = Mage::getModel('sales/order')
->getCollection()
->addFieldToFilter('store_id', Mage::app()->getStore()->getId())
->addAttributeToFilter('status', Mage_Sales_Model_Order::STATE_COMPLETE)
->addAttributeToSelect('*')
->addAttributeToSort('created_at','DESC');
foreach ($collection as $c) {
echo $c->getCustomerName() . "\t" .
$c->getCustomerEmail() . "\t" .
$c->getCreatedAt() . "\r\n";
}
?>
Could you please give me a hand with this?
Thanks a lot,
Nestor

try below code
foreach ($collection as $c) {
$order = Mage::getModel("sales/order")->load($c->getId());
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
echo $item->getItemId();
echo $item->getSku(); -
}
echo $c->getCustomerName() . "\t" .
$c->getCustomerEmail() . "\t" .
$c->getCreatedAt() . "\r\n";
}

Related

Magento order grid

Hi I need to display Order Details grid in my Magento FRONT END page. I tried different ways, but nothing worked fine.
Also how to find <?php echo $this->getChildHtml('customer.account.dashboard.extra') ?> block.
Any idea please.
Here is the quick way,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app()->getStore();
echo '<pre>';
$orders = Mage::getModel('sales/order')->getCollection()
//->addFieldToFilter('status', 'complete')
->addAttributeToSelect('customer_email')
->addAttributeToSelect('status')
->addAttributeToSelect('increment_id')
;
//echo $orders->getSelect();
//exit;
foreach ($orders as $order) {
$email = $order->getCustomerEmail();
echo $order->getIncrementId() . ": '" . $order->getStatus() . "', " . $email . "\n";
}
It fetches all order details. Please comment here if you have any doubt.

How to get correct product url for multistore magento?

I am running following code in Magento root to get the product urls
<?php
require_once('app/Mage.php');
umask(0);
Mage::app(1);
$collection = Mage::getModel('catalog/product')
->setStoreId(1)
->getCollection();
foreach( $collection as $product )
{
echo $product->getProductUrl();
echo "<br>";
}
?>
I am getting product urls like http://example.com/catalog/product/view/id/5/ , But these urls are invalid.
The product urls are as following in front end http://example.com/product.html
How do I get the correct product urls? I have multi store Magento set-up.
You need to get store url for each product separetly. In other words, you need to use something like this:
$collection = Mage::getModel('catalog/product')
->getCollection();
foreach( $collection as $product )
{
echo $product->setStoreId(5)->getProductUrl();
echo "<br>";
}

fetch product from more than one category

I am trying to fetch product from more than one category and I am using
$category_collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('name', array('like' => '%'.$searchstring.'%'))
->load();
foreach ($category_collection as $category) {
$ids[] = $category->getId();
}
$collection = Mage::getModel('catalog/product')
->getCollection()
->joinField('category_id', 'catalog/category_product', 'category_id', 'product_id = entity_id', null, 'left')
->addAttributeToFilter('category_id', array('in' => array('finset' => $ids)))
->addAttributeToSelect('*')
->setPageSize(5);
but it shows error Item (Mage_Catalog_Model_Product) with the same id "2" already exist
please help me to get distinct collection.
Try adding a group by statement. You get duplicate products because you have the same product in multiple categories and your code retrieves them twice (at least) and a collection does not support items with the same id.
So at the end of your code add this
$collection->getSelect()->group('e.entity_id');
or
$collection->getSelect()->group('main_table.entity_id');
I don't remember exactly what is the table alias.
I get by code:-
// Code to Search Product by $searchstring and get Product IDs
$category_collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('name', array('like' => '%' . $searchstring . '%'))
->load();
foreach ($category_collection as $category) {
$category_id = $category->getId();
$collection = Mage::getResourceModel('catalog/product_collection');
$collection->addCategoryFilter($category); //category filter
$collection->addAttributeToFilter('status', 1); //only enabled product
$collection->addAttributeToFilter('visibility', 4);
$collection->addAttributeToSelect('*'); //add product attribute to be fetched
$collection->addStoreFilter();
foreach ($collection as $_product):
$ids[] = $_product->getId();
endforeach;
}
$ids = array_unique($ids);
$html = '';
foreach ($ids as $_ids):
$product = Mage::getModel('catalog/product')->load($_ids);
$html .= '<img name="'.$product->getPrice().'" alt="'.$product->getName().'" draggable="true" ondragstart="drag(event)" id="' . $product->getId() . '" class="ui-widget-content ui-draggable" src="' . $product->getImageUrl() . '" width="50px" height="50px" />';
endforeach;
echo $html;
$ids will return unique id and in foreach we can load product.

How Do I Output Delivery Instructions for Magento?

I am trying to create a script that I can access via URL so that it outputs delivery details, specifically the fields I need to display are: orderID, status, gift_message and a custom customer address attribute that is called "delivery_instructions". So far I have the following, but I am stuck with trying to get the delivery instructions, any help would be much appreciated.
<?php
require_once 'app/Mage.php';
Mage::app('default');
$myOrder=Mage::getModel('sales/order');
$orders=Mage::getModel('sales/mysql4_order_collection');
$message = Mage::getModel('giftmessage/message');
$customer = Mage::getModel('customer/customer');
$orders->addFieldToFilter('total_paid',Array('gt'=>0)); //Amount paid larger than 0
//$orders->addFieldToFilter('gift_message_id',Array('gt'=>0));
$allIds=$orders->getAllIds();
foreach($allIds as $thisId) {
$myOrder->reset()->load($thisId);
$shippingAddress = $myOrder->getShippingAddress();
$customerDetails = $myOrder->getCustomer();
//echo $myOrder->shippingaddress->getData('delivery_instructions');
$customer_id = $myOrder->getCustomerId() . "',";
echo $customer_id . "',";
echo "'" . $myOrder->getStatus() . "',";
echo "'" . $myOrder->getIncrementID() . "',";
$gift_message_id = $myOrder->getGiftMessageId();
if(!is_null($gift_message_id)) {
$message->load($gift_message_id);
echo $gift_message = $message->getData('message') . "',";
};
$customer = Mage::getModel('customer/customer')->load( $customer_id);
echo $customer = $customer->getData('incrementId');
echo "\r\n";
echo "<br/ >";
}
?>
You are close with this:
//echo $myOrder->shippingaddress->getData('delivery_instructions');
In order to get the shipping address object you need to use:
$myOrder->getShippingAddress()
and to get the delivery_instruction from the shipping address object:
$myOrder->getShippingAddress()->getDeliveryInstructions()
You can create custom address attributes and custom customer attributes.
Customer attributes can be accessed like this:
// A customer is not mandatory for an order.
if($myOrder->getCustomerId()){
$customer = Mage::getModel(’customer/customer’)->load($myOrder->getCustomerId());
$customer->getDeliveryInstructions();
}
Address attributes can be accessed like this:
$myOrder->getShippingAddress()->getDeliveryInstructions();

How to get order list for logged in customer in magento

I am working on getting list of order numbers(name) ordered by a customer. I have tried using
Mage::getModel('sales/order')->load($order_id);
but didn't works for me? Actually i am working on help desk module and trying to assign orders to the tickets.
Ok friends thanks for your hints, I got this by using this
require_once 'app/Mage.php';
Mage::app();
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getCustomer()->getId())
->addFieldToFilter('state', array('in' => Mage::getSingleton('sales/order_config')->getVisibleOnFrontStates()))
->setOrder('created_at', 'desc')
;
$this->setOrders($orders);
foreach ($orders as $order):
echo $order->getRealOrderId().' at '.$this->formatDate($order->getCreatedAtStoreDate()).' ('.$order->formatPrice($order->getGrandTotal()).')';
endforeach;
you can try this:
$yourCustomerId = '123123';
$field = 'customer_id';
$collection = Mage::getModel("sales/order")->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter($field, $yourCustomerId);
echo "<pre>";
print_r($collection);
echo "</pre>";
// if the customer is logged in you can add this
if(!Mage::getSingleton('customer/session')->isLoggedIn()){
$yourCustomerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
$field = 'customer_id';
$collection = Mage::getModel("sales/order")->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter($field, $yourCustomerId);
echo "<pre>";
print_r($collection);
echo "</pre>";
}
You can try this as well,
$customer = Mage::getSingleton('customer/session')->getCustomer();
$email = $customer->getEmail();
Now you have customer email address in your $email variable. So you can easily get order collection using this email address as following:
$orderCollection = Mage::getModel(‘sales/order’)->getCollection();
$orderCollection->addFieldToFilter(‘customer_email’, $email);
foreach ($orderCollection as $_order)
{
echo $_order->getRealOrderId() ;
echo $this->formatDate($_order->getCreatedAtStoreDate()) ;
echo $_order->getShippingAddress() ? $this->escapeHtml($_order->getShippingAddress()->getName()) ;
echo $_order->formatPrice($_order->getGrandTotal());
echo $_order->getStatusLabel();
}
$orders = Mage::getModel('sales/order')->getCollection();
$orders->getSelect()->where('e.customer_id ='.$customer_id);

Resources