How to Import Suppliers 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 "Supplier" field using a temporary Custom Field and the 'pmxi_saved_post' hook.
- Add a New Custom Field in WP All Import and name it "_temp_supplier"(see https://d.pr/i/3Z8iGv).
- That field will store the name of the supplier in the import file, and it'll be used to get that supplier's ID from the database. Keep in mind that this will not create new suppliers, so they need to exist on the site before running the import.
- Paste the function in the Function Editor.
- The function will use the ID obtained from the supplier's name, in the 'atum_product_data' table, 'supplier_id' column, for each imported product.
function save_data_to_custom_database_table($id) {
global $wpdb; // Make wpdb object available
// Retrieve value from '_temp_supplier' meta key
$temp_supplier = get_post_meta($id, '_temp_supplier', true);
// Check if 'temp_supplier' post exists
$temp_supplier_post_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE post_title = %s",
$temp_supplier
)
);
// Define target database table
$table_name = $wpdb->prefix . "atum_product_data";
if ($temp_supplier_post_id) {
// Check if the record already exists
$exists = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE product_id = %d",
$id
)
);
// Update or insert value into database table
if ($exists) {
$wpdb->update(
$table_name,
['supplier_id' => $temp_supplier_post_id],
['product_id' => $id],
['%d'],
['%d']
);
} else {
$wpdb->insert(
$table_name,
['product_id' => $id, 'supplier_id' => $temp_supplier_post_id],
['%d', '%d']
);
}
}
// Delete temporary custom field
delete_post_meta($id, '_temp_supplier');
}
add_action('pmxi_saved_post', 'save_data_to_custom_database_table', 10, 1);