Skip to content

Instantly share code, notes, and snippets.

@juanlistab
Created April 13, 2024 17:31
Show Gist options
  • Save juanlistab/61beb02daaea63e807f93d5728511cac to your computer and use it in GitHub Desktop.
Save juanlistab/61beb02daaea63e807f93d5728511cac to your computer and use it in GitHub Desktop.
Import Data to ATUM Barcode using WP All Import #wpallimport #wpai

How to Import Barcodes to the ATUM Inventory Management for WooCommerce plugin using WP All Import Pro.

This function will import data to the ATUM Inventory Management for WooCommerce, more specifically, to the "Barcode" field using a temporary Custom Field and the 'pmxi_saved_post' hook.

Instructions:

  • Add a New Custom Field in WP All Import and name it "_temp_barcode"(see https://d.pr/i/F75Ze9).
  • Paste the function in the Function Editor.
  • The function will use the data from the temp field and insert it in the 'atum_product_data' table, 'barcode' column, for each imported product.
function save_data_to_custom_database_table($id) {
    global $wpdb; // Make wpdb object available

    // Retrieve value to save
    $value = get_post_meta($id, '_temp_barcode', true);

    // Define target database table
    $table_name = $wpdb->prefix . "atum_product_data";

    // Check if the record already exists
    $exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE product_id = %s", $id));

    // Update or insert value into database table
    if ($exists) {
        $wpdb->update($table_name, ['barcode' => $value], ['product_id' => $id], ['%s'], ['%s']);
    } else {
        $wpdb->insert($table_name, ['product_id' => $id, 'barcode' => $value], ['%s','%s']);
    }

    // Delete temporary custom field
    delete_post_meta($id, '_temp_barcode');
}
add_action('pmxi_saved_post', 'save_data_to_custom_database_table', 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment