Get customer ID using Mage::getModel('customer/customer') - magento

I not able to get the customersID am I calling it correctly with Mage::getModel
$customer = Mage::getModel('customer/customer')
->setWebsiteId(2)
->load($this->getRequest()->getParam('customer'));
$customer_id = $customer->getId();
$sku = 'BOX3151';
$sql = "SELECT sfoi.item_id FROM sales_flat_order_item sfoi INNER JOIN sales_flat_order sfo
ON sfo.entity_id = sfoi.order_id WHERE sfo.customer_id = '$customer_id' AND sfoi.sku = '$sku'";
$read_db = Mage::getSingleton('core/resource')->getConnection('core_read');
$result = $read_db->fetchAll($sql);
$total_rows = count($result);
if($total_rows >= 1):
echo ' Found ';
else:
echo ' Empty ';
endif;
endif;

Please check for available data in
$this->getRequest()->getParam('customer')
if you have $customerId there you can use the load() method.
$customer = Mage::getModel('customer/customer')
->setWebsiteId(2)
->load($customerId)
also you can get customer object by $email.
$customer = Mage::getModel('customer/customer')
->setWebsiteId(2)
->loadByEmail($email);

<?php
$email = "customer#gmail.com";
$customer = Mage::getModel("customer/customer")->setWebsiteId(Mage::app()->getWebsite()->getId())->loadByEmail($email);
//use
echo $customer->getId();
echo $customer->getEmail();
echo $customer->getFirstname();
?>

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

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

How to get invoice id from order increment id

I have order increment id like 100000096 and i want to get its invoice id, please help me to achieve this in magento, i have tried with
$incrementId = '100000096';
$order = Mage::getModel('sales/order')->load($incrementId);
$invIncrementIDs = array();
foreach ($order->getInvoiceCollection() as $inv) {
$invIncrementIDs[] = $inv->getIncrementId();
}
But it returns an empty array.
You are using order increment id in load() function, you have to use loadByIncrementId().
Try to use this -
$orderid = '100000096'; // order increment id
$order = Mage::getModel('sales/order')->loadByIncrementId($orderid);
if ($order->hasInvoices()) {
$invIncrementIDs = array();
foreach ($order->getInvoiceCollection() as $inv) {
$invIncrementIDs[] = $inv->getIncrementId();
}
}
echo "<pre>";
print_r($invIncrementIDs);
echo "</pre>";
Try this :
$orderIncrementId = '100000004'; // your order increment id
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
if ($order->hasInvoices()) {
$invIncrementId = array();
foreach ($order->getInvoiceCollection() as $invoice) {
$invoiceIncId[] = $invoice->getIncrementId();
}
}
echo "<pre>";print_r($invoiceIncId);echo '<br>';
In order to get Invoice id from Invoice incrementId
$incrementId = $invoiceIncId[0];
$invoive = Mage::getModel('sales/order_invoice')->loadByIncrementId($incrementId);
$invoiceId = $invoice->getId();
echo "<pre>";print_r($invoiceId);exit;
try this you will not regret
$order_id = "type_here_your_orderid";
$invoices = Mage::getResourceModel('sales/order_invoice_collection')
->setOrderFilter($order_id)
->load();
$invoice_id = $invoices->getData()[0]["increment_id"];
echo $invoice_id;
thats all.

magento get product using proudct id

I am trying to get feature product of magento, I am getting all product ids properly in each variables, but its not working inside the loop and shows repeated product.
here is my code
$storeId = Mage::app()->getStore()->getId();
$categoryId = $this->getRequest()->getParam('id', false);
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('catalog_read');
$categoryProductTable = $resource->getTableName('catalog/category_product');
//$productEntityIntTable = $resource->getTableName('catalog/product_entity_int'); // doesn't work :(
$productEntityIntTable = (string)Mage::getConfig()->getTablePrefix() . 'catalog_product_entity_int';
$eavAttributeTable = $resource->getTableName('eav/attribute');
$select = $read->select()
->from(array('cp'=>$categoryProductTable))
->join(array('pei'=>$productEntityIntTable), 'pei.entity_id=cp.product_id', array())
->joinNatural(array('ea'=>$eavAttributeTable))
->where('pei.value=1')
->where('ea.attribute_code="featured"');
$_product = $read->fetchAll($select);
$total_product= count($_product);
when i print_r($_product) it shows multiple id of product which are features so its correct
but inside for loop when i try to get product name using id it shows repeated Product names,here is the code
$obj = Mage::getModel('catalog/product');
for($cp=0;$cp <= $total_product; $cp++):
$_product= $obj->load($_product[$cp]['product_id']);
echo $_product->getName();
endfor;
Try this
for($cp=0;$cp <= $total_product; $cp++):
$obj = Mage::getModel('catalog/product');
$_productnew= $obj->load($_product[$cp]['product_id']);
echo $_productnew->getName();
endfor;
Put $obj inside for loop like below,
for($cp=0;$cp <= $total_product; $cp++):
$obj = Mage::getModel('catalog/product');
$_product= $obj->load($_product[$cp]['product_id']);
echo $_product->getName();
endfor;

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