WordPress开发小记录

woocommerce

最近在给公司做一个外贸商城 主要用到的框架是WordPress+woocommerce 由于之前公司已经有了 不过由于php版本太低和界面UI不好等原因 需要重新开发一个,所以在网上找了找还是选择了WordPress。

First blood

第一步就是需要先讲数据导入新的数据库中:

  1. 利用内部函数
  2. csv导入数据

利用内部函数

No.1:将数据导入post表[1]


//$product是一个数组
$user_id = get_current_user_id();// 获取当前使用者的id
$product = array(
    'post_author' => $user_id,
    'post_date' => date('Y-m-d h:i:s'),
    'post_data_gmt' => gmdate('Y-m-d h:i:s'),
    'post_connect' => '',
    'post_connect_filtered' => '',
    'post_title' => '商品标题',
    'post_excerpt' => '',
    'post_type' => 'product',// 如果是导入商品的话 必须是product
    'post_status' => 'publish',
    'post_name' => 商品名称,
    'post_modified' => date('Y-m-d h:i:s'),
    'post_modified_gmt' => gmdate('Y-m-d h:i:s'),
    'guid' => 'http://chutetest.test/?post_type=product&p=''',// 也可以为空
);

$post_id= wp_insert_post($product, $wp_error = true, $fire_after_hooks = true);
//如果成功返回值是post_ id  也就是添加进去的商品的id  失败返回wp_error

/**
 * @link: https://developer.wordpress.org/reference/functions/wp_insert_post/
 */
 add_metadata($meta_type, $post_id, '_' . $v_key, $v_val);
 /**
  * mate_type = post  以为数据是导入wp_postmeta表中
  * post_id  = wp_insert_post生成的post_id
  * $v_key是数据的键  $v_val是数据的值
  */

No.2:添加分类

	//先导入父类分类
	$parent_arr = wp_insert_term(__($parent_cate_val->name), 'product_cat', $parent_cate_data); // return a Array ( [term_id] =>  [term_taxonomy_id] =>  )返回值是一个数组
//__()是一个翻译函数
//wp_insert_term  数据插入terms表
//循环导入子分类  
 foreach ($sub_cate as $sub_cate_val) {
     $sub_cate_data = array(
         'alias_of' => $sub_cate_val->sub_cate,
         'parent' => $parent_arr['term_id']
     );
     $sub_arr = wp_insert_term(__($sub_cate_val->sub_cate), 'product_cat', $sub_cate_data); //return  array
 }

这个用了之后效果还是很好的,不过还有有些不好的就是得敲很多的代码 为了解放双手我的建议是使用第二个方法

利用wp自带的工具

我们可以使用WordPress自带的import功能导入数据

就是这个功能

注意
文件必须是csv文件

当你选择好对应的csv文件之后点击提交 你会看见这样的画面:
请添加图片描述

如果说你的csv文件和这个匹配的话就会完美的匹配好 如果有一些数据不匹配的话 需要自己在重新选择
请添加图片描述就在右边的红色框框中选择 有时候你会发现有些你需要的标签并不在这里边 不过不需要担心 因为 WordPress有函数可以实现你所需要的东西

就在这个这地址

为了方便看我截取了这一部分代码

/**
 * Register the 'Custom Column' column in the importer.
 *
 * @param array $options
 * @return array $options
 */
function add_column_to_importer( $options ) {

	// column slug => column name
	$options['custom_column'] = 'Custom Column';

	return $options;
}
add_filter( 'woocommerce_csv_product_import_mapping_options', 'add_column_to_importer' );

/**
 * Add automatic mapping support for 'Custom Column'. 
 * This will automatically select the correct mapping for columns named 'Custom Column' or 'custom column'.
 *
 * @param array $columns
 * @return array $columns
 */
function add_column_to_mapping_screen( $columns ) {
	
	// potential column name => column slug
	$columns['Custom Column'] = 'custom_column';
	$columns['custom column'] = 'custom_column';

	return $columns;
}
add_filter( 'woocommerce_csv_product_import_mapping_default_columns', 'add_column_to_mapping_screen' );

/**
 * Process the data read from the CSV file.
 * This just saves the value in meta data, but you can do anything you want here with the data.
 *
 * @param WC_Product $object - Product being imported or updated.
 * @param array $data - CSV data read for the product.
 * @return WC_Product $object
 */
function process_import( $object, $data ) {
	
	if ( ! empty( $data['custom_column'] ) ) {
		$object->update_meta_data( 'custom_column', $data['custom_column'] );
	}

	return $object;
}
add_filter( 'woocommerce_product_import_pre_insert_product_object', 'process_import', 10, 2 );

当然 你的csv文件还是有别的其他要求的 如果你的属性好好多的话你会发现 不知道该怎么选择 所以这里就要在你生成csv文件之前处理一下你的数据了
我是这样处理的 (其中也包含了我添加的一些别的列)仅供参考 当然也欢迎大佬指出错误 我好改进

	if($value['format'] == 'Plat' ){
			$value['Epaisseur'] = $value['diameter'];
			$value['Diameter_Largrue_Cote'] = $value['width'];
			$value['Longueur'] = $value['length'];
		}else{
			$value['Epaisseur'] = $value['width'];
			$value['Diameter_Largrue_Cote'] = $value['diameter'];
			$value['Longueur'] = $value['length'];
		}

		$value['Categories'] = $value['parents'].'>'.$value['designation'];
		$value['Tags'] = $value['format'];
		$value['Stock'] = 1;
        $value['Sold individually?'] = 1;
		$value['Attribute 0 name'] = 'Format';
		$value['Attribute 0 value(s)'] = $value['format'];
		$value['Attribute 0 global'] = 1;
		$value['Attribute 1 name'] = 'AFNOR';
		$value['Attribute 1 value(s)'] = $value['AFNOR'];
		$value['Attribute 1 global'] = 1;
		$value['Attribute 2 name'] = 'Euronorme';
		$value['Attribute 2 value(s)'] = $value['Euronorme'];
		$value['Attribute 2 global'] = 1;
		$value['Attribute 3 name'] = 'NF';
		$value['Attribute 3 value(s)'] = $value['NF'];
		$value['Attribute 3 global'] = 1;

这样子你在前面就将数据处理好 可以节省一大部分的时间 我就是在处理数据上没有弄好 返工了好几回才好

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值