Skip to content

Instantly share code, notes, and snippets.

@trey8611
Created November 19, 2021 16:28
Show Gist options
  • Save trey8611/4b1464a95af72e313fcf2a08dd15ff56 to your computer and use it in GitHub Desktop.
Save trey8611/4b1464a95af72e313fcf2a08dd15ff56 to your computer and use it in GitHub Desktop.
[WP All Import] Custom log / custom logging functions with our API

This shows how to save a custom log of imported items. It can be adjusted to include any product data.

  /***********************************************/
 /***** BEGIN LOG CREATE / UPDATE / DELETE  *****/
/********************************************* */

// This creates a custom log file in the root WordPress uploads folder
// Some of this code will need to be adjusted, please read comments.

// Delete log file before import starts
function my_erase_log_updates( $import_id ) {
    $uploads = wp_upload_dir();
    $log_file_name = my_get_log_file_name();
    if ( file_exists( $log_file_name ) ) {
        unlink( $log_file_name );
    }
}

function my_get_log_file_data( $log_file_name ) {
    if ( file_exists( $log_file_name ) ) {
        $existing_data = file_get_contents( $log_file_name );
    } else {
        touch( $log_file_name );
        $existing_data = '';            
    }
    return $existing_data;
}
function my_get_log_file_name() {
    $import_id = wp_all_import_get_import_id();
    $uploads = wp_upload_dir();
    $log_file_name = $uploads['basedir'] . '/' . $import_id . '_import_custom_log.txt';
    return $log_file_name;   
}

// Log all skips
function my_log_skips( $created, $import_id, $xml_data ) {
    $log_file_name = my_get_log_file_name();
    $existing_data = my_get_log_file_data( $log_file_name );
  	// This assumes {sku[1]} and {title[1]}
  	// Update this code if your import elements are different
    if ( array_key_exists( 'sku', $xml_data ) && array_key_exists( 'title', $xml_data ) ) {
        $existing_data .= '[ID: N/A] [SKU: ' . $xml_data['sku'] . '] [Title: ' . $xml_data['title'] . '] has been skipped.' . PHP_EOL;
        file_put_contents( $log_file_name, $existing_data );
    }
}

// Log all updates and creates
function my_log_updates( $id, $xml, $is_update ) {
    $log_file_name = my_get_log_file_name();
    $existing_data = my_get_log_file_data( $log_file_name );

    $product = wc_get_product( $id );

    if ( ! $product ) {
        return;
    }

    if ( $is_update ) {
        $existing_data .= '[ID: ' . $id . '] [SKU: ' . $product->get_sku() . '] [Title: ' . $product->get_name() . '] has been updated.' . PHP_EOL;
    } else {
        $existing_data .= '[ID: ' . $id . '] [SKU: ' . $product->get_sku() . '] [Title: ' . $product->get_name() . '] has been created.' . PHP_EOL;
    }

    file_put_contents( $log_file_name, $existing_data );
}

// Log all deletes
function my_log_deletes( $ids = array() ){
    $log_file_name = my_get_log_file_name();
    $existing_data = my_get_log_file_data( $log_file_name );

    foreach( $ids as $pid ) {
    
        $product = wc_get_product( $pid );

        $existing_data .= '[ID: ' . $pid . '] [SKU: ' . $product->get_sku() . '] [Title: ' . $product->get_name() . '] has been deleted.' . PHP_EOL;
        file_put_contents( $log_file_name, $existing_data );

    }
}

add_action( 'pmxi_before_xml_import', 'my_erase_log_updates', 10, 1 );
add_action( 'wp_all_import_post_skipped', 'my_log_skips', 10, 3 );
add_action( 'pmxi_saved_post', 'my_log_updates', 10, 3 );
add_action( 'pmxi_delete_post', 'my_log_deletes', 10, 1 );


  /********************************************** */
 /***** END LOG CREATE / UPDATE / DELETE  ****   */
/*********************************************  */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment