默认的woocommerce缺少了产品的结构化数据,虽然yoast支持,但是属于Pro功能。
使用PHP简单实现这个功能,顺便增加商家资讯的结构化SEO数据.
add_action('wp_head', 'add_product_structured_data');
function add_product_structured_data() {
if (is_product()) {
global $product;
// 获取商品信息
$product_id = get_the_ID();
$product_title = get_the_title();
$product_image = wp_get_attachment_url(get_post_thumbnail_id());
$product_description = esc_js(strip_tags(get_the_excerpt()));
$product_sku = $product->get_sku();
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
// 获取商家信息,使用网站名称作为商家名称(如果有具体商家字段,可以修改这里)
$seller_name = get_bloginfo('name'); // 网站名称作为商家名称
$seller_url = home_url(); // 网站首页 URL 作为商家网址
// 如果是变体商品,获取变体价格
if ($product->is_type('variable')) {
$variations = $product->get_available_variations();
$structured_data = [
"@context" => "https://schema.org/",
"@type" => "Product",
"name" => $product_title,
"image" => [$product_image],
"description" => $product_description,
"sku" => $product_sku,
"brand" => [
"@type" => "Brand",
"name" => $seller_name // 使用商家名称
],
"offers" => [],
"seller" => [
"@type" => "Organization", // 使用 Organization 类型表示商家
"name" => $seller_name,
"url" => $seller_url
]
];
// 获取每个变体的价格
foreach ($variations as $variation) {
$variation_product = wc_get_product($variation['variation_id']);
$variation_regular_price = $variation_product->get_regular_price();
$variation_sale_price = $variation_product->get_sale_price();
$variation_sku = $variation_product->get_sku();
$variation_url = get_permalink($variation['variation_id']);
$variation_availability = $variation_product->is_in_stock()
? "https://schema.org/InStock"
: "https://schema.org/OutOfStock";
// 如果变体有促销价格
if (!empty($variation_sale_price)) {
$structured_data['offers'][] = [
"@type" => "Offer",
"url" => $variation_url,
"priceCurrency" => "HKD",
"price" => $variation_sale_price, // 使用变体的促销价格
"availability" => $variation_availability,
"itemCondition" => "https://schema.org/NewCondition",
"description" => "特惠,原價{$variation_regular_price} HKD,現價{$variation_sale_price} HKD。"
];
} else {
// 如果没有促销价格,使用常规价格
$structured_data['offers'][] = [
"@type" => "Offer",
"url" => $variation_url,
"priceCurrency" => "HKD",
"price" => $variation_regular_price, // 使用变体的常规价格
"availability" => $variation_availability,
"itemCondition" => "https://schema.org/NewCondition"
];
}
}
} else {
// 如果是单一商品,处理常规价格或促销价格
$structured_data = [
"@context" => "https://schema.org/",
"@type" => "Product",
"name" => $product_title,
"image" => [$product_image],
"description" => $product_description,
"sku" => $product_sku,
"brand" => [
"@type" => "Brand",
"name" => $seller_name // 使用商家名称
],
"offers" => [],
"seller" => [
"@type" => "Organization", // 使用 Organization 类型表示商家
"name" => $seller_name,
"url" => $seller_url
]
];
// 如果没有促销价格,只有常规价格
if (!empty($sale_price)) {
$structured_data['offers'][] = [
"@type" => "Offer",
"url" => get_permalink(),
"priceCurrency" => "HKD",
"price" => $sale_price,
"availability" => "https://schema.org/InStock",
"itemCondition" => "https://schema.org/NewCondition",
"description" => "特惠,原價{$regular_price} HKD,現價{$sale_price} HKD。"
];
} else {
// 如果没有促销价格,使用常规价格
$structured_data['offers'][] = [
"@type" => "Offer",
"url" => get_permalink(),
"priceCurrency" => "HKD",
"price" => $regular_price,
"availability" => "https://schema.org/InStock",
"itemCondition" => "https://schema.org/NewCondition"
];
}
}
// 输出结构化数据
echo '<script type="application/ld+json">' . wp_json_encode($structured_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>';
}
}