I am working on fetching data from csv to database in wordpress .Below are my functions.The problem is it is not fetching special characters (e.g. Ā) properly.How can I modify this to make to fetch with special charecters.
public function process_feed(){
// title, image URL, retail price, sale price, product link, SKU, group ID (new), inventory
// CSV Fields
// [id] => 00412504024374
// [title] => D.L. & Co Votive Candle Gift Set-Multi
// [description] => A collection of six 1 oz. votive candles in three scents: two Thorn Apple, two Angels Trumpet and two Lady Rhubarb candles. Each housed in signature hand-blown frosted glass vases. Delightfully packaged in signature silk hat box.
// [google_product_category] => Home & Garden > Decor > Candles
// [product_type] => Home > Decor & Accessories > Home Fragrance > Candles
// [link] => http://www.barneyswarehouse.com/D.L.-&-Co-Votive-Candle-Gift-Set-125046857.html
// [image_link] => http://s7d9.scene7.com/is/image/Barneys/125046857
// [additional_image_link] =>
// [condition] => new
// [availability] => in stock
// [price] => 125.00 USD
// [sale_price] => 45.00 USD
// [sale_price_effective_date] => 2014-11-17T14:39:06/2015-11-17T14:39:06
// [brand] => D.L. & Co
// [gtin] =>
// [mpn] => 00412504024374
// [item_group_id] => 125046857
// [color] => Multi
// [material] =>
// [pattern] =>
// [size] => 1 Size
// [gender] => Unisex
// [age_group] => Adult
// [adwords_labels] =>
// [custom_label_0] =>
// [custom_label_1] =>
// [custom_label_2] =>
// [custom_label_3] =>
// [custom_label_4] =>
$file_paths = array(
get_option( 'sjr_product_import_file_path_1' ),
get_option( 'sjr_product_import_file_path_2' )
);
if( !empty($file_paths) ){
// Capture all product IDs in the feeds to compare against existing products in DB
// to deactivate products that are no longer in the feeds.
$current_products = array();
foreach( $file_paths as $file_path ){
$csv = new parseCSV();
$csv->delimiter = "\t";
$csv->enclosure = "";
$csv->sort_by = "item_group_id";
$csv->parse( $file_path );
if( $csv->error === 0 ){
$current_group = null;
$i = $i_all = 1;
foreach( $csv->data as $product_row ){
if( $current_group === $product_row['item_group_id'] ){
$i_all++;
continue;
}
// Only import one of the item group products
$current_group = $product_row['item_group_id'];
$i++;
$current_products[] = $product_row['id'];
$args = array(
'meta_query' => array(
array(
'key' => $this->meta_field_prefix . 'product_id',
'value' => $product_row['id']
)
),
'post_type' => 'imported_product',
'posts_per_page' => 1
);
$existing_post = new \WP_Query($args);
$post_data = array(
'post_title' => $product_row['item_group_id'] . ' - ' . format_product_name( $product_row['title'] ),
'post_content' => $product_row['description'],
'post_excerpt' => $product_row['description'],
'post_status' => 'publish',
'post_type' => 'imported_product',
'post_author' => 1,
'ping_status' => 'closed',
'comment_status' => 'closed',
);
// No need for those items
$product_row['product_id'] = $product_row['id'];
unset($product_row['id'], $product_row['description']);
if( !empty($existing_post->posts[0]) ){
$post_id = $existing_post->posts[0]->ID;
$post_data['ID'] = $post_id;
wp_update_post($post_data);
} else {
$post_id = wp_insert_post($post_data);
}
// The rest are meta fields, add a prefix
if (isset($post_id) ){
$meta_fields = array(
'item_group_id' => $product_row['item_group_id'],
'product_name' => str_replace(strrchr($product_row['title'], '-'), '', $product_row['title']),
'product_id' => $product_row['product_id'],
'is_inactive' => 0,
'additional_information' => array(
'image_link' => $product_row['image_link'],
'price' => $product_row['price'],
'sale_price' => $product_row['sale_price'],
'sale_price_effective_date' => $product_row['sale_price_effective_date'],
'link' => $product_row['link'],
'brand' => $product_row['brand']
),
);
foreach( $meta_fields as $key => $value ){
if (is_array($value) ){
$value = serialize($value);
}
update_post_meta($post_id, $this->meta_field_prefix . $key, $value);
}
}
} // end foreach
} else { // else if( $csv->error === 0)
} // end if( $csv->error === 0)
} // end foreach( $file_paths as $file_path)
}
}
function format_product_name( $title ){
return str_replace( strrchr($title, '-'), '', $title );
};
DB :
function install(){
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$db_table = $wpdb->prefix . 'products';
$sql = "CREATE TABLE $db_table (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`row_id` varchar(22) DEFAULT NULL,
`title` varchar(255) DEFAULT '',
`description` text,
`google_product_category` text,
`product_type` text,
`link` text,
`image_link` text,
`additional_image_link` text,
`condition` text,
`availability` text,
`price` text,
`sale_price` text,
`sale_price_effective_date` text,
`brand` text,
`gtin` text,
`mpn` varchar(22) DEFAULT NULL,
`item_group_id` int(22) DEFAULT NULL,
`color` text,
`material` text,
`pattern` text,
`size` text,
`gender` text,
`age_group` text,
`adwords_labels` text,
`custom_label_0` text,
`custom_label_1` text,
`custom_label_2` text,
`custom_label_3` text,
`custom_label_4` text,
`feed_file` varchar(255) DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `row_id` (`row_id`),
KEY `title` (`title`),
KEY `item_group_id` (`item_group_id`)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
} // End install ()
Try using your function format_product_name
with utf8_encode/decode, i.e.:
function format_product_name( $title ){
$title = str_replace( "-", "", $title );
$title = utf8_encode( $title );
//you can also try $title = utf8_decode( $title );
return $title;
};
Then use it to format the post content:
...
'post_title' => $product_row['item_group_id'] . ' - ' . format_product_name( $product_row['title'] ),
'post_content' => format_product_name( $product_row['description'] ),
'post_excerpt' => format_product_name( $product_row['description'] ),
...
Note:
Check your database field encoding setting to make sure it's utf, if not, you'll have to encode the csv content to match it, for that, use iconv or similar php function.