在运营大型WooCommerce商城时,我们经常会遇到产品标题重复的问题。重复的产品标题不仅影响用户体验,还会对SEO产生负面影响,导致搜索引擎难以区分相似产品,降低页面排名。
重复标题可能带来的问题
-
SEO负面影响:搜索引擎会认为内容是重复的,降低页面权重
-
用户体验差:客户难以区分相似产品
-
管理困难:管理员难以快速定位特定产品
-
数据混乱:可能导致订单处理错误
解决方案
我将提供一个完整的代码解决方案,用于检测和提醒WooCommerce产品标题重复问题。
// -------------------------
// WooCommerce 产品标题实时查重提示(右侧红字显示)
// -------------------------
add_action('admin_footer-post-new.php', 'realtime_product_title_check_side_notice');
add_action('admin_footer-post.php', 'realtime_product_title_check_side_notice');
function realtime_product_title_check_side_notice() {
$screen = get_current_screen();
if ($screen->post_type !== 'product') return;
?>
<style>
#title-duplicate-notice {
display: inline-block;
margin-left: 12px;
font-weight: 500;
}
</style>
<script>
jQuery(function($) {
let timer;
const titleField = $('#title');
const postID = $('#post_ID');
const noticeID = 'title-duplicate-notice';
if (!$('#' + noticeID).length) {
titleField.after('<span id="' + noticeID + '"></span>');
}
titleField.on('input', function() {
clearTimeout(timer);
const title = $(this).val().trim();
if (title.length < 2) return;
timer = setTimeout(function() {
$.post(ajaxurl, {
action: 'check_product_title_duplicate',
title,
post_id: postID.val()
}, function(res) {
$('#' + noticeID).html(
res.exists
? '<span style="color:#d63638;">⚠️ 标题已存在</span>'
: '<span style="color:#46b450;">✓ 可用</span>'
);
}, 'json');
}, 400);
});
});
</script>
<?php
}
