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();
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", "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", "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>'; } }
|