HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/bwcdev/wp-content/themes/salient/functions.php
<?php
//  session_start();
// echo $_SESSION['uid'];
/**
 * Salient functions and definitions.
 *
 * @package Salient
 * @since 1.0
 */


/**
 * Define Constants.
 */
require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
// require_once '/guzzle-7.8/guzzle-7.8/src/Client.php';


$splash_inc_path = get_template_directory() . '/includes';
// Gravity Form IDs start
if (!defined('GF_INDIVIDUAL_FORM_ID'))
  define('GF_INDIVIDUAL_FORM_ID', 15);

// if (!defined('GF_PERSONAL_FORM_ID'))
// 	define('GF_PERSONAL_FORM_ID', 13);

if (!defined('GF_GROUP_FORM_ID'))
  define('GF_GROUP_FORM_ID', 13);

// if (!defined('GF_GROUP_FORM_ID'))
// define('GF_GROUP_FORM_ID', 44);

if (!defined('GF_UNLIMITED_FORM_ID'))
  define('GF_UNLIMITED_FORM_ID', 16);

/* PRODUCTION */

// if (!defined('GF_CHILD_DETAILS_FORM_ID'))
// 	 define('GF_CHILD_DETAILS_FORM_ID', 46);
// if (!defined('GF_TEAM_FORM_ID'))
//   define('GF_TEAM_FORM_ID', 47);

/* Gravity Form IDs end */

/* STAGING */

if (!defined('GF_CHILD_DETAILS_FORM_ID'))
  define('GF_CHILD_DETAILS_FORM_ID', 29);
if (!defined('GF_TEAM_FORM_ID'))
  define('GF_TEAM_FORM_ID', 28);

require_once($splash_inc_path . '/setup.php');
require_once($splash_inc_path . '/custom.php');


function enqueue_jquery()
{
  wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'enqueue_jquery');

function custom_footer_script()
{
  // Start or resume the session
  session_start();

  // Output the jQuery code only if $_SESSION['uid'] is set
  if (isset($_SESSION['uid'])) {
?>
    <script>
      jQuery(document).ready(function($) {
        // Change the menu item text to "My Account"
        $('#menu-item-1675').hide();
      });
    </script>
  <?php
  } else {
  ?>
    <script>
      jQuery(document).ready(function($) {
        // Change the menu item text to "My Account"
        $('#menu-item-1674').hide();
      });
    </script>
<?php
  }
}
add_action('wp_footer', 'custom_footer_script');
define('NECTAR_THEME_DIRECTORY', get_template_directory());
define('NECTAR_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/nectar/');
define('NECTAR_THEME_NAME', 'salient');


if (!function_exists('get_nectar_theme_version')) {
  function nectar_get_theme_version()
  {
    return '11.0.4';
  }
}


/**
 * Load text domain.
 */
add_action('after_setup_theme', 'nectar_lang_setup');

if (!function_exists('nectar_lang_setup')) {
  function nectar_lang_setup()
  {
    load_theme_textdomain('salient', get_template_directory() . '/lang');
  }
}


/**
 * General WordPress.
 */
require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/wp-general.php';


/**
 * Get Salient theme options.
 */

function get_nectar_theme_options()
{

  $legacy_options  = get_option('salient');
  $current_options = get_option('salient_redux');

  if (!empty($current_options)) {
    return $current_options;
  } elseif (!empty($legacy_options)) {
    return $legacy_options;
  } else {
    return $current_options;
  }
}

$nectar_options                    = get_nectar_theme_options();
$nectar_get_template_directory_uri = get_template_directory_uri();



/**
 * Register/Enqueue theme assets.
 */
require_once NECTAR_THEME_DIRECTORY . '/includes/class-nectar-element-assets.php';
require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/enqueue-scripts.php';
require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/enqueue-styles.php';
require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/dynamic-styles.php';


/**
 * Salient Plugin notices.
 */
require_once NECTAR_THEME_DIRECTORY . '/nectar/plugin-notices/salient-plugin-notices.php';


/**
 * Salient welcome page.
 */
require_once NECTAR_THEME_DIRECTORY . '/nectar/welcome/welcome-page.php';


/**
 * Theme hooks & actions.
 */
function nectar_hooks_init()
{

  require_once NECTAR_THEME_DIRECTORY . '/nectar/hooks/hooks.php';
  require_once NECTAR_THEME_DIRECTORY . '/nectar/hooks/actions.php';
}



add_action('after_setup_theme', 'nectar_hooks_init', 10);


/**
 * Post category meta.
 */
require_once NECTAR_THEME_DIRECTORY . '/nectar/meta/category-meta.php';


/**
 * Media and theme image sizes.
 */
require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/media.php';


/**
 * Navigation menus
 */
require_once NECTAR_THEME_DIRECTORY . '/nectar/assets/functions/wp-menu-custom-items/menu-item-custom-fields.php';
require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/nav-menus.php';


/**
 * TGM Plugin inclusion.
 */
require_once NECTAR_THEME_DIRECTORY . '/nectar/tgm-plugin-activation/class-tgm-plugin-activation.php';
require_once NECTAR_THEME_DIRECTORY . '/nectar/tgm-plugin-activation/required_plugins.php';


/**
 * WPBakery functionality.
 */

require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/wpbakery-init.php';

if (!is_page('my-account')) {
  /**
   * Theme skin specific class and assets.
   */
  $nectar_theme_skin    = (!empty($nectar_options['theme-skin'])) ? $nectar_options['theme-skin'] : 'original';
  $nectar_header_format = (!empty($nectar_options['header_format'])) ? $nectar_options['header_format'] : 'default';

  if ('centered-menu-bottom-bar' === $nectar_header_format) {
    $nectar_theme_skin = 'material';
  }

  add_filter('body_class', 'nectar_theme_skin_class');

  function nectar_theme_skin_class($classes)
  {
    global $nectar_theme_skin;
    $classes[] = $nectar_theme_skin;
    return $classes;
  }


  function nectar_theme_skin_css()
  {
    global $nectar_theme_skin;
    wp_enqueue_style('skin-' . $nectar_theme_skin);
  }

  add_action('wp_enqueue_scripts', 'nectar_theme_skin_css');



  /**
   * Search related.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/search.php';


  /**
   * Register Widget areas.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/widget-related.php';


  /**
   * Header navigation helpers.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/header.php';


  /**
   * Blog helpers.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/blog.php';


  /**
   * Page helpers.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/page.php';
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/footer.php';

  /**
   * Theme options panel (Redux).
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/redux-salient.php';


  /**
   * WordPress block editor helpers (Gutenberg).
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/gutenberg.php';


  /**
   * Admin assets.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/admin-enqueue.php';


  /**
   * Pagination Helpers.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/pagination.php';


  /**
   * Page header.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/page-header.php';


  /**
   * Third party.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/wpml.php';
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/woocommerce.php';


  /**
   * v10.5 update assist.
   */
  require_once NECTAR_THEME_DIRECTORY . '/nectar/helpers/update-assist.php';
}
/**
 * Custom logout function.
 *
 * @param string $redirect_url URL to redirect to after logout.
 */
function custom_logout($redirect_url = '')
{
  // Log the user out
  session_start();
  session_unset();
  session_destroy();
  wp_logout();

  // Redirect to the specified URL or the home URL if not provided
  $redirect_url = !empty($redirect_url) ? esc_url($redirect_url) : home_url('/');
  wp_safe_redirect($redirect_url);
  exit;
}

// Hook into the WordPress init action to check for custom logout request
add_action('init', 'check_custom_logout');

/**
 * Check for custom logout request.
 */
function check_custom_logout()
{
  // Check if the custom logout action is triggered
  if (isset($_GET['action']) && $_GET['action'] === 'custom_logout') {

    // Call the custom_logout function with a redirect URL if needed
    $redirect_url = isset($_GET['redirect']) ? $_GET['redirect'] : '';
    custom_logout($redirect_url);
  }
}

function custom_menu()
{
  $menu = '<ul id="primary-menu">';
  // echo $_SESSION['uid'];
  if (isset($_SESSION['uid'])) {
    // User is logged in, show "My Account" link
    $menu .= '<li><a href="' . home_url('/my-account/') . '">My Account</a></li>';
  } else {
    // User is not logged in, show "Login" link
    $menu .= '<li><a href="' . wp_login_url() . '">Login</a></li>';
  }

  // Add other menu items as needed
  $menu .= '<li><a href="' . home_url('/') . '">Home</a></li>';
  $menu .= '<li><a href="' . home_url('/about/') . '">About</a></li>';

  $menu .= '</ul>';

  echo $menu;
}

// Register the menu
add_action('init', 'register_custom_menu');

function register_custom_menu()
{
  register_nav_menu('primary-menu', __('Primary Menu'));
}

// add_action('wp_sync_product_cron', 'sync_product_cron');
// function sync_product_cron()
// {

//   $curl = curl_init();

//   curl_setopt_array($curl, array(
//     CURLOPT_URL => 'https://shootinschool.spericorn.com/wp-json/wc/v3/products',
//     CURLOPT_RETURNTRANSFER => true,
//     CURLOPT_ENCODING => '',
//     CURLOPT_MAXREDIRS => 10,
//     CURLOPT_TIMEOUT => 0,
//     CURLOPT_FOLLOWLOCATION => true,
//     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//     CURLOPT_CUSTOMREQUEST => 'GET',
//     CURLOPT_HTTPHEADER => array(
//       'Authorization: Basic Y2tfNzQ0ZjQ4MTQ2Yzc3Y2FhMmUxNGM4NDQzOGQ1NTEwNjcxYTZiOTQzZDpjc19hOGY3MmI4MjliZWRkZmVlYThhOTgzOGUwOWMxMDExNGJhNDBmZjhm'
//     ),
//   ));

//   $response = curl_exec($curl);

//   curl_close($curl);
//   $response =  json_decode($response);
//   if (!empty($response)) {
//     $desiredCategory = [];
//     foreach ($response as $product) {
//       // echo "<pre>";
//       // print_r($product->categories);


//       foreach ($product->categories as $category) {
//         if ($category->name === 'BWC') {
//           array_push($desiredCategory, $product->id);
//           break;
//         }
//       }
//     }


//     // Your desired category IDs

//     // Prepare an empty array to store IDs not found in the table
//     $notFoundIds = array();

//     // Prepare the SQL query
//     global $wpdb;
//     $tableName = $wpdb->prefix . 'ferdaratedData'; // Assuming 'wp_4s71kwx95x_' is your WordPress table prefix
//     $query = $wpdb->prepare("SELECT shootpid FROM $tableName WHERE shootpid IN (%s)", implode(',', $desiredCategory));

//     // Execute the query
//     $results = $wpdb->get_results($query);

//     // Check if any rows are returned
//     if ($results) {
//       // Iterate through the rows
//       foreach ($results as $result) {
//         // Remove found ID from the desired category array
//         $key = array_search($result->shootpid, $desiredCategory);
//         if ($key !== false) {
//           unset($desiredCategory[$key]);
//         }
//       }
//       // IDs not found in the table
//       $notFoundIds = $desiredCategory;
//     } else {
//       // If no rows are returned, all IDs are not found in the table
//       $notFoundIds = $desiredCategory;
//     }

//     // Output the result
//     // echo "IDs found in the table: " . implode(',', array_diff($desiredCategory, $notFoundIds)) . "<br>";
//     // echo "IDs not found in the table: " . implode(',', $notFoundIds);
//     if (!empty($notFoundIds)) {
//       foreach ($notFoundIds as $id) {
//         $curl = curl_init();

//         curl_setopt_array($curl, array(
//           CURLOPT_URL => "https://shootinschool.spericorn.com/wp-json/wc/v3/products/$id",
//           CURLOPT_RETURNTRANSFER => true,
//           CURLOPT_ENCODING => '',
//           CURLOPT_MAXREDIRS => 10,
//           CURLOPT_TIMEOUT => 0,
//           CURLOPT_FOLLOWLOCATION => true,
//           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//           CURLOPT_CUSTOMREQUEST => 'GET',
//           CURLOPT_HTTPHEADER => array(
//             'Authorization: Basic Y2tfNzQ0ZjQ4MTQ2Yzc3Y2FhMmUxNGM4NDQzOGQ1NTEwNjcxYTZiOTQzZDpjc19hOGY3MmI4MjliZWRkZmVlYThhOTgzOGUwOWMxMDExNGJhNDBmZjhm'
//           ),
//         ));

