Woocommerce集成谷歌GMC顾客评价Customer Reviews代码
<?php //请替换代码中的XXXXX为GMC ID /**1, Add Google review language scripts*/ function gmc_google_customer_reviews_language() { $gmc_customer_reviews_language_script = 'window.___gcfg = {lang: \'en_US\'};'; wp_register_script( 'gmc_customer_reviews_language_script', '', '', 'false', 'true' ); wp_enqueue_script( 'gmc_customer_reviews_language_script' ); wp_add_inline_script( 'gmc_customer_reviews_language_script', $gmc_customer_reviews_language_script ); } add_action( 'wp_enqueue_scripts', 'gmc_google_customer_reviews_language', 20 ); /*** 2, Add Google Reviews request to order thank you page*/ function gmc_google_customer_reviews_optin( $order_id ) { $order = new WC_Order( $order_id ); ?> <script src="https://apis.google.com/js/platform.js?onload=renderOptIn" async defer></script> <script> window.renderOptIn = function() { window.gapi.load('surveyoptin', function() { window.gapi.surveyoptin.render( { // REQUIRED FIELDS "merchant_id": XXXXX, "order_id": "<?php echo esc_attr( $order->get_order_number() ); ?>", "email": "<?php echo esc_attr( $order->get_billing_email() ); ?>", "delivery_country": "<?php echo esc_attr( $order->get_billing_country() ); ?>", "estimated_delivery_date": "<?php echo esc_attr( date( 'Y-m-d', strtotime( '+5 day', strtotime( $order->get_date_created() ) ) ) ); ?>", // replace "5 day" with the estimated delivery time of your orders "opt_in_style": "CENTER_DIALOG" }); }); }</script> <?php } add_action( 'woocommerce_thankyou', 'gmc_google_customer_reviews_optin' ); /**3, Add Google Reviews badge to site*/ function gmc_google_customer_reviews_badge() { $gmc_google_customer_reviews_script = '<script src="https://apis.google.com/js/platform.js?onload=renderBadge" async defer></script>'; echo $gmc_google_customer_reviews_script; $gmc_google_customer_reviews_badge_script = ' window.renderBadge = function() { var ratingBadgeContainer = document.createElement("div"); document.body.appendChild(ratingBadgeContainer); window.gapi.load(\'ratingbadge\', function() { window.gapi.ratingbadge.render(ratingBadgeContainer, { "merchant_id": XXXXX, "position": "BOTTOM_LEFT" }); }); }'; wp_register_script( 'gmc_google_customer_reviews_badge_script', '', '', 'false', 'true' ); wp_enqueue_script( 'gmc_google_customer_reviews_badge_script' ); wp_add_inline_script( 'gmc_google_customer_reviews_badge_script', $gmc_google_customer_reviews_badge_script ); } add_action( 'wp_enqueue_scripts', 'gmc_google_customer_reviews_badge', 20 );