Magento Customer-Collection and Product-Collection?

0 votes
288 views
asked Jul 11, 2016 in Magento by Russel Andrew
How to get Magento Customer-Collection and Product-Collection?

1 Answer

0 votes
answered Jul 11, 2016 by Mithun K R

Use this code for Customer Collection

<?php
// Get customer model, run a query
$collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');
$result = array();

// Get customer details. getName, getEmail so on.
foreach ($collection as $customer) {
    $result[] = array($customer->getEmail());
}

// Used if you need email as comma separated file. (Use the customer details as you wish, code beneath is just an example. You could just use the built in exporter.)
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=email_' . date("d.m.Y-H:i:s") . '.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

foreach($result as $fields) {
    // output the column headings
    fputcsv($output, $fields);
}

?>

And for Product Collection

<?php
# Product collection
$products = Mage::getResourceModel('catalog/product_collection');

# Select all (*) filter by visibility (not equal to 1 (1 == Not visible individually, 2 == Catalog, 3 == Search)) and where status = 1 (enabled)
$products->addAttributeToSelect('*')->addAttributeToFilter('visibility', array('neq' => 1))->addAttributeToFilter('status', 1);

# Filter by attributes. Ex: 'like' => %param% / param% or like could be eq (equal) neq (not equal) so on.
$products->addAttributeToFilter(
    array(
        array('attribute' => 'custom_attr', 'like' => $param . '%')
    )
);

# Could set page size (how many products to return). Nice for pagination
$products->setPageSize(10);

# You could also sort desc/asc so on by using this method:
$products->addAttributeToSort('created_at', 'desc');

# Or
$products->setOrder('entity_id','desc');

# Loop through result:
foreach($products as $_product) {
    printf("%s <br />\r\n", $_product->getName());
}

?>

Related questions

+2 votes
1 answer 248 views
0 votes
1 answer 332 views
+2 votes
1 answer 176 views
asked Jun 13, 2016 in Magento by Silver James
0 votes
2 answers 314 views
0 votes
0 answers 293 views
+1 vote
1 answer 377 views
...