Skip to content

Instantly share code, notes, and snippets.

@mitchellkrogza
Created September 8, 2025 02:34
Show Gist options
  • Save mitchellkrogza/fce99de8f4c6a41ffce741c9ade2fee1 to your computer and use it in GitHub Desktop.
Save mitchellkrogza/fce99de8f4c6a41ffce741c9ade2fee1 to your computer and use it in GitHub Desktop.
Google Customer Reviews OPT IN AT CHECKOUT
/**
* Adds the Google Customer Reviews opt-in form to the checkout confirmation page
* Add your merchant ID where the placeholder "123456789" is.
*
* @param int|string $order_id WooCommerce order id
*
*/
function 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": 123456789, // mywebsite.com
"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( '+7 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', 'google_customer_reviews_optin' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment