The following code is supposed to output 4 fields into a CSV, Category, SKU, Price and Special Price, and for the most part works well except the output has 3 quote marks around all items like thus
"""Joystick Head""","""Man222""","""155""","""124.95"""
yet the output in the browser is fine
Type: "Joystick Head" SKU: "Man222" Price: "155" Special Price: "124.95"
I have tried str_replace, implode and anything else i can think of, with varying results, sometimes managing to remove almost all quotes.
I am hoping this is something silly and I have been staring at it too long, help?
define('SAVE_FEED_LOCATION','var/export/prices.csv');
set_time_limit(0);
require_once 'app/Mage.php';
Mage::app('default');
try{
$handle = fopen(SAVE_FEED_LOCATION, 'w');
$attributeName = 'producttype';
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1);
$products->addAttributeToFilter('visibility', 4);
$products->addAttributeToSelect('*');
$prodIds = $products->getAllIds();
$product = Mage::getModel('catalog/product');
$counter_test = 0;
foreach($prodIds as $productId) {
if (++$counter_test > 0 && $counter_test < 100){
$product->load($productId);
$product_data = array();
$productId = $product->getId();
$productAtt = Mage::getModel('catalog/product')->load($productId);
$attributes = $productAtt->getAttributes();
$attributeValue = null;
if(array_key_exists($attributeName , $attributes)){
$attributesobj = $attributes["{$attributeName}"];
$attributeValue = $attributesobj->getFrontend()->getValue($productAtt);
}
if ($attributeValue == "No")
{
$attributeValue = NULL;
}
$product_data['producttype'] = $attributeValue;
$product_data['sku'] = $product->getSku();
$product_data['price'] = round($product->getPrice(),2);
$product_data['special_price'] = round($product->getSpecialprice(),2);
// if no special price use normal price
if ($product_data['special_price'] == NULL) {
$product_data['special_price'] = round($product->getPrice(),2);
}
foreach($product_data as $k=>$val){
$bad=array('"',"\r\n","\n","\r","\t");
$good=array(""," "," "," ","");
$product_data[$k] = '"'.str_replace($bad,$good,$val).'"';
}
echo "Type: ". $product_data['producttype'] . " SKU: " . $product_data['sku'] . " Price: ". $product_data['price'] . " Special Price: " . $product_data['special_price'] . "<br>";
fputcsv($handle, $product_data, ',', '"');
}
}
fclose($handle);
echo '<br> Completed at: ' . time();
}
catch(Exception $e){
$headers = 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
error_log($e->getMessage(), 1, 'myemail@mydomain.com', $headers);
die($e->getMessage());
}
The issue is in line
There the code adds double quotes to each value of the array. Later the
fputcsv()escapes quotes by tripling them to properly output data in CVS format. You can take a look at examples in PHP documentation forfputcsv()to see, that this is a normal behavior.There is nothing wrong in 3x double quotes, as it is just a method to preserve double quotes in original data, while passing values to another system via CSV transport. However, if you intentionally strip double quotes, then that line looks like having a typo. It is expected to be:
Additional notes
bad symbol for CSV. I suggest to remove double quote from bad symbols and do not mind triple double quotes at all.
str_replace()can take array as thirdargument. Threfore you can replace all bad values in one call, rather then doing it in
foreachcycle. E.g.:$product_data = str_replace($bad, $good, $product_data);cycle iteration. They are not changed in the script, so their declaration can be
moved right before
foreach ($prodIds as $productId) {line