Skip to content

Instantly share code, notes, and snippets.

@trey8611
Created November 17, 2021 21:31
Show Gist options
  • Save trey8611/53c9e2a21dc61cdd286d27c63695fc85 to your computer and use it in GitHub Desktop.
Save trey8611/53c9e2a21dc61cdd286d27c63695fc85 to your computer and use it in GitHub Desktop.
[WP All Import - Export taxonomy mapping rules]

This snippet will export a file named "import_rules_{import_id}.csv" in your WordPress uploads folder with your taxonomy mapping rules. Be sure to change 'product_cat' to the appropriate taxonomy name if you're not exporting WooCommerce Product Category rules.

function my_export_maprules( $import_id ) {
    $import = new PMXI_Import_Record();
    $import->getById( $import_id );
    if ( $import->isEmpty() ) return false;
    
    $options = maybe_unserialize( $import->options );
    $rules   = $options['tax_mapping']['product_cat'];
    $rules   = json_decode( $rules, 1 );
    
    $uploads = wp_upload_dir();
    $filename = $uploads['basedir'] . '/import_rules_'.$import_id.'.csv';
    if (file_exists($filename)){
            @unlink($filename);
    } else {
        touch( $filename );
    }
    
    $csv = array( array( 'In Your File', 'Translated To' ) );
    
    foreach ( $rules as $rule ) {
        $key = key( $rule );
        $csv[] = array( $key, array_pop( $rule ) );
    }
    
    $fp = fopen( $filename, 'w');
      
    foreach ( $csv as $fields ) {
        fputcsv( $fp, $fields );
    }
      
    fclose( $fp );
}

Usage:

my_export_maprules( 8 );

Change "8" to your import ID. You can use the Code Snippets plugin ( https://wordpress.org/plugins/code-snippets/ ) and their "Only run once" feature to run the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment