0 votes
1.4k views
in Magento by
How to get Magento Customer-Collection and Product-Collection?

1 Answer

0 votes
by

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 1.3k views
0 votes
1 answer 1.2k views
+2 votes
1 answer 1.1k views
asked Jun 13, 2016 in Magento by Silver James
0 votes
2 answers 1.5k views
0 votes
0 answers 1.1k views
+1 vote
1 answer 1.7k views
...