Created
April 19, 2022 05:26
-
-
Save webtoffee-git/ee958a3b9bf98eca259b216e378ae74f to your computer and use it in GitHub Desktop.
To import/export related product values based on product SKUs using WP all import/export (https://wordpress.org/plugins/wt-woocommerce-related-products/)
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 //do not copy this line of code | |
add_filter('wp_all_export_csv_rows', 'wt_process_all_export_export', 999999, 3); | |
function wt_process_all_export_export($rows, $export_options, $export_id) { | |
$product_skus = array(); | |
foreach ($rows as $r_key => $row) { | |
foreach ($row as $d_key => $d_value) { | |
if ($d_key == '_crp_related_ids' && !empty($d_value) && (@unserialize($d_value) == false)) { | |
$s_value = explode(',', $d_value); | |
if (!empty($s_value)) { | |
foreach ($s_value as $key => $id) { | |
$product_sku = get_post_meta($id, '_sku', true); | |
$product_skus[] = $product_sku; | |
} | |
} | |
} | |
} | |
if (!empty(array_filter($product_skus))) { | |
$rows[$r_key][$d_key] = implode(",", $product_skus); | |
} | |
unset($product_skus); | |
} | |
return $rows; | |
} | |
/*To import related product values based on product SKUs based using WP all import*/ | |
add_action('pmxi_update_post_meta', 'wt_process_all_import_imports', 99999, 3); | |
function wt_process_all_import_imports($pid, $meta_key, $meta_value) { | |
if ($meta_key == '_crp_related_ids') { | |
$custom_meta = explode( ",", $meta_value ); | |
foreach ($custom_meta as $sku) { | |
$product_ids[] = wc_get_product_id_by_sku(trim($sku)); | |
} | |
} | |
update_post_meta($pid, $meta_key, $product_ids); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment