Magento - Get customer gender from order query - magento

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

Related

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

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

Laravel Query Builder fetch multiple table

$name = $_POST['name'];
$statement = $db->prepare("SELECT * FROM authors WHERE name = ?");
$statement->execute(array($name));
$author_id = $statement->fetch()["id"];
$statement = $db->prepare("SELECT * FROM quotes WHERE author_id = ?");
$statement->execute(array($author_id));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$quote = $row['quote'];
}
How can i do same operation in laravel query builder ?
You can try like this:
$name = $_POST['name'];
$where[] = ['authors.name ', $name];
$data = \DB::table('authors')
->join('quotes', 'quotes.author_id', '=', 'authors.id')
->where($where)
->get();

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

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.

What am I missing in my foreach and/or echo

I am trying to cycle through the results of my query to display in table format on my page.
What am I missing in my foreach and my echo to cycle through the results in my display table.
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "
SELECT image_url
FROM #__image_data
WHERE user_name = '$name'";
$db->setQuery($query);
$list = $db->loadObjectList();
foreach ($list as $item){
$item->image_url;
}
?>
<td><?php echo $item->image_url;?></td><br/>
<td><?php echo $item->image_url;?></td><br/>
<td><?php echo $item->image_url;?></td><br/>
{/source}
{source 0}
<?php
$user =& JFactory::getUser();
if (!$user->guest)
$name = $user->username;
$db =& JFactory::getDbo();
$query = $db->getQuery(true);
$query = "
SELECT image_url
FROM #__image_data
WHERE user_name = '$name'";
$db->setQuery($query);
$list = $db->loadObjectList();
foreach ($list as $item){
echo "<td>".$item->image_url."</td><br/>";
}
?>
{/source}
You can easily loops through and get the <td> as below.
Method 1 - Here you don't have to concatenate since you are using ""
$list = $db->loadObjectList();
foreach ($list as $item){
echo "<td> $item->image_url </td><br/>";
}
Method 2
$list = $db->loadObjectList();
foreach ($list as $item){
echo '<td>'.$item->image_url'.</td><br/>';
}

Resources