//         $response = curl_exec($curl);

//         curl_close($curl);
//         // echo $response;
//         // echo "<pre>";  print_r($response);
//         $data = json_decode($response, true);
//         // echo "<pre>";  print_r($data['images'][0]['id']);
//         // die();
//         // Unset the 'id' key if it exists
//         $pid = $data['id'];
//         unset($data['id']);
//         unset($data['images'][0]['id']);


//         // Iterate through the meta_data array to find the _gravity_form_data and change its id
//         foreach ($data['meta_data'] as &$meta) {
//           if ($meta['key'] == '_gravity_form_data') {
//             if ($meta['value']['id'] == 13) {
//               $meta['value']['id'] = "5"; // Change the ID to 5
//               break; // Exit the loop once we've made our change
//             }
//           }
//         }


//         // Encode back to JSON
//         $newJsonResponse = json_encode($data);

//         // Output the modified JSON
//         // echo $newJsonResponse;

//         $curl = curl_init();

//         curl_setopt_array($curl, array(
//           CURLOPT_URL => 'https://bwc.spericorn.com/wp-json/wc/v3/products',
//           CURLOPT_RETURNTRANSFER => true,
//           CURLOPT_ENCODING => '',
//           CURLOPT_MAXREDIRS => 10,
//           CURLOPT_TIMEOUT => 0,
//           CURLOPT_FOLLOWLOCATION => true,
//           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//           CURLOPT_CUSTOMREQUEST => 'POST',
//           CURLOPT_POSTFIELDS => $newJsonResponse,
//           CURLOPT_HTTPHEADER => array(
//             'Content-Type: application/json',
//             'Authorization: Basic Y2tfZTM2ZGY4ODFhZTg1MTcwYWFhMDUyN2JlY2RkMzc0NjE0ODZlYWY2Mzpjc181MWE2MjYxMThkYWJlMjhjOTRlYWI4YTZlNTU2NDZmNTYzY2FjMTE3'
//           ),
//         ));

//         $response = curl_exec($curl);

//         curl_close($curl);
//         $Newdata = json_decode($response, true);
//         // echo $response;
//         global $wpdb;
//         $tableName = $wpdb->prefix . 'ferdaratedData'; // Assuming 'wp_4s71kwx95x_' is your WordPress table prefix
//         $wpdb->insert(
//           $tableName,
//           array(
//             'shootpid' => $id,
//             'bwcpid' => $Newdata['id'],
//           )
//         );
//       }
//     }
//   }
// }


add_action('woocommerce_payment_complete', 'my_custom_webhook_after_checkout');

