Created
August 17, 2018 11:21
-
-
Save xadapter/2920ed0878aa202237d5b465cd10c0d8 to your computer and use it in GitHub Desktop.
Import / Export bundle products using XAdapter Product Import Export for WooCommerce plugin - - with SKU
This file contains hidden or 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
add_filter('hf_insert_post_extra_data', 'hf_import_alter_product_csv_row_data', 100, 3); | |
add_action('hf_alter_product_export_csv_columns', 'hf_alter_product_csv_columns_head', 100); | |
add_action('hf_alter_product_export_csv_data', 'hf_alter_product_csv_row_data', 100, 2); | |
/** | |
Product bundle export/import code snippet - with SKU | |
@global type $wpdb | |
@param type $additional_columns | |
@return array | |
*/ | |
function hf_alter_product_csv_columns_head($additional_columns) { | |
global $wpdb; | |
$table_name = $wpdb->prefix . 'woocommerce_bundled_itemmeta'; | |
$meta_keys = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT * FROM $table_name ORDER BY `meta_key` ASC", array())); | |
// get meta_key value from object | |
$meta_keys = array_unique(wp_list_pluck($meta_keys, 'meta_key')); | |
$additional_columns[] = 'meta:bundle_bundled_par_item_id'; | |
foreach ($meta_keys as $value) { | |
if('real_id'!=$value){ | |
$additional_columns[] = 'meta:bundle_' . $value; | |
} | |
} | |
return $additional_columns; | |
} | |
function hf_alter_product_csv_row_data($additional_data, $productID) { | |
global $wpdb; | |
$bundled_items = $wpdb->get_results($wpdb->prepare('SELECT * FROM ' . $wpdb->prefix . 'woocommerce_bundled_items WHERE `bundle_id`=' . $productID, array())); | |
$itemmeta = array(); | |
$par_id = ''; | |
foreach ($bundled_items as $key => $meta_value) { | |
$prod = wc_get_product($meta_value->product_id); | |
$par_id .= '|' . $prod->get_sku(); | |
$itemmeta[] = $wpdb->get_results($wpdb->prepare("SELECT * FROM " . $wpdb->prefix . "woocommerce_bundled_itemmeta WHERE `bundled_item_id`= $meta_value->bundled_item_id ORDER BY meta_key", array())); | |
} | |
$extra_data = array(); | |
if (is_array($itemmeta) && !empty($itemmeta)) { | |
foreach ($itemmeta as $bundle_index => $bundled_arr) { | |
if (is_array($bundled_arr) && !empty($bundled_arr)) { | |
foreach ($bundled_arr as $key => $meta_value) { | |
if('real_id'!=$meta_value->meta_key){ | |
isset($extra_data[$key]) ? $extra_data[$key] .= '|' . $meta_value->meta_value : $extra_data[$key] = $meta_value->meta_value; | |
} | |
} | |
} | |
} | |
} | |
$additional_data[] = ltrim($par_id, '|'); | |
foreach ($extra_data as $key => $extra_value) { | |
$additional_data[] = $extra_value; | |
} | |
return $additional_data; | |
} | |
function hf_import_alter_product_csv_row_data($post_meta, $post, $post_id = '') { | |
global $wpdb; | |
foreach ($post_meta as $post_key => &$meta_array) { | |
if ($meta_array['key'] == 'bundle_bundled_par_item_id') { | |
$bundled_items_id = ''; | |
$bundle_par_id_arra = explode("|", $meta_array['value']); | |
foreach ($bundle_par_id_arra as $key => $sku) { | |
$bundle_par_id_arra[$key] = wc_get_product_id_by_sku($sku); | |
} | |
$bundle_id = !empty($post_id) ? $post_id : $post['post_id']; | |
foreach ($bundle_par_id_arra as $par_index => $bundle_par_id) { | |
$results = $wpdb->get_results($wpdb->prepare('SELECT bundled_item_id FROM ' . $wpdb->prefix . 'woocommerce_bundled_items WHERE `bundle_id`=' . $bundle_id . ' and `product_id`=' . $bundle_par_id, array())); | |
if (!$results) { | |
$wpdb->insert($wpdb->prefix . 'woocommerce_bundled_items', array('product_id' => $bundle_par_id, 'bundle_id' => !empty($post_id) ? $post_id : $post['post_id'],)); | |
$bundled_items_id[$par_index] = $wpdb->insert_id; | |
} else { | |
foreach ($results as $value) { | |
$bundled_items_id[$par_index] = $value->bundled_item_id; | |
} | |
} | |
} | |
unset($post_meta[$post_key]); | |
continue; | |
} | |
if (!empty($bundled_items_id) && strpos($meta_array['key'], 'bundle_') !== false) { | |
$meta_array['key'] = str_replace("bundle_", "", $meta_array['key']); | |
$bundle_par_data_arra = explode("|", $meta_array['value']); | |
$bundle_id = !empty($post_id) ? $post_id : $post['post_id']; | |
foreach ($bundle_par_data_arra as $par_index => $bundle_meta) { | |
$results = $wpdb->get_results($wpdb->prepare('SELECT meta_id FROM ' . $wpdb->prefix . 'woocommerce_bundled_itemmeta WHERE `bundled_item_id`=' . $bundled_items_id[$par_index] . ' and `meta_key`="'. $meta_array["key"].'"', array())); | |
if (!$results) { | |
$wpdb->insert($wpdb->prefix . 'woocommerce_bundled_itemmeta', array('bundled_item_id' => $bundled_items_id[$par_index], 'meta_key' => $meta_array['key'], 'meta_value' => $bundle_meta,)); | |
} else { | |
$wpdb->update($wpdb->prefix . 'woocommerce_bundled_itemmeta', array('bundled_item_id' => $bundled_items_id[$par_index], 'meta_key' => $meta_array['key'], 'meta_value' => $bundle_meta,), array('meta_id'=>$results[0]->meta_id)); | |
} | |
} | |
unset($post_meta[$post_key]); | |
} | |
} | |
return $post_meta; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment