You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

查询WooCommerce商品图片变更发邮件时图库正确POST字段名

问题解答

WooCommerce原生后台编辑商品时,对应字段如下:

  • 商品图库提交的正确POST字段名是 product_image_gallery,字段值为英文逗号拼接的附件ID字符串,需要先拆分转成整型数组再和原有数据对比
  • 如果需要同时监控商品主图(特色图)变更,对应的POST字段名是 _thumbnail_id

除了字段名错误外,你原有的逻辑还缺少商品类型校验、参数安全过滤的步骤,完整可运行的代码示例如下:

add_action('pre_post_update', 'product_image_change_email', 10, 2);
function product_image_change_email($post_ID, $data) {
    // 仅触发商品类型内容的更新校验
    if (get_post_type($post_ID) !== 'product') {
        return;
    }
    $product = wc_get_product($post_ID);
    if (!$product) {
        return;
    }

    // 获取原有图片数据
    $old_gallery_ids = $product->get_gallery_image_ids();
    $old_thumbnail_id = $product->get_image_id();
    $image_changed = false;
    $change_log = [];

    // 校验图库是否变更
    if (isset($_POST['product_image_gallery'])) {
        $new_gallery_str = sanitize_text_field($_POST['product_image_gallery']);
        $new_gallery_ids = $new_gallery_str ? array_map('intval', explode(',', $new_gallery_str)) : [];
        if ($new_gallery_ids !== $old_gallery_ids) {
            $image_changed = true;
            $change_log[] = '商品图库图片调整';
        }
    }

    // 校验主图是否变更
    if (isset($_POST['_thumbnail_id'])) {
        $new_thumbnail_id = intval($_POST['_thumbnail_id']);
        if ($new_thumbnail_id !== $old_thumbnail_id) {
            $image_changed = true;
            $change_log[] = '商品主图替换';
        }
    }

    // 有变更时发送通知邮件
    if ($image_changed) {
        $to = '你的接收通知邮箱';
        $subject = '商品图片变更通知:' . $product->get_name();
        $message = "商品ID:{$post_ID}\n商品名称:" . $product->get_name() . "\n变更内容:" . implode('、', $change_log);
        wp_mail($to, $subject, $message);
    }
}

注:如果你使用第三方插件自定义了商品图库字段,字段名可能存在差异,上述字段为WooCommerce原生默认值。

内容的提问来源于stack exchange,提问作者Jedidiah Fowler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 16:36:04