我正在努力将word中的数据从csv提取到数据库.Below是我的函数。问题是它没有正确获取特殊字符(例如Â)。如何修改它以使用特殊字符进行获取。 / p>
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 ()
答案 0 :(得分:0)
尝试使用函数format_product_name
和utf8_encode / decode,即:
function format_product_name( $title ){
$title = str_replace( "-", "", $title );
$title = utf8_encode( $title );
//you can also try $title = utf8_decode( $title );
return $title;
};
然后用它来格式化帖子内容:
...
'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'] ),
...
注意:
检查您的数据库字段编码设置以确保它 utf ,如果没有,您必须对csv内容进行编码以匹配它为此,使用iconv或类似的php函数。