0

I'm trying to read from a csv file a list of increment_ids and get order using them.

if (($file = fopen('increment_ids.csv','r')) != FALSE) {
    while($data = fgetcsv($file,0,"\t")) {
        $incremenet_id = $data[0];
        $order = Mage::getModel('sales/order')->loadByIncrementId($incremenet_id);
        print_r($order);
        break;
    ...

The issue is that when print_r($order) is empty but when use a number instead of the variable, the variable $order has the informations. I can echo $data[0] without a problem. Thank you.

1
  • Can you please check the answers and give some feedback?
    – sv3n
    Commented Dec 4, 2018 at 17:41

2 Answers 2

0

Can you try below code..

$file = 'increment_ids.csv';
$headers = false;
$handle = fopen($file, "r");
if (empty($handle) === false) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if ($headers) {
            $incremenet_id = $data[0];
            $order = Mage::getModel('sales/order')->loadByIncrementId($incremenet_id);
            print_r($order->getData());
            break;
        }
    }
    fclose($handle);
}
4
  • No it does not work. When i do echo $data[0] : it displays 21452 but loadByIncrementId($data[0]) returns empty $order by using directly the number 21452 gives order information. I have order like 21456-3 in my file and they displayed with out problem upon reading from the file.
    – mockingbid
    Commented May 17, 2018 at 14:36
  • are you sure $data[0] contained the increment id. Can you please do echo $data[0] and confirm. Commented May 17, 2018 at 14:45
  • I'm 100% positive. 12000|| 000013561-1||210.0000 Here a caption, i have result from the second and not from the first althought there are both orders. Increment_id || $order->getGrandTotal();
    – mockingbid
    Commented May 17, 2018 at 14:52
  • then nothing wrong with that, you can try some different way. Commented May 17, 2018 at 15:06
0

This should work too (should be better then loadByIncrementId() in a loop)

$csv = new Varien_File_Csv();
$csv->setDelimiter("\t");
$file = 'increment_ids.csv';
$data = $csv->getData($file);

$columnNumber = 0; # depends on your csv structure
$incrementIds = array_column($data, $columnNumber);

$orders = Mage::getModel('sales/order')->getCollection()
    ->addAttributeToFilter('increment_id', $incrementIds);

foreach ($orders as $order) {
    var_dump($order->getData());
}
2
  • My file is tab-separated. when i echo $incrementsIds it's empty.
    – mockingbid
    Commented May 17, 2018 at 14:41
  • @mockingbid added setDelimiter("\t") to work with tabs. Depending on your CSV you have to adjust the $columnNumber var
    – sv3n
    Commented May 17, 2018 at 14:51

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.