When importing products into WooCommerce, it’s common to encounter errors due to various reasons such as incorrect data formatting or missing required fields. To handle these errors and provide useful feedback to the user, you can use the woocommerce_product_import_inserted_product and woocommerce_product_import_insert_product_errors hooks.
The woocommerce_product_import_inserted_product hook is triggered after a product has been successfully imported. You can use this hook to perform any additional actions or validations on the imported product.
Here’s an example of how you can use this hook to log the imported product’s ID and title:
function wpsnippets_log_imported_product( $product_id, $product_data ) {
// Log the imported product's ID and title
error_log( 'Imported product: ID ' . $product_id . ', Title: ' . $product_data['post_title'] );
}
add_action( 'woocommerce_product_import_inserted_product', 'wpsnippets_log_imported_product', 10, 2 );
On the other hand, the woocommerce_product_import_insert_product_errors hook allows you to handle errors that occur during the product import process. You can use this hook to display error messages or perform custom error handling.
Here’s an example of how you can use this hook to display error messages to the user:
function wpsnippets_display_import_errors( $errors ) {
// Display each error message
foreach ( $errors as $error ) {
echo '<div class="error">' . esc_html( $error ) . '</div>';
}
}
add_action( 'woocommerce_product_import_insert_product_errors', 'wpsnippets_display_import_errors' );
By using these hooks, you can enhance the product import process in WooCommerce by logging successful imports and handling errors effectively.
Examples
Example 1: Handling WooCommerce product import errors with a custom function
This example demonstrates how to handle errors that may occur during the import of WooCommerce products using a custom PHP function. The function wpsnippets_handle_product_import_errors() takes the error message as a parameter and logs it to the WordPress debug log.
function wpsnippets_handle_product_import_errors( $error_message ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'WooCommerce product import error: ' . $error_message );
}
}
Explanation: The wpsnippets_handle_product_import_errors() function checks if the WP_DEBUG constant is defined and set to true. If so, it logs the error message to the WordPress debug log using the error_log() function.
Example 2: Displaying WooCommerce product import errors to the user
In this example, we’ll show how to display import errors to the user during the WooCommerce product import process. We’ll use the woocommerce_product_import_error action hook to add a custom error message to the import log.
function wpsnippets_display_product_import_errors( $error_message ) {
$import_log = WC_Install::get_log_file_path( 'product_import' );
if ( $import_log ) {
$error_message .= ' Please check the import log for more details: <a href="' . esc_url( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) . '">' . esc_html__( 'Import Log', 'woocommerce' ) . '</a>';
}
return $error_message;
}
add_filter( 'woocommerce_product_import_error', 'wpsnippets_display_product_import_errors' );
Explanation: The wpsnippets_display_product_import_errors() function appends a custom error message to the existing error message. It includes a link to the WooCommerce import log page where the user can find more details about the import errors. The function is hooked into the woocommerce_product_import_error filter.
Example 3: Sending email notifications for WooCommerce product import errors
In this example, we’ll demonstrate how to send email notifications to the site administrator whenever a WooCommerce product import error occurs. We’ll use the woocommerce_product_import_error action hook to trigger the email notification.
function wpsnippets_send_product_import_error_notification( $error_message ) {
$admin_email = get_option( 'admin_email' );
$subject = 'WooCommerce Product Import Error';
$message = 'An error occurred during the WooCommerce product import: ' . $error_message;
wp_mail( $admin_email, $subject, $message );
}
add_action( 'woocommerce_product_import_error', 'wpsnippets_send_product_import_error_notification' );
Explanation: The wpsnippets_send_product_import_error_notification() function retrieves the site administrator’s email address from the WordPress options and sends an email notification with the error message. The function is hooked into the woocommerce_product_import_error action.
