Created
June 13, 2014 19:31
-
-
Save devinsays/cda1fad0d81f659ae76b to your computer and use it in GitHub Desktop.
CSV Purchase Import for EDD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Easy Digital Downloads - CSV Purchase Import | |
Plugin URL: http://easydigitaldownloads.com/extension/csv-purchase-import | |
Description: Allows you to import purchase history CSVs from other ecommerce systems | |
Version: 0.1 | |
Author: Pippin Williamson | |
Author URI: http://pippinsplugins.com | |
Contributors: mordauk | |
*/ | |
if ( !defined('EDDCSVPI_PLUGIN_DIR') ) { | |
define('EDDCSVPI_PLUGIN_DIR', dirname(__FILE__)); | |
} | |
ini_set('max_execution_time', 90); | |
ini_set("auto_detect_line_endings", true); | |
function eddcsvpi_menu_page() { | |
global $edd_csv_import; | |
$edd_csv_import = add_submenu_page( | |
'edit.php?post_type=download', | |
__('CSV Import', 'edd'), | |
__('CSV Import', 'edd'), | |
'manage_options', | |
'edd-csv-import', | |
'edd_csv_purchase_import' | |
); | |
} | |
add_action('admin_menu', 'eddcsvpi_menu_page'); | |
function edd_csv_purchase_import() { | |
?> | |
<div class="wrap"> | |
<h2><?php _e('CSV Purchase History Import', 'edd'); ?></h2> | |
<P><?php _e('Use this tool to import previous purchase history from other ecommerce systems into Easy Digital Downloads.', 'edd'); ?></p> | |
<form id="edd_csv_import" enctype="multipart/form-data" method="post"> | |
<input type="file" name="edd_csv_file"/> | |
<input type="hidden" name="edd_action" value="process_csv_import"/> | |
<?php wp_nonce_field('edd_csv_nonce', 'edd_csv_nonce'); ?> | |
<?php submit_button( 'Process Purchase History Import' ); ?> | |
</form> | |
</div> | |
<?php | |
} | |
function edd_process_csv( $data ) { | |
if ( isset( $data['edd_csv_nonce'] ) && wp_verify_nonce( $data['edd_csv_nonce'], 'edd_csv_nonce' ) ) { | |
//$_FILES['edd_csv_file']['tmp_name']; | |
$csv = $_FILES['edd_csv_file']['tmp_name']; | |
$csv_array = edd_csv_to_array( $csv, ','); | |
foreach( $csv_array as $purchase ) { | |
if !isset( $purchase['Total'] ) { | |
$purchase['Total'] = '0.00'; | |
} | |
if !isset( $purchase['Coupon'] ) { | |
$purchase['Coupon'] = false; | |
} | |
$user = get_user_by( 'email', $purchase['Email'] ); | |
// All purchases are from current users | |
// So I am not making a new account for each | |
if ( ! $user ) { | |
// Skip | |
} else { | |
$user_id = $user->ID; | |
$email = $user->user_email; | |
if ( $user->user_firstname ) { | |
$purchase['Billing First Name'] = $user->user_firstname; | |
} else { | |
$purchase['Billing First Name'] = ''; | |
} | |
if ( $user->user_lastname ) { | |
$purchase['Billing Last Name'] = $user->user_lastname; | |
} else { | |
$purchase['Billing Last Name'] = ''; | |
} | |
} | |
$download = get_page_by_title( $purchase['Product'], OBJECT, 'download' ); | |
if ( $download ) { | |
// get products here | |
$products = array( | |
array( | |
'id' => $download->ID, | |
'options' => array() | |
) | |
); | |
// get products here | |
$cart_details = array( | |
array( | |
'id' => $download->ID, | |
'price' => $purchase['Total'] | |
) | |
); | |
$payment_data = array( | |
'price' => $purchase['Total'], | |
'post_date' => date( 'Y-m-d H:i:s', strtotime( $purchase['Date'] ) ), | |
'user_email' => $email, | |
'purchase_key' => strtolower( md5( uniqid() ) ), // random key | |
'currency' => 'USD', | |
'downloads' => $products, | |
'cart_details' => $cart_details, | |
'user_info' => array( | |
'id' => $user_id, | |
'email' => $email, | |
'first_name'=> $purchase['Billing First Name'], | |
'last_name' => $purchase['Billing Last Name'], | |
'discount' => $purchase['Coupon'] | |
), | |
'user_id' => $user_id, | |
'status' => 'pending', | |
'post_data' => array(), | |
'gateway' => 'csv_import', | |
); | |
$payment_id = edd_insert_payment( $payment_data ); | |
remove_action( 'edd_update_payment_status', 'edd_trigger_purchase_receipt', 10 ); | |
remove_action( 'edd_complete_purchase', 'edd_trigger_purchase_receipt', 999 ); | |
edd_update_payment_status( $payment_id, 'publish' ); | |
} else { | |
// no product with the given name was found | |
} | |
} | |
} | |
} | |
add_action('edd_process_csv_import', 'edd_process_csv'); | |
function edd_csv_to_array( $filename = '', $delimiter = ',' ) { | |
if( !file_exists( $filename ) || !is_readable( $filename ) ) | |
return FALSE; | |
$header = NULL; | |
$data = array(); | |
if ( ( $handle = fopen( $filename, 'r' ) ) !== FALSE ) { | |
while ( ( $row = fgetcsv( $handle, 1000, $delimiter ) ) !== FALSE ) { | |
if ( ! $header ) { | |
$header = $row; | |
} | |
else { | |
$data[] = array_combine( $header, $row ); | |
} | |
} | |
fclose( $handle ); | |
} | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment