get qty of products in an order with several products - magento

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();
}
}

Related

how I can get all product which i buy

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';

Geting quantity for a single item in cart magento

Hello i need to get the quantity of the item added in cart for the item i am viewing in product detail
atm i am using this
<?php $cart = Mage::getModel('checkout/cart')->getQuote();
$result = array();
foreach ($cart->getAllItems() as $item) {
$result['qty'] = $item->getQty();
}
$qty=$result['qty'];
//echo $qty ;
if(!$qty == 0){
echo '<span>'.$qty.'</span>';
}else{
echo "<span>0</span>";
}?>
but it returns only the last qty added/updated in the cart, and i need the specific single product qty
Try this:
//$product is current product that you are viewing
$qty = null;
foreach ($cart->getAllItems() as $item) {
if ($product->getId() == $item->getProductId()) {
$qty = $item->getQty();
}
}
if (!is_null($qty)) {
//$qty is what you are looking for
}
else {
//the current product is not in the cart
}

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 - Product Collection with the current user's Wishlist

Within a Magento php Controller, how can I get a Product Collection containing the products listed in the logged in user's (ie current user's) Wishlist.
I am getting the Wishlist using:
$wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer(Mage::getSingleton('customer/session')->getCustomer());
and this contains the correct number of items.
But I would like to get a Product Collection. I have tried:
$productCollection = $wishList->getProductCollection();
and
$productCollection = $wishList->getProductCollection()->addAttributeToSelect('id')->load();
but the Product Collection I get has a length of 0.
How do I get the Product Collection?
You can use the getWishlistItemCollection (see link for more details) off the wishlist helper to return a collection of items, you then need to get the product from the item.
I have been using the following code to create an associative array of the products, which I then use to determine if a product I am displaying in the list page is in the wishlist...hopefully this will help:
public function getWishList() {
$_itemCollection = Mage::helper('wishlist')->getWishlistItemCollection();
$_itemsInWishList = array();
foreach ($_itemCollection as $_item) {
$_product = $_item->getProduct();
$_itemsInWishList[$_product->getId()] = $_item;
}
return $_itemsInWishList;
}
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; } } ?>
$customer = Mage::getSingleton('customer/session')->getCustomer();
$wishlist = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
$wishListItemCollection = $wishlist->getItemCollection();
foreach ($wishListItemCollection as $item)
{
// do things
}

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