0

hope you're having a wonderful day.

Currently developing a wordpress hook for my agency, in the 'functions.php' file I have a method that takes the list of items and generates a template CSV file for them to enter in relevant information to their package.

It's not hard coded in the sense of generation, it takes the list of cart items and generates the CSV accordingly.

However on deployment it gives me a 500 error, which I'm presuming is caused by a syntax error; my phplint is also not working on sublime so cannot actually figure out what's going on.

*Note I have been programming in php for all of 3 days now, so any constructive pointers would be much appreciated, apologies if my code looks like an eyesore.

Already tried going through every line I've changed, however I've not worked with CSV files before, the server error could be generated by the CSV file? I'm not entirely sure.

add_action('woocommerce_after_order_notes', 'customise_checkout_field');

function customise_checkout_field($checkout)
{
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();

    $total_price = preg_replace("/[^0-9]./", "", $woocommerce->cart->get_cart_total());


    if($total_price >= 1500)
    {

        echo '<br> <h3>Have us do your anchors for you! (Bonus when having an order bigger than $1,500)</h3> 
        <div id="custom-anchors"> ';

        woocommerce_form_field( 'anchor_checkbox', array(
        'type'          => 'checkbox',
        'required'  => false,
        ), $checkout->get_value('anchor_checkbox'));

        echo ' </div>';
        echo '</br>';

    }


    generate_csv_file($checkout);

}

function generate_csv_file($checkout)
{
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();

    $fileName = 'anchors.csv';

    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="' . $fileName . '"');

    $arrayData[0] = array('', 'Target URL', 'Anchor', 'Notes'); //headers.

    $_product =  wc_get_product($values['data']->get_id());
    $quantity = $values['quantity'];
    $product_name = $_product->get_title();
    $product = preg_replace('/\s+/', '', $product_name);

    $product_ = wc_get_product($values['product_id']);

    $description = $product_->get_description();

    $full_item_content = $product . $description; //full product info to scan.


    if(!is_null($items))
    {
        $iteration_count = 1; //used as index within array data.
        foreach($item as $items => $values)
        {
            for($i = 0 ; $i < $quantity ; $i++)
            {

                $link_quantity = 0;

                if(!stripos($product_name, 'package') > -1)
                {
                    if(stripos($full_item_content, '1x') > -1)
                        {
                            $link_quantity = 1;
                        }
                        else if(stripos($full_item_content, '3x') > -1)
                        {
                            $link_quantity = 3;
                        }
                        else if(stripos($full_item_content, '5x') > -1)
                        {
                            $link_quantity = 5;
                        }
                        else if(stripos($full_item_content, '10x') > -1)
                        {
                            $link_quantity = 10;
                        }
                        else if(stripos( $full_item_content, 'DR') > -1)
                        {
                            $link_quantity = 1;
                        }
                        else if(stripos($full_item_content, 'PBN') > -1)
                        {
                            $link_quantity = 1;
                        }
                        else if(stripos($full_item_content, 'RD') > -1)//foreign links
                        {
                            $link_quantity = 1;
                        }


                    for($k = 0 ; $k < $link_quantity ; $k++)
                    {
                        $arrayData[$iteration_count] = ($product_name, ' ', ' ', ' ');
                        $iteration_count++;//increment at the end of loop.
                    }


                    $iteration_count++; //Add space inbetween new product.

                }
                else //is package.
                {

                }


            $iteration_count++; //increment the value

            }

        }
    }

    //Now gen it.

    $fp = fopen('php://output', 'wb');

    foreach($arrayData as $row)
    {
        fputcsv($fp, $row, ',');
    }

    fclose($fp);

}

Expected it to run and download a template file for the user, however that doesn't seem to be the case, all help is appreciated! Thank you in advance!

Mat
  • 11
  • 3
  • So go check the error log then, that is _always_ the first thing you do when you get a 500. – misorude Jul 26 '19 at 10:08
  • *noob question incoming* Apologies if this sounds like a dumb question, I'm new to php, where would I find the error log? Thank you for the help :) – Mat Jul 26 '19 at 10:17
  • completely agree with @misorude, but reading carefully the code, probably you missed a ; in the last else body. To find the log check the output of phpinfo – stefano Jul 26 '19 at 10:24
  • @misorude I've checked the log and there's nothing there, should it fire an exception directing me to the line the given error is on if there is one? – Mat Jul 26 '19 at 10:29
  • Simple parse errors don’t cause exceptions. You should be seeing a line saying something like `Parse error: syntax error, unexpected ',' in […] on line 102`. If you don’t see that, then you are either checking the wrong file, of have your PHP error reporting configured to suppress certain types of errors. ` – misorude Jul 26 '19 at 10:32
  • maybe that the log has to be enable, have a look [here:](https://stackoverflow.com/questions/5127838/where-does-php-store-the-error-log-php5-apache-fastcgi-cpanel). – stefano Jul 26 '19 at 10:32
  • `$arrayData[$iteration_count] = ($product_name, ' ', ' ', ' ');` is not valid PHP syntax. Was that maybe supposed to use _square_ brackets instead …? – misorude Jul 26 '19 at 10:34
  • In the line cited by misorude I guess you try to add an element to an array, but this is not the syntax php admits. Have a look at [this article](https://appdividend.com/2019/03/29/php-array-push-example-add-elements-to-an-array/) – stefano Jul 26 '19 at 10:45

0 Answers0