how I can get all product which i buy - magento

I try write this code
public function allsalesblock(){
echo 'other block ';
$orders = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('*')
->addFieldToFilter('customer_id', Mage::getSingleton('customer/session')->getId());
->addAttributeToSort('created_at', 'DESC');
$order = Mage::getModel("sales/order")->load($orders); //load order by order id
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){
echo $item->getItemId(); //product id
echo $item->getSku();
echo $item->getQtyOrdered(); //ordered qty of item
echo $item->getName();
}
}
But this doesn't work - i see white screen. I found this code(and modification) here How can I display all products bought by a customer in magento?

You can get it using below code,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$email = 'albert#example.com';
$_orders = Mage::getModel('sales/order')->getCollection()->addFieldToFilter('customer_email',$email);
foreach ($_orders as $order) {
$id[]= $order->getId();
}
//print_r($id);
foreach ($id as $order_id) {
$order = Mage::getModel('sales/order')->load($order_id);
#get all items
$items = $order->getAllItems();
$itemcount= count($items);
$data = array();
$i=0;
#loop for all order items
foreach ($items as $itemId => $item)
{
$data[$i]['name'] = $item->getName();
$data[$i]['price'] = $item->getPrice();
$data[$i]['sku'] = $item->getSku();
$data[$i]['id'] = $item->getProductId();
$data[$i]['qty'] = $item->getQtyToInvoice();
}
#displaying the array in order to see the products
echo '<pre/>';print_r($data);
}
here change the email address as you want to get products which that customer bought,
$email = 'albert#example.com';

Related

Magento - Get customer gender from order query

I have made a query that gets customer's orders. Can I get customer gender based on this query? If not, how can I get customer gender? This is the code I have so far:
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productId = 297;
$orders = Mage::getModel('sales/order')->getCollection();
$orders
->getSelect()
->joinLeft(
array(
'order_item' =>Mage::getSingleton('core/resource')->getTableName('sales/order_item')),
'order_item.order_id = main_table.entity_id',
array('product_id'=>'product_id'))
->where('product_id=?', $productId)
->group('main_table.entity_id')
->order('main_table.entity_id DESC');
$fields = "date,company,name,country,city,address,postcode,itemname,quantity,price,sku \n";
foreach($orders as $order):
$order->getData();
$order->getIncrementId();
$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();
$billingStreet = $billingAddress->getStreet();
$countryCode = $billingAddress->getCountry();
$country = Mage::getModel('directory/country')->loadByCode($countryCode);
$fields .=
date('Y-m-d',strtotime($order->getCreatedAt())).','.
$billingAddress->getCompany().','.
$billingAddress->getName().','.
$country->getName().','.
$billingAddress->getCity().','.
$billingStreet[0].','.
$billingAddress->getPostcode().','.
$order->getShippingDescription() .',';
foreach ($order->getAllItems() as $itemId => $item):
$itemname = $item->getName();
$itemname = str_replace('&', " ", $itemname);
$fields .=
$itemname. ','.
$item->getQtyOrdered().','.
$item->getPrice().','.
$item->getSku().',';
endforeach;
$fields = rtrim($fields, ',');
$fields .= "\n";
endforeach;
$name = date('d-m-Y-H-i-s');
$output = fopen('backend/assets/csv/' . $name . '.csv', 'w');
fwrite ($output,$fields);
fclose ($output);
foreach($orders as $order){
$customer = $order->getCustomer();
Mage::log($customer->getGender());
}
if this does not work then:
foreach($orders as $order){
$customer = $order->getCustomer();
$customerObj = Mage::getModel('customer/customer')->load( $customer->getEntityId());
Mage::log($customerObj->getGender());
}

get qty of products in an order with several products

Hi i would like to know how to get the qty_ordered of a specific product in an order with several products.
i have tried these
$orders = Mage::getModel('sales/order')->getCollection()->
addAttributeToSelect('shipping_description')->
addAttributeToSelect('increment_id')->
addAttributeToSelect('base_grand_total')->
addAttributeToSelect('total_qty_ordered')->
addAttributeToSelect('shipping_address_id')->
addAttributeToSelect('billing_address_id')->
addAttributeToSelect('created_at')->
addAttributeToSelect('shipping_incl_tax')->
addAttributeToFilter('status', 'pending');
but addAttributeToSelect('total_qty_ordered')-> which total? which product?
"total_qty_ordered" give total no orders qty of all product in an order
If,you want item qty of product then
you can try
<?php $order_id = 2314; //use your own order id
$order = Mage::getModel("sales/order")->load($order_id); //load order by order id
$ordered_items = $order->getAllItems();
foreach($ordered_items as $item){ //item detail
echo $item->getItemId();
//product id
echo $item->getSku();
echo $item->getQtyOrdered();
//ordered qty of item
echo $item->getName();
// etc.
} ?>
for me it worked like this:
<?php
$_order = $this->getOrder(); //call the order
$order_qty = floor($_order->getData('total_qty_ordered')); //get qty of all items rounded to full number (without 3.0000 or so)
echo $order_qty;
?>
used it on template/sales/order/totals.phtml
You can Try this also:-
$orderCollection = Mage::getModel('sales/order');
$order_data = $orderCollection->getAllVisibleItems();
foreach ($order_data as $key) {
$id = $key->getProductId();
$orderItems = Mage::getResourceModel('sales/order_item_collection')
->addAttributeToFilter('product_id', $id);
foreach ($orderItems as $key ) {
echo $number_of_prod = $key->getQtyOrdered();
}
}

How do I get the shipping address and order status in Magento?

I have this code:
$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item) {
echo $item->getName();
echo $item->getId();
echo Mage::helper('core')->formatPrice($item->getPrice());
}
But I want to get shipping address and order status of each item I ordered.
To get all orders for the current log in customer
$customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
$_orders = Mage::getModel("sales/order")->getCollection()
->addAttributeToSelect('*')
->addFieldToFilter('customer_id', $customerId);
/* to get last order only -- uncomment
->setOrder('created_at', Varien_Data_Collection_Db::SORT_ORDER_DESC)
->setPageSize(1);
*/
foreach($_orders as $order) {
foreach($order->getAllItems() as $item){
echo $item->getName();
echo $item->getId();
echo Mage::helper('core')->formatPrice($item->getPrice());
}
//display status
echo $order->getStatusLabel();
//display shipping address
print_r($order->getShippingAddress()->getData());
}
To get order info for a particular order by order_id then
$order_id = 123; // put your order id here
$_order = Mage::getModel('sales/order')->load($order_id);
if(!$_order->getID()){
echo 'Order not found.';
}
else{
//display shipping address
print_r($_order->getShippingAddress()->getData());
foreach($_order->getAllItems() as $order) {
echo $order->getName();
echo $order->getId();
echo Mage::helper('core')->formatPrice($order->getPrice());
echo $order->getStatusLabel();
}
}
This code get the customer cart (so this is before you place an order)
$session = Mage::getSingleton('checkout/session');
Get shipping details by order id
$order_id = 2314; //use your own order id
$order = Mage::getModel("sales/order")->load($order_id); //load order by order id
$shipping_address = $order->getShippingAddress();
echo $shipping_address->getTelephone();
echo $shipping_address->getPostcode(); //use print_r($shipping_address->getData()); to get all the available elements of the object

Magento - Accessing a customer's wishlist

I would like to be able to load a customer's wishlist and return the product id's of the products within the list
I am using:
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
The problem is that the arrays in the Item Collection are protected and I can't find any methods to extract the data.
Is there another way to do this?
You're very close to your target.
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
if (count($wishListItemCollection)) {
$arrProductIds = array();
foreach ($wishListItemCollection as $item) {
/* #var $product Mage_Catalog_Model_Product */
$product = $item->getProduct();
$arrProductIds[] = $product->getId();
}
}
The array variable $arrProductIds will now contain the list of all Product IDs that have been wishlisted by that specific Customer.
Your code is correct. There may be customer is not loaded. here is code.
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}
In any template, using magento 1.8, this works
Total: <?php echo $this->helper('wishlist')->getItemCount() ?>
// Items
$this->helper('wishlist')->getWishlist()->getItemCollection();
$wishList = Mage::getSingleton('wishlist/wishlist')->loadByCustomer($customer);
$wishListItemCollection = $wishList->getItemCollection();
foreach ($wishListItemCollection as $item)
{
//do your thing e.g. echo $item->getName();
}
Try this with product all details like name, images etc...
<?php
$customer = Mage::getSingleton('customer/session')->getCustomer();
if($customer->getId())
{
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
echo $item->getName()."</br>";
echo $item->getId()."</br>";
echo $item->getPrice()."</br>";
echo $item->getQty()."</br>";
$item = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
if ($item->getId()) :
?>
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'small_image')->resize(113, 113); ?>" width="113" height="113" />
<?php endif; } } ?>

show count of only configurable products of current category

i want to show count of only configurable products of current category on category page. for that I have written following code...
<?php $cate = Mage::registry('current_category')->getName();
$total=0;
$category = Mage::registry('current_category');
$products = $category->getProductCollection();
foreach ( $products as $_product )
if ($_product->isConfigurable())
{
$total++;
}
echo $cate."(".$total.")"; ?>
my problem is the code is showing the total count of configurable products of all child categories... can anyone help me with this?
try this
<?php
$cate = Mage::registry('current_category')->getName();
$total = 0;
$category = Mage::registry('current_category');
$products = $category->getProductCollection();
foreach ( $products as $_product ){
if ($_product->getType_id()=="configurable"){
$total++;
}
}
echo $cate."(".$total.")";
?>
$category = Mage::registry('current_category');
$products = $category->getProductCollection()
->addAttributeToFilter('type_id', 'configurable');
$total = $products->getSize();
echo $this->__('%s (%d)', $category->getName(), $total);

Resources