WooCommerce保存产品变体时如何向WP管理员展示上传错误
WooCommerce变体图片上传第三方服务器出错时通知管理员的方案
1. 即时邮件通知
直接通过wp_mail给管理员邮箱发送错误信息,适合需要即时知晓错误的场景。
add_action('woocommerce_save_product_variation', 'upload_variation_image_to_third_party', 10, 2); function upload_variation_image_to_third_party($variation_id, $i) { $variation = wc_get_product($variation_id); $image_id = $variation->get_image_id(); if (!$image_id) return; $image_url = wp_get_attachment_url($image_id); $error_message = ''; try { // 第三方服务器上传逻辑示例 $upload_response = wp_remote_post('第三方服务器接口地址', [ 'body' => [ 'image_url' => $image_url, 'variation_id' => $variation_id ] ]); if (is_wp_error($upload_response)) { throw new Exception($upload_response->get_error_message()); } $response_body = wp_remote_retrieve_body($upload_response); $response_data = json_decode($response_body, true); if (!isset($response_data['success']) || !$response_data['success']) { throw new Exception(isset($response_data['message']) ? $response_data['message'] : '上传未知错误'); } } catch (Exception $e) { $error_message = '产品变体ID ' . $variation_id . ' 图片上传第三方服务器失败:' . $e->getMessage(); // 获取管理员邮箱并发送通知 $admin_email = get_option('admin_email'); wp_mail( $admin_email, '[WooCommerce] 变体图片上传失败通知', $error_message, ['Content-Type: text/plain; charset=UTF-8'] ); // 同时记录到PHP错误日志(可选) error_log($error_message); } }
2. 后台管理员持久化通知
将错误信息存储到WordPress选项中,管理员进入后台时会看到醒目的错误提示,避免邮件被拦截。
add_action('woocommerce_save_product_variation', 'upload_variation_image_to_third_party', 10, 2); function upload_variation_image_to_third_party($variation_id, $i) { $variation = wc_get_product($variation_id); $image_id = $variation->get_image_id(); if (!$image_id) return; $image_url = wp_get_attachment_url($image_id); $error_message = ''; try { // 第三方服务器上传逻辑(同上) } catch (Exception $e) { $error_message = '产品变体ID ' . $variation_id . ' 图片上传第三方服务器失败:' . $e->getMessage(); // 存储错误信息,保留最近10条避免数据冗余 $upload_errors = get_option('wc_variation_image_upload_errors', []); $upload_errors[] = [ 'variation_id' => $variation_id, 'message' => $error_message, 'timestamp' => current_time('mysql') ]; if (count($upload_errors) > 10) { array_shift($upload_errors); } update_option('wc_variation_image_upload_errors', $upload_errors); // 可选:同时发送邮件通知 $admin_email = get_option('admin_email'); wp_mail($admin_email, '[WooCommerce] 变体图片上传失败通知', $error_message); } } // 在后台页面显示错误通知 add_action('admin_notices', 'display_variation_image_upload_errors'); function display_variation_image_upload_errors() { $upload_errors = get_option('wc_variation_image_upload_errors', []); if (empty($upload_errors)) return; echo '<div class="notice notice-error is-dismissible">'; echo '<p><strong>WooCommerce变体图片上传失败通知:</strong></p>'; echo '<ul>'; foreach ($upload_errors as $error) { echo '<li>' . esc_html($error['timestamp']) . ' - ' . esc_html($error['message']) . '</li>'; } echo '</ul>'; echo '</div>'; }
3. WooCommerce日志记录+邮件通知
利用WooCommerce内置日志系统记录详细错误,同时发送邮件提醒,方便后续排查问题。
add_action('woocommerce_save_product_variation', 'upload_variation_image_to_third_party', 10, 2); function upload_variation_image_to_third_party($variation_id, $i) { $variation = wc_get_product($variation_id); $image_id = $variation->get_image_id(); if (!$image_id) return; $image_url = wp_get_attachment_url($image_id); $logger = wc_get_logger(); $context = ['source' => 'variation-image-upload']; try { // 第三方服务器上传逻辑(同上) } catch (Exception $e) { $error_message = '产品变体ID ' . $variation_id . ' 图片上传第三方服务器失败:' . $e->getMessage(); // 记录到WooCommerce日志 $logger->error($error_message, $context); // 发送邮件通知管理员 $admin_email = get_option('admin_email'); wp_mail( $admin_email, '[WooCommerce] 变体图片上传失败通知', $error_message . "\n\n可在WooCommerce > 状态 > 日志中查看详细记录", ['Content-Type: text/plain; charset=UTF-8'] ); } }
注意事项
- 确保服务器支持
wp_mail函数,若邮件发送失败,可配置SMTP插件优化邮件投递。 - 第三方上传逻辑需根据实际接口调整,注意处理超时、HTTP错误状态码等异常情况。
- 持久化通知可添加关闭后清空错误的逻辑,避免通知长期显示。
内容的提问来源于stack exchange,提问作者mewiben39
相关产品推荐
相关产品推荐

