使用PHP为Woocommerce产品生成(schema)结构化SEO商品(Product)数据

默认的woocommerce缺少了产品的结构化数据,虽然yoast支持,但是属于Pro功能。

使用PHP简单实现这个功能,顺便增加商家资讯的结构化SEO数据.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128


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>';
}
}