Supporting CSV Loader

CSV Loader plugin, as any other TheCartPress plugin, allows to be extended, using actions and filters.

In this case, a plugin developer can add more fields to be updated using CSV Loader. Those are the hooks that CSV Loader offers to plugin developers:

  1. tcp_csvl_option_columns (filter), allows to add more fields to be assigned
  2. tcp_csv_loader_row (action), allows to process added fields
  3. tcp_csv_loader_new_post (filter), allows to add more values to the new product (or any other post type)

The best way will be to see an example:

<?php
/*
Plugin Name: MyPlugin for TheCartPress
Plugin URI: http://thecartpress.com/
Description: MyPlugin for TheCartPress
Version: 1.0
Author: TheCartPress team
Author URI: http://thecartpress.com
License: GPL
Parent: thecartpress
*/

/**
 * This file is part of TheCartPress-MyPlugin.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

if ( ! class_exists( 'MyPluginClass' ) ) {
class MyPluginClass {

	function __construct() {
		add_action( 'admin_init', array( $this, 'admin_init' ) );
	}

	function admin_init() {
		add_filter( 'tcp_csvl_option_columns', array( &$this, 'tcp_csvl_option_columns' ), 10, 2 );
		add_filter( 'tcp_csv_loader_new_post', array( &$this, 'tcp_csv_loader_new_post' ), 10, 2 );
		add_action( 'tcp_csv_loader_row', array( &$this, 'tcp_csv_loader_row' ), 10, 2 );
	}

	function tcp_csvl_option_columns( $options, $col ) {
		//Adding a new option, With those options CSVLoader will load the drop down lists in the Load Admin panel
		$options[] = array( 'myplugn_option_id', strtoupper( $col ) == 'MY_PLUGIN_OPTION_TITLE',  'MyPlugin: My Option' );
		$options[] = array( 'myplugn_option_id_2', strtoupper( $col ) == 'MY_PLUGIN_OPTION_2_TITLE',  'MyPlugin: My Option 2' );
		return $options;
	}

	function tcp_csv_loader_new_post( $post, $cols ) {
		//Nothing to do if the post is created. Do you want to add any value to the default products value?
		return $post;
	}

	function tcp_csv_loader_row( $post_id, $cols ) {
		//Checks for values
		//foreach defined column, the functions, checks if it's present in the request data (columns are not ordered)
		foreach( $cols as $i => $col ) {
			//Gets the requested columns and tries to join them with the defined columns
			$col_names = isset( $_REQUEST['col_' . $i] ) ? $_REQUEST['col_' . $i] : array();
			if ( is_array( $col_names ) && count( $col_names ) > 0 ) {
				foreach( $col_names as $col_name ) {
					if ( 'myplugin_option_id' == $col_name ) {
						$myplugin_option_id = $col;
					} elseif ( 'myplugin_option_id_2' == $col_name ) {
						$myplugin_option_id_2 = $col;
					}
				}
			}
		}
		//for each value, set the value to the product
		if ( isset( $myplugin_option_id ) ) {
			update_post_meta( $post_id, 'myplugin_option_id', $myplugin_option_id );
		}
		if ( isset( $myplugin_option_id_2 ) ) {
			update_post_meta( $post_id, 'myplugin_option_id_2', $myplugin_option_id_2 );
		}
	}
}
new MyPluginClass();
} // class_exists check

And that’s all, enjoy it.