function my_custom_webhook_after_checkout($order_id)
{
  $order = wc_get_order($order_id);
  if (in_array($order->get_status(), array('completed', 'processing'))) {
    session_start();
    $uid = $_SESSION['uid'];
    $curl = curl_init();

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://bwcsports.com/wp-json/wc/v3/orders/$order_id",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'GET',
      CURLOPT_HTTPHEADER => array(
        'Authorization: Basic Y2tfMjRkM2Q5YjBjNTNiMDY3MzQ2YTg4NTRiNjU3NGY3MjRjNDQ3NWQ0MDpjc19hNTY1ZmJmNTdjZjhlYjNjYzIwOTViYjdkNGMyNzE4MGE1MzNiNTIw'
      ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);

    // $response = json_encode($response);
    // $json = preg_replace('/[[:^print:]]/', '', $response);
    error_log("****before decode***:".$uid);
    $response = json_decode($response);
    // error_log("****decode***:".$response);
    if ($response !== null) {
      $response->customer_id = $uid;
      // $response->id = $new_id; #based on the data to be set
      unset($response->id); #based on the data to be set
      $all_line_items = $response->line_items;
      $response->line_items = [];
      // unset($response->line_items);
      foreach ($all_line_items as $key => $line_items) {
        unset($line_items->id);
        unset($line_items->image);
        unset($line_items->parent_name);
        if ($line_items->product_id == 1723) {
          $line_items->product_id = 9939;
        }elseif ($line_items->product_id == 1724) {
          $line_items->product_id = 9940;
        }elseif ($line_items->product_id == 1725) {
          $line_items->product_id = 9941;
        }elseif ($line_items->product_id == 1726) {
          $line_items->product_id = 9942;
        }elseif ($line_items->product_id == 1727) {
          $line_items->product_id = 9943;
        }elseif ($line_items->product_id == 1728) {
          $line_items->product_id = 9944;
        }
        foreach ($line_items->meta_data as $linekey => $metadata) {
          unset($metadata->display_value);
          unset($metadata->value->_gravity_form_linked_entry_id);
          if ($line_items->product_id == 9939) {
            $metadata->value->_gravity_form_lead->form_id = 64;
          }elseif ($line_items->product_id == 9940) {
            $metadata->value->_gravity_form_lead->form_id = 65;
          }elseif ($line_items->product_id == 9941) {
            $metadata->value->_gravity_form_lead->form_id = 66;
          }elseif ($line_items->product_id == 9942) {
            $metadata->value->_gravity_form_lead->form_id = 67;
          }elseif ($line_items->product_id == 9943) {
            $metadata->value->_gravity_form_lead->form_id = 68;
          }elseif ($line_items->product_id == 9944) {
            $metadata->value->_gravity_form_lead->form_id = 69;
          }
          $line_items->meta_data[$linekey] = $metadata;
        }


        $response->line_items[$key] = $line_items;
      }

      $all_fee_lines = $response->fee_lines;
      $response->fee_lines = [];
      // unset($response->line_items);
      foreach ($all_fee_lines as $key => $fee_lines) {
        unset($fee_lines->id);
        $response->fee_lines[$key] = $fee_lines;
      }



      // error_log("***wwwwwwww****:".json_encode($response));


      $client = new Client();
      $headers = [
        'Content-Type' => 'application/json',
        'Accept' => '*/*',
        'Authorization' => 'Basic Y2tfMTFlOTgzMjdiMjg0YTFhNThkNWZiNmRkNTcwZTBiNjczZDJmOGUzNTpjc183NmVlNmIxNDA4NGU4YzI1YWY5MzkyNjk0OTUwNTRjN2NhNGVjNjc2',
        'Cookie' => 'PHPSESSID=q4g1t78eti2k2j9oipqbh3rpip'
      ];
      $body = json_encode($response);

      $url = 'https://shootinschool.com/wp-json/wc/v3/orders';
      $client->postAsync($url, [
        'headers' => $headers,
        'body' => $body,
      ])->then(
        function ($response) {
          global $wpdb;
          $apiurl =  "https://shootinschool.com";
          $data = json_decode($response->getBody(), true); // Decode response as associative array

          // Check if the response contains the expected data
          if (isset($data['id'])) {
            $orderID = $data['id'];
            $table_name = "wp_4s71kwx95x_ferdaratedData";

            $dataToInsert = array(
              'bwcpid' => 1,
              'shootpid' => 2,
              'bwcgfid' => 3,
              'shootgfid' => $orderID,
              'test' => 'orderID'
            );

            // // Insert data into database
            // $wpdb->insert($table_name, $dataToInsert);
            $curl = curl_init();

            curl_setopt_array($curl, array(
              CURLOPT_URL => "$apiurl/wp-json/bwc-api/v1/BwcOrderCreate?order_id=$orderID",
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'GET',

            ));

            $response = curl_exec($curl);

            curl_close($curl);
          }
        },
        function (Exception $e) {
          // Error callback
          echo 'Error: ' . $e->getMessage();
        }
      )->wait(); // Wait for the promise to resolve



      // You can access other fields similarly
    } else {
      echo "Failed to decode JSON\n";
    }
  }
}


// add_action('woocommerce_new_order', 'bwc_new_order');
// function bwc_new_order($order_id)
// {


