add_filter( 'rank_math/json_ld', function( $data, $jsonld ) { if ( empty( $data['richSnippet'] ) || ! in_array( $data['richSnippet']['@type'], [ 'Product', 'ProductGroup' ] ) ) { return $data; } // ✅ 修正 shippingRate 结构,避免 Google 报错 $data['shippingDetails'] = [ '@context' => 'https://schema.org/', '@type' => 'OfferShippingDetails', '@id' => '#shipping_policy', 'deliveryTime' => [ '@type' => 'ShippingDeliveryTime', 'handlingTime' => [ '@type' => 'QuantitativeValue', 'minValue' => 1, 'maxValue' => 2, 'unitCode' => 'DAY', ], 'transitTime' => [ '@type' => 'QuantitativeValue', 'minValue' => 5, 'maxValue' => 10, 'unitCode' => 'DAY', ], ], 'shippingRate' => [ // ✅ 只保留一个对象,避免重复字段 '@type' => 'PriceSpecification', // ✅ 修改 @type 'price' => 5.95, // ✅ 直接定义运费 'priceCurrency' => 'USD', 'eligibleTransactionVolume' => [ '@type' => 'PriceSpecification', 'price' => 99, // ✅ 满 $99 免运费 'priceCurrency' => 'USD', ], ], 'shippingDestination' => [ '@type' => 'DefinedRegion', 'addressCountry' => 'US', ], ]; // 退货政策 $data['hasMerchantReturnPolicy'] = [ '@context' => 'https://schema.org/', '@type' => 'MerchantReturnPolicy', '@id' => '#merchant_policy', 'applicableCountry' => 'US', 'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow', 'merchantReturnDays' => 30, 'returnMethod' => 'https://schema.org/ReturnByMail', 'returnFees' => 'https://schema.org/CustomerResponsibility', ]; if ( 'Product' === $data['richSnippet']['@type'] ) { $data['richSnippet']['offers']['shippingDetails'] = [ '@id' => '#shipping_policy' ]; $data['richSnippet']['offers']['hasMerchantReturnPolicy'] = [ '@id' => '#merchant_policy' ]; return $data; } if ( empty( $data['richSnippet']['hasVariant'] ) ) { return $data; } foreach ( $data['richSnippet']['hasVariant'] as $key => $value ) { if ( empty( $value['offers'] ) ) { continue; } $data['richSnippet']['hasVariant'][$key]['offers']['shippingDetails'] = [ '@id' => '#shipping_policy' ]; $data['richSnippet']['hasVariant'][$key]['offers']['hasMerchantReturnPolicy'] = [ '@id' => '#merchant_policy' ]; } return $data; }, 10, 2);