0

I am having some issues with a wordpress plugin (Customer Order CSV Export) that for some reason has started to put error warning into the exported file. It is saying there is an illegal string offset for every section of the order (so about 40 warnings for each file). I have only attached the warning for the order_number but the rest all say the same lines:

<b>Warning</b>:  Illegal string offset 'order_number' in <b>/home/.../wp-content/plugins/woocommerce-customer-order-csv-export/includes/class-wc-customer-order-csv-export-generator.php</b> on line <b>709</b><br />
<b>Warning</b>:  Illegal string offset 'order_number' in <b>/home/.../wp-content/plugins/woocommerce-customer-order-csv-export/includes/class-wc-customer-order-csv-export-generator.php</b> on line <b>713</b><br />
<b>Warning</b>:  Illegal string offset 'order_number' in <b>/home/.../wp-content/plugins/woocommerce-customer-order-csv-export/includes/class-wc-customer-order-csv-export-generator.php</b> on line <b>713</b><br />

The offending lines in the php file look like this:

private function write( $row ) {

    $data = array();

    foreach ( $this->headers as $header_key => $_ ) {

        if ( ! isset( $row[ $header_key ] ) ) {
            $row[ $header_key ] = '';
        }

        // strict string comparison, as values like '0' are valid
        $data[] = ( '' !== $row[ $header_key ] ) ? $row[ $header_key ] : '';
    }

    fputcsv( $this->stream, $data, $this->delimiter, $this->enclosure );
}

The error lines are 709:

$row[ $header_key ] = '';

and 713:

$data[] = ( '' !== $row[ $header_key ] ) ? $row[ $header_key ] : '';

Looking around at some similar questions, it looks like it is to do with isset: as in this answer and in this one. I'm not sure how to fix the code I have to make this work, or if that is indeed the actual problem and not something else.

The exported csv file contains all the warning messages each on a new line after each order.

Any help appreciated.

2
  • In your foreach loop define proper key and value and just replace this line $data[]=$row[$header_key]; on $data[] = ( '' !== $row[ $header_key ] ) ? $row[ $header_key ] : ''; And let me know it works or not ? Commented Feb 17, 2015 at 5:24
  • Thanks. I tried doing as you said but that is still producing the same result. Could be an issue coming from somewhere else in the file? Commented Feb 18, 2015 at 23:05

1 Answer 1

0

The function seems to be a bit of an overkill for me, so I'll rewrite it in a simpler to understand way.

private function write( $row ) {

$data = array();

foreach ( $this->headers as $header_key => $header_value ) {

    if ( !isset($row[$header_key]) ){
        $row[$header_key] = '';
    }
    $data[]=$row[$header_key];
}

fputcsv( $this->stream, $data, $this->delimiter, $this->enclosure );
}

And I crunched it down because all the code really does is assumes theres a stream, delimiter, enclosure variables and headers array all stored elsewhere within the class. if the headers array doesn't consist of alphanumeric indexes then you'll get an error.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I have tried replacing the function with the one you wrote but it is still giving the same error. Maybe I have the header keys set up wrong elsewhere in the file? Though looking through it, it does not appear that way.
Let's debug. Right under private function write( $row ) {, add echo "<!-- debug start-->\n";print_r($row);echo "\n <!-- debug end-->\n" on its own separate line then run the script. If you use a browser, you might see strange output, but thats ok. Just go to the view source code option. Somewhere in it you'll find the words "debug start". Between there and "debug end", you'll find the data that's being evaluated and if you don't see "order_number" between "debug start" and "debug end" then you'll see the errors you mentioned.
Arrrghhh!! Thanks Mike for your help. It did help me get to the bottom of the problem. Which was so simple when you find it (as these things always are). There was a filter in my functions.php file that was trying to overwrite an earlier function in the file. I removed this filter and it is all working as it should. Cheers again for your time and expertise ;)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.