Created
March 23, 2017 00:00
-
-
Save SubCon/85d2a5799fa5ea019bef71c55a721a6a to your computer and use it in GitHub Desktop.
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: PID Import | |
*/ | |
defined('ABSPATH') or die('No script kiddies please!'); | |
add_filter( 'woo_pd_options_addons', 'alo_import_product_fields'); | |
function alo_import_product_fields( $fields ) { | |
$woo_ped_addon_fields = array(); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'newprod_date', | |
'label' => 'New Product Date', | |
'acf' => true, | |
'group_open' => '<optgroup label="Extra Fields">' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'prod_type_abnab', | |
'label' => 'Product Type ab/nab', | |
'acf' => true, | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'wc_productdata_options', | |
'label' => 'AB/NAB Fields' | |
); | |
//Lots | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_catnum', | |
'label' => 'Lot Cat#', | |
'acf' => false, | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_number', | |
'label' => 'Lot #', | |
'acf' => false, | |
'group_close' => '</optgroup>' | |
); | |
$fields = array_merge($fields, $woo_ped_addon_fields); | |
return $fields; | |
} | |
add_filter( 'woo_pd_import_addons', 'alo_pd_extend_import_addons', 10, 2 ); | |
function alo_pd_extend_import_addons( $import, $csv_data ) { | |
$woo_ped_addon_fields = array(); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'newprod_date', | |
'label' => 'New Product Date', | |
'group_open' => '<optgroup label="Extra Fields">' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'prod_type_abnab', | |
'label' => 'Product Type ab/nab' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'wc_productdata_options', | |
'label' => 'AB/NAB Fields' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_catnum', | |
'label' => 'Lot Cat#' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_number', | |
'label' => 'Lot #', | |
'group_close' => '</optgroup>' | |
); | |
foreach($woo_ped_addon_fields as $fld ){ | |
if( isset( $csv_data[$fld['name']] ) ) { | |
$import->headers[] = $fld['name']; | |
$import->{'csv_'.$fld['name']} = array_filter( $csv_data[$fld['name']] ); | |
$import->log .= "<br />>>> " . __( $fld['label'] . ' has been detected and grouped', 'woocommerce-product-importer' ); | |
} | |
} | |
return $import; | |
} | |
add_filter( 'woo_pd_product_addons', 'alo_pd_extend_product_addons', 10, 3 ); | |
function alo_pd_extend_product_addons( $product, $import, $count ) { | |
$woo_ped_addon_fields = array(); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'newprod_date', | |
'label' => 'New Product Date', | |
'group_open' => '<optgroup label="Extra Fields">' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'prod_type_abnab', | |
'label' => 'Product Type ab/nab' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'wc_productdata_options', | |
'label' => 'AB/NAB Fields' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_catnum', | |
'label' => 'Lot Cat#' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_number', | |
'label' => 'Lot #', | |
'group_close' => '</optgroup>' | |
); | |
foreach($woo_ped_addon_fields as $fld ){ | |
$product->$fld['name'] = ( isset( $import->{'csv_'.$fld['name']}[$count] ) ? $import->{'csv_'.$fld['name']}[$count] : null ); | |
} | |
return $product; | |
} | |
add_filter( 'woo_pd_create_product_addons', 'alo_pd_create_product_addons', 10, 2 ); | |
function alo_pd_create_product_addons( $product, $import ) { | |
$woo_ped_addon_fields = array(); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'newprod_date', | |
'label' => 'New Product Date', | |
'acf' => true, | |
'group_open' => '<optgroup label="Extra Fields">' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'prod_type_abnab', | |
'label' => 'Product Type ab/nab', | |
'acf' => true, | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'wc_productdata_options', | |
'label' => 'AB/NAB Fields' | |
); | |
//Lots | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_catnum', | |
'label' => 'Lot Cat#' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_number', | |
'label' => 'Lot #', | |
'group_close' => '</optgroup>' | |
); | |
foreach($woo_ped_addon_fields as $fld ){ | |
if( $product->$fld['name'] !== null ) { | |
if( $product->post_type <> 'product_variation' ) { | |
if( WOO_PD_DEBUG !== true ) { | |
if (!empty($fld['acf']) && $fld['acf']) { //Diiferent method for ACF fields | |
update_field($fld['name'], $product->$fld['name'], $product->ID); | |
} else { | |
update_post_meta($product->ID, $fld['name'], $product->$fld['name']); | |
} | |
} | |
} | |
} | |
} | |
return $product; | |
} | |
add_filter( 'woo_pd_create_product_log_addons', 'alo_pd_create_product_log_addons', 10, 2 ); | |
function alo_pd_create_product_log_addons( $import, $product ) { | |
$woo_ped_addon_fields[] = array( | |
'name' => 'newprod_date', | |
'label' => 'New Product Date', | |
'group_open' => '<optgroup label="Extra Fields">' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'prod_type_abnab', | |
'label' => 'Product Type ab/nab' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'wc_productdata_options', | |
'label' => 'AB/NAB Fields' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_catnum', | |
'label' => 'Lot Cat#' | |
); | |
$woo_ped_addon_fields[] = array( | |
'name' => 'post_lot_number', | |
'label' => 'Lot #', | |
'group_close' => '</optgroup>' | |
); | |
foreach($woo_ped_addon_fields as $fld ){ | |
if( $product->$fld['name'] !== null ) { | |
if( $product->post_type <> 'product_variation' ) { | |
if( $import->advanced_log ) | |
$import->log .= "<br />>>>>>> " . sprintf( __( 'Setting '.$fld['label'].': %s', 'woocommerce-product-importer' ), $product->$fld['name'] ); | |
else | |
$import->log .= "<br />>>>>>> " . __( 'Setting '.$fld['label'], 'woocommerce-product-importer' ); | |
} else { | |
if( $import->advanced_log ) | |
$import->log .= "<br />>>>>>> " . __( 'Skipping ' . $fld['label'] .', cannot set '.$fld['label'].' for Variations', 'woocommerce-product-importer' ); | |
else | |
$import->log .= "<br />>>>>>> " . __( 'Skipping '.$fld['label'], 'woocommerce-product-importer' ); | |
} | |
} else if( $import->advanced_log ) { | |
$import->log .= "<br />>>>>>> " . __( 'Skipping '.$fld['label'], 'woocommerce-product-importer' ); | |
} | |
} | |
return $import; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment