/*
Theme Name:   xstore-child
Theme URI:    https://xstore.8theme.com
Description:  XStore is a multi-purpose theme that offers the ultimate WordPress and WooCommerce synergy, providing a comprehensive, all-in-one solution.
Author:       8theme
Author URI:   https://www.8theme.com
Template:     xstore
Version:      1.0
Text Domain:  xstore-child
Tags: e-commerce, two-columns, left-sidebar, right-sidebar, custom-background, custom-colors, custom-header, custom-menu, custom-logo, featured-images, full-width-template, threaded-comments, accessibility-ready, rtl-language-support, footer-widgets, sticky-post, theme-options, translation-ready, ecommerce, woocommerce, shop, elementor, business, corporate, blog, news, light, dark
*/ add_action('woocommerce_thankyou', 'kb_google_customer_reviews_optin');

function kb_google_customer_reviews_optin($order_id) {
    if (!$order_id) return;

    $order = wc_get_order($order_id);
    if (!$order) return;

    $email = $order->get_billing_email();
    $order_number = $order->get_order_number();

    // Kod kraju dostawy
    $delivery_country = $order->get_shipping_country();
    if (empty($delivery_country)) {
        $delivery_country = $order->get_billing_country();
    }

    // Szacowana data dostawy – ustaw realnie
    $estimated_delivery_date = date('Y-m-d', strtotime('+5 days'));

    ?>
    <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({
            "merchant_id": 5639420889,
            "order_id": "<?php echo esc_js($order_number); ?>",
            "email": "<?php echo esc_js($email); ?>",
            "delivery_country": "<?php echo esc_js($delivery_country); ?>",
            "estimated_delivery_date": "<?php echo esc_js($estimated_delivery_date); ?>"
          });
        });
      }
    </script>
    <?php
}