3

I am trying to create a csv, using script that contains the below product attributes in magento 1.

id, title, description, link, image_link, price, availability

Has anyone implemented it? Any thoughts how it can be done?

1 Answer 1

2

customize the code according to your requirement i have written it for one my work where i needed to export products from magento 1. i this code am just taking the sku, id , and price you can take other things as well if you want.

    <?php
// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=report.csv'); //you can change the file name here if you want
// Load the Magento core
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);

// Load the product collection

$collection = Mage::getModel('catalog/product')
 ->getCollection()
 ->addAttributeToSelect('*') //Select everything from the table
 ->addUrlRewrite(); //Generate nice URLs

/*
 For this example I am generating a CSV file,
 but you can change this to suit your needs.
*/
 // create a file pointer connected to the output stream
$output = fopen('php://output', 'w');
// output the column headings
fputcsv($output, array('title', 'sku', 'id', 'price'));

foreach($collection as $product) {
 //Load the product categories
 $categories = $product->getCategoryIds();
 //Select the last category in the list
 $categoryId = end($categories);
 
 //Load that category
 $category = Mage::getModel('catalog/category')->load($categoryId);
 // Collect details in variables
 $title=$product->getName();
 $sku=$product->getSku();
 $id=$product->getId();
 $sku=$product->getPrice();
 $url=str_replace('export.php/','',Mage::getBaseUrl()).$product->getPrice($product);
 fputcsv($output, array($title, $sku, $id, $price));
}
?>

put the script in magento root directtory and then run it in browser like below :- http://127.0.0.1:96/mage/export_products.php

23
  • Thanks for the answer to the question, let me check if the above code works. Jul 28, 2020 at 5:17
  • @HamendraSunthwal did the code worked for you?
    – Pramod
    Jul 28, 2020 at 5:25
  • I am checking on it. i will update if it works. Jul 28, 2020 at 5:30
  • $userModel = Mage::getModel('admin/user'); $userModel->setUserId(0); Why used this one ? Jul 28, 2020 at 5:33
  • this for loading magento core model
    – Pramod
    Jul 28, 2020 at 5:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.