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!