//   $the_order = wc_get_order($order_id);
//   echo "<pre>";
//   print_r($the_order);
//   die();
//   global $wpdb;

//   $table_name = $wpdb->prefix . 'ferdaratedData';

//   // Prepare the data to be inserted
//   $data = array(
//     'bwcpid' => 1,
//     'shootpid' => 2,
//     'bwcgfid' => 3,
//     'shootgfid' => 5,
//     'test' => 'before order'
//   );

//   // Insert the data into the custom table
//   $wpdb->insert($table_name, $data);
// }

// add_filter('woocommerce_checkout_create_order', 'insert_custom_data_before_order_creation');

// function insert_custom_data_before_order_creation($order) {
//     global $wpdb;

//     $table_name = $wpdb->prefix . 'ferdaratedData';

//     // Prepare the data to be inserted
//     $data = array(
//         'bwcpid' => 1,
//         'shootpid' =>2,
//         'bwcgfid' => 3,
//         'shootgfid' => 5,
//         'test' => 'before order'
//     );

//     // Insert the data into the custom table
//     $wpdb->insert($table_name, $data);
// }



add_action('woocommerce_before_checkout_form', 'custom_intermediate_page');

function custom_intermediate_page()
{
  // Check if it's the checkout page
  if (is_checkout()) {
   // session_start();
    if (!isset($_SESSION['uid'])) {
      wp_redirect(get_site_url() . "/login");
    }
    // wp_redirect('https://bwc.spericorn.com/login/'); // Change 'your-custom-page' to the slug of your custom page
    // exit;
  }
}


add_filter('woocommerce_cart_needs_payment', '__return_true');

// Define a function to apply discount
function apply_custom_discount() {
  session_start();
  
  $apiurl =  "https://shootinschool.com";
  $uid = $_SESSION['uid'];
  $curl = curl_init();

            curl_setopt_array($curl, array(
              CURLOPT_URL => "$apiurl/wp-json/bwc-api/v1/BwcCheckSelfDirection?customer_id=$uid",
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'GET',

            ));

            $response = curl_exec($curl);

            curl_close($curl);
  if($response == "1"){
    global $woocommerce;

    // Get the cart subtotal
    $subtotal = $woocommerce->cart->subtotal;

    // Calculate discount percentage (for example, 10%)
    $discount_percentage = 100; // Change this to your desired percentage

    // Calculate discount amount
    $discount_amount = $subtotal * ($discount_percentage / 100);

    // Apply discount
    $woocommerce->cart->add_fee( 'Discount', -$discount_amount );
  }
   
}

// Hook the function to apply the discount
add_action( 'woocommerce_cart_calculate_fees', 'apply_custom_discount' );


function add_custom_text_before_payment() {
  // 	session_start();
    
  $apiurl =  "https://shootinschool.com";
  $uid = $_SESSION['uid'];
  $curl = curl_init();

            curl_setopt_array($curl, array(
              CURLOPT_URL => "$apiurl/wp-json/bwc-api/v1/BwcCheckSelfDirection?customer_id=$uid",
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_ENCODING => '',
              CURLOPT_MAXREDIRS => 10,
              CURLOPT_TIMEOUT => 0,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
              CURLOPT_CUSTOMREQUEST => 'GET',

            ));

            $response = curl_exec($curl);

            curl_close($curl);
  if($response == "1"){
      echo '<p style="font-size:18px;color:#e74c3c;margin-bottom: 0px;margin-top: 41px;">If you have self direction, we need your card on file only for <br>no-shows or cancellations.</p>';
    }
  }
  add_action('woocommerce_review_order_before_payment', 'add_custom_text_before_payment');

  ?>
  <script>
  document.addEventListener("DOMContentLoaded", function() {
      // The target URL
      var targetUrl = "https://bwcsports.com/login/";
  
      // Get the current URL
      var currentUrl = window.location.href;
  
      // Check if the current URL matches the target URL
      if (currentUrl === targetUrl) {
         window.location.hash = 'login';
      } else {
          console.log("The URL does not match.");
      }
  });
  </script>