/**
 * Template:       metro
 * Theme Name:     Metro Child
 * Theme URI:      https://radiustheme.com/demo/wordpress/themes/metro
 * Author:         RadiusTheme
 * Author URI:     https://www.radiustheme.com
 * Description:    Metro is a clean and modern WordPress Theme for Ecommerce and Online Store. This theme comes with all necessary features and pages for a Ecommerce website.
 * Version:        1.0
 * Tags:           two-columns, three-columns, left-sidebar, right-sidebar, custom-colors, custom-menu, featured-images, footer-widgets, full-width-template, sticky-post, theme-options, threaded-comments, translation-ready, blog
 * License:        Envato split License
 * License URI:    https://themeforest.net/licenses/terms/regular
**/

/**
 * set code for 
**/
:root{
	--fluidcheckout--checkout-progress--bar-color--complete: #003478;
	--fluidcheckout--color--success: #003478;
	--e-global-color-primary: #003478;
    --e-global-color-secondary: #00A0DE;
    --e-global-color-text: #000000;
    --e-global-color-accent: #FFFFFF;
}


/**
 * reset CSS
**/
body #respond form input, body #respond form textarea, body #respond form select{border-radius: 0; }

/**
 * Pre Header 
**/
.top-header .top-header-inner {padding: 0; color: #fff; font-weight: 400; font-size: 13px; }
.pre-h-menu {list-style: none; padding: 0; margin: 0; }
.pre-h-link {padding: 5px; padding-right: 10px; display: inline-block; position: relative; font-size: 14px; }
.top-header a {color: white !important; }
.top-header .hilighted a {color: var(--e-global-color-primary) !important; }
.top-header a:hover {color: #ddd !important; }
.top-header .hilighted {border: 0px solid #fff; border-radius: 5px; padding: 0px 8px; background: #fff; line-height: 1.6; }

/**
 * Footer css
**/
.footer-bottom-area.footer-bottom-area-new-2{padding: 0 0 12px;}
.footer-bottom-area .footer-bottom-inner .copyright-text{font-size: 14px;}

/**
 * banner update for all pages
**/
.banner .banner-content {padding: 30px 0 20px; text-align: left; }
.banner .banner-content h1, .banner .banner-content h2{margin-bottom: 10px; }
/**
 * Shop Attribute table in Product page
**/
.single-product.woocommerce table.shop_attributes th {width: 200px; font-size: 14px; line-height: 20px; }
.single-product.woocommerce table.shop_attributes td, .single-product.woocommerce table.shop_attributes th {border: 1px solid rgba(0, 0, 0, 0.1); padding: 6px 14px; font-style: normal; }
.single-product.woocommerce table.shop_attributes {width: auto; min-width: 100%; }

/**
 * for Coupon display in Product page
**/
.product-offers li .offer-box .off {padding: 2px 8px; background: var(--e-global-color-primary); color: white; margin-right: 6px; }
ul.product-offers {list-style: none; padding: 0; margin-left: 0px; margin-bottom: 0; }
.product-offers li {font-size: 14px; }
.product-offers li .offer-box {border: 1px dashed var(--e-global-color-primary); padding: 0; padding-right: 8px; display: inline-flex; align-items: center; }
.product-offers li .copy-btn {cursor: pointer; padding: 8px; margin-left: 6px; background: #efefef; color: #000; display: initial; }
.product-offers li .coupon-msg {color: #2c4bda; }
.product-offers li b.coupon-code {}

/**
 * Hide Meta Area in Product page
**/
.product_meta-area:has(.product-meta-group){display: none; }
.product_meta-area:not(:has(.product-meta-group)){padding-top: 30px; border-top: 1px solid #ececec; }


/**
 * updated styling for woo variation Table
**/
.woocommerce div.product #woo-variations-table-component span.price{font-size: 16px !important; }
.woocommerce div.product  #woo-variations-table-component span.price del {display:none; }
.woocommerce #woo-variations-table-component .quantity .qty {height: 33px!important; }
#variations-table .available-title{display: none; }
#woo-variations-table-component #variations table.variations thead th, #woo-variations-table-component table caption.header-caption{text-align: center; vertical-align: bottom; border-bottom: 2px solid #dee2e6; font-weight: 700; padding: 6px 0; }
#woo-variations-table-component table caption{border-color: #eee !important; font-size: 24px; font-weight: 700; }
.woocommerce #woo-variations-table-component table button.button, #woo-variations-table-component table caption.footer-caption{font-size: 14px; font-weight: normal; }
.woocommerce #woo-variations-table-component table caption.footer-caption .button{margin-left: 10px; }
#woo-variations-table-component #variations table.variations td{font-size: 16px !important; padding: 2px 10px; text-align: center; }

/**
 * Styling for Before Woo table list icons
**/
.dev-why-us ul {list-style: none; padding: 0; display: flex; flex-wrap: wrap; column-gap: 25px; justify-content: center; width: 100%; margin: 26px 0; }
.dev-why-us ul li {font-size: 19px; }
/**
 * Styling for after Woo table review width 
**/
.rdtheme-wc-reviews {padding: 40px min(10%, 100px); }
/**
 * Hide Wishlist Button from list vieww
**/
.product a:is(.rtsb-wishlist-btn,.rtsb-compare-btn,.rtsb-quick-view-btn), .rtsb-actions-button a:is(.rtsb-wishlist-btn,.rtsb-compare-btn,.rtsb-quick-view-btn) .icon {display: none !important; }
/**
 * styling for external checkout on cart page
**/
.elementor-widget-woocommerce-cart .woocommerce .wc-proceed-to-checkout {display: flex !important; flex-direction: row !important; }
.elementor-widget-woocommerce-cart .woocommerce a.whatsapp:not(.add_to_cart_button):not(.restore-item):not(.wc-backward):not(.wc-forward) {color: #43b13e; }
.elementor-widget-woocommerce-cart .woocommerce a.mail:not(.add_to_cart_button):not(.restore-item):not(.wc-backward):not(.wc-forward) {color: #dc3545; }
/*.dev-check-out-with {margin: auto; margin-right: 0px; }*/
.btn-checkout-with {border: 1px solid; color: #7a4600; height: 42px; padding: 5px 5px; display: inline-flex; align-items: center; border-radius: 99px	; margin: 12px auto 0; }
.btn-checkout-with.whatsapp {padding-right: 16px; border: 1px solid #43b13e; }
.btn-checkout-with img {margin-right: 10px; }
.btn-checkout-with.mail {padding: 5px 10px; padding-right: 16px; border: 1px solid #dc3545; }

/**
 * styling for checkout page
**/
.woocommerce form .form-row .select2-container .select2-selection .select2-selection__arrow {top:0 !important; }
.woocommerce form .form-row .select2-container .select2-selection .select2-selection__rendered{padding: 0 20px 0; line-height: calc(var(--fluidcheckout--field--height, 45.625px) - var(--fluidcheckout--field--border-width, 1px) * 2); }
.has-checkout-layout--multi-step.woocommerce-checkout form.woocommerce-checkout .fc-wrapper [data-step-complete] .fc-step__substep .fc-step__substep-title:before{background-color: var(--e-global-color-primary); }
.fc-wrapper #payment ul.payment_methods>li.wc_payment_method .payment_box {border-radius: 0 0 13px 13px; }
div.woocommerce .fc-wrapper .fc-checkout-order-review .fc-checkout-order-review__inner {padding-bottom: 12px; }
body div.woocommerce .fc-wrapper .fc-coupon_code__collapsible .fc-coupon-code__apply{right: 15px; }
body.woocommerce-checkout .fc-content .woocommerce button.button /*.fc-step__next-step, .woocommerce-checkout .fc-content .fc-step__substep-save*/{padding: 12px 40px; width: var(--checkout-button-width, auto); font-size: 15px; color: var(--checkout-button-normal-text-color, #fff); border-radius: var(--checkout-button-border-radius, 3px); min-height: auto; margin-bottom: 0; line-height: 1.3; display: block; text-transform: uppercase; font-weight: 400; border-style: 1px solid var(--e-global-color-primary); }
div.woocommerce form .collapsible-content__inner > * {padding-right: 10px; }

/**
 *
**/
.woocommerce ul.order_details{background-color: aliceblue;padding: 20px;}
.woocommerce ul.order_details li strong{font-size: 1.3em;font-weight: 600;}
.woocommerce-order-received .woocommerce h2{font-size: 26px;font-weight: 700;}
.woocommerce-product-attributes-item--attribute_pa_pack-size{display:none;}

/**
 * removed star from widget
**/
.widget.widget_products .rtsb-star-rating{display:none}



@media (max-width:1024px){
	/**
	 * Logo size setoff in header
	**/
	.mean-container .mean-bar img {width: 115px; }
    #site-header {display: block; }
    #site-header .menu-top-bar-menu-container, #site-header .tophead-right, #site-header .main-header,#site-header .header-search-area{display: none; }
    #site-header .top-header-inner{justify-content: space-around; }
}



@media screen and (max-width: 767px) { 
   #variations table.variations caption.header-caption h2{font-size: 15px; padding: 4px 0px; }
   #woo-variations-table-component #variations table.variations thead th {font-size: 10px; }
   #woo-variations-table-component #variations table.variations td {font-size: 10px !important; line-height: 14px; }
   .woocommerce div.product #woo-variations-table-component span.price {font-size: 10px !important; }
   .woocommerce #woo-variations-table-component table button.button, #woo-variations-table-component table caption.footer-caption {font-size: 10px;}
   .woocommerce #woo-variations-table-component .quantity .qty {min-width:43px!important;width: 15px !important;}
   #woo-variations-table-component #variations table.variations tr td:last-child, #woo-variations-table-component #variations table.variations tr th:last-child{min-width:22px !important;width: 22px !important;}
   .footer-bottom-area .footer-bottom-inner .copyright-text{font-size: 12px;}
}



/* Payment Link */

/* Payment Link */
add_action('woocommerce_new_order', 'set_sequence_of_payment_link', 10, 1);

function set_sequence_of_payment_link( $order_id ) {
	$order = wc_get_order($order_id);
    $total = $order->get_total();
	
	// Query custom post type
	$args = array(
		'post_type' => 'paypal_links', // Replace 'your_custom_post_type' with your actual custom post type slug
		'posts_per_page' => -1, // Retrieve all posts
	);

	$query = new WP_Query( $args );

	// Check if there are posts
	if ( $query->have_posts() ) {
		// Initialize array to store custom field values
		$links = array();

		// Loop through posts
		while ( $query->have_posts() ) {
			$query->the_post();

			// Get custom field value (replace 'your_custom_field_key' with the key of your custom field)
			$paypal_link_url = get_post_meta( get_the_ID(), 'paypal_link_url', true );
			$paypal_link_text = get_post_meta( get_the_ID(), 'paypal_link_text', true );

			// Store custom field value in array
			$links[] = array(
				'link' => $paypal_link_url . $total,
				'label' => $paypal_link_text,
			);
		}

		// Restore original post data
		wp_reset_postdata();
	}


    // Check if a counter is set in the transients
    $current_order_number = get_option('current_order_number', '0');
    if ( isset($current_order_number) && $current_order_number >= count($links)) {
        $current_order_number = 0;
    }
    
    // Get the current link and its text
    $current_link_url = isset($current_order_number) ? $links[$current_order_number]['link'] : '';
    $current_link_text = isset($current_order_number) ? $links[$current_order_number]['label'] : '';

    update_post_meta($order_id, 'payment_link_url', $current_link_url);
    update_post_meta($order_id, 'payment_link_text', $current_link_text);

    $new_order_number = (int) $current_order_number + 1;
    update_option('current_order_number', $new_order_number);

}


/* Payment Link Mail*/
function custom_payment_links( $order, $sent_to_admin, $plain_text, $email ) {
	
	if ( 'customer_processing_order' != $email->id ) {
		return;
	}

	if ( !empty($order) && !empty($order->customer_ip_address) ) {
		$response = wp_remote_get('https://ipinfo.io/' . $order->customer_ip_address . '/json');
		if (!is_wp_error($response)) {
			$body = wp_remote_retrieve_body($response);
			$data = json_decode($body);
			if ($data && isset($data->country)) {
				if ( 'IN' == $data->country ) {
					do_shortcode('[content_india]');

                    $first_name = $order->get_billing_first_name();
				    echo "<p style='margin:0 0 16px'>Thank you for shopping with us!</p> <p style='margin:0 0 16px'>Your order has been successfully placed.</p> <p style='margin:0 0 16px'><strong>We will send you the PAYMENT <span class='il'>LINK</span> within 24 hours to your email. After your payment confirmation, Your order will be shipped within 24 hours and provide you the tracking number.</strong></p> <p style='margin:0 0 16px'>Stay Tuned with your Email...!</p> <p style='margin:0 0 16px'><b>Our Operation time:</b><br>9am to 9pm (Indian Time Only)</p>";

				} else {

                    $order_id = $order->get_id();

                    // Get the current link and its text
                    $current_link_url = get_post_meta($order_id, 'payment_link_url', true);
                    $current_link_text = get_post_meta($order_id, 'payment_link_text', true);

                    echo "<p><span style='font-size: 24px;padding-bottom: 20px;'><strong>Payment link: <a style='color: blue' href='{$current_link_url}'>{$current_link_text}</a></strong></span></p></br></br><div style='min-height:10px;text-align:left' align='left'><br></br><p style='margin:0px'><span style='color:#339966;font-size:18px'><strong>If you don't have a PayPal account, no worries! You can still make a secure payment with your credit card through PayPal. Follow these simple steps:</strong></span></p> <p style='margin:0px'>&nbsp;</p> <ol> <li><span style='font-size:16px'>Click on the payment link provided above.</span></li> <li><span style='font-size:16px'>You'll be directed to the PayPal payment page. Look for the option that says 'Pay with Debit or Credit Card.' It's usually located beneath the PayPal login form.</span></li> <li><span style='font-size:16px'>Click on 'Pay with Debit or Credit Card.'</span></li> <li><span style='font-size:16px'>Enter your credit card details, billing information, and contact details. PayPal ensures your information is securely processed.</span></li> <li><span style='font-size:16px'>Review the payment details to make sure everything is correct.</span></li> <li><span style='font-size:16px'>Click 'Pay Now' to complete your transaction.</span></li> </ol> <p style='margin:0px'><span style='font-size:16px'>Still confused follow this link: <span style='color:#000000'><strong><a href='https:///payment-process/' style='font-weight:normal;text-decoration:underline;color:#000000' target='_blank' data-saferedirecturl='https://www.google.com/url?q=https://knightpills.com/payment-process/&amp;source=gmail&amp;ust=1705648285628000&amp;usg=AOvVaw3JqVG-xURZ1695H3Ptt5dL'>https://knightpills.com/<wbr>payment-process/</a></strong></span></span></p> </div></br><div style='min-height:10px;text-align:left' align='left'> <h3 style='display:block;font-size:16px;font-weight:bold;line-height:130%;font-family:inherit;color:inherit;text-align:inherit;margin:0px'><span style='color:#008000;font-size:16px'><strong>Before you make Payment, we would like to clarify the following.</strong></span></h3> <ol> <li><span style='font-size:15px'>Please avoid mentioning any drug names during or after the checkout process, as PayPal prohibits drug transactions.</span></li> <li><span style='font-size:15px'>If you encounter any problems, please contact us via email <strong><a href='mailto:order@knightpills.com' style='color:#96588a;font-weight:normal;text-decoration:underline' target='_blank'>order@knightpills.com</a></strong> instead of opening a dispute with PayPal.</span></li> <li><span style='font-size:15px'>We promise to provide you with a satisfactory resolution or refund within 24 - 48 hours.</span></li> <li><span style='font-size:15px'>Once the payment is successfully processed, your order will be shipped, and you'll receive your tracking ID via email within 24-48 working hours. We appreciate your business and look forward to fulfilling your order. Thank you for choosing us!</span></li> </ol> </div>";
                }
            }
        }	
    }
}

add_action( 'woocommerce_email_before_order_table', 'custom_payment_links', 10, 4 );

/* Payment Link Thank You Page*/
function th_custom_payment_links( $order_id ) {
	
    $order = wc_get_order($order_id);
	$total = $order->get_total();
    
	if ( !empty($order) && !empty($order->customer_ip_address) ) {
		$response = wp_remote_get('https://ipinfo.io/' . $order->customer_ip_address . '/json');
		if (!is_wp_error($response)) {
			$body = wp_remote_retrieve_body($response);
			$data = json_decode($body);
			if ($data && isset($data->country)) {
				if ( 'IN' == $data->country ) {
					do_shortcode('[content_india]');

                    $first_name = $order->get_billing_first_name();
				    echo "<p style='margin:0 0 16px'>Thank you for shopping with us!</p> <p style='margin:0 0 16px'>Your order has been successfully placed.</p> <p style='margin:0 0 16px'><strong>We will send you the PAYMENT <span class='il'>LINK</span> within 24 hours to your email. After your payment confirmation, Your order will be shipped within 24 hours and provide you the tracking number.</strong></p> <p style='margin:0 0 16px'>Stay Tuned with your Email...!</p> <p style='margin:0 0 16px'><b>Our Operation time:</b><br>9am to 9pm (Indian Time Only)</p>";

				} else {

                    // Get the current link and its text
                    $current_link_url = get_post_meta($order_id, 'payment_link_url', true);
                    $current_link_text = get_post_meta($order_id, 'payment_link_text', true);

                    echo "<div class='alert-success'><b><p style='margin-bottom: 0.6em !important;'>Procedure for Payment</p></b><ul><li> Click on the <b>Pay <span class='woocommerce-Price-amount amount'><bdi><span class='woocommerce-Price-currencySymbol'>$</span>" . $total . "</bdi></span></b> button and wait a few seconds to open your order payment in a new tab.</li><li>Click on the <b>Pay <span class='woocommerce-Price-amount amount'><bdi><span class='woocommerce-Price-currencySymbol'>$</span>" . $total . "</bdi></span></b>button to proceed for payment. After clicking on the <b>Pay <span class='woocommerce-Price-amount amount'><bdi><span class='woocommerce-Price-currencySymbol'>$</span>" . $total . "</bdi></span> </b>button a new window will open.</li></ul><p style='text-align: center;'><a target='_blank' style='margin-bottom:10px;border-radius: 25px;' href='{$current_link_url}' class='button' rel='nofollow'> Pay Now $" . $total . "</a></p><div class='tybox'><b><p style='margin-bottom: 0.6em !important;'>Important Instructions : </p></b><ul><li>While doing payment do not mention anything related to order or medicine because paypal doesn't allow medicine related transactions. Leave it blank.</li><li>If you want to put any notes, you can directly mail us <b><i><a href='mailto:order@knightpills.com'>order@knightpills.com</a></i></b> regarding your order.</li><li>We will send you a confirmation mail within 3-4 hrs after you made a payment.</li><li>If PayPal asks you in the future, please inform them that the transaction was related to IT Services or Freelance Consulting Services please do not mention anything related to medicine.</li><li>If you have made the payment and mentioned anything related to order or medicine in payment notes (in paypal) then we will not accept your order and immediately we will refund your full amount and we will never accept any order from you in the future.</li></ul></div><div class='tybox'><b></b><p style='margin-bottom: 0.6em !important;'><b>If you want to do payment via credit/debit card then reply to order mail we will send an invoice to your registered email for credit/debit card payment.</b></p></div></div>";
                }
            }
        }	
    }
}

add_action('woocommerce_before_thankyou', 'th_custom_payment_links', 10, 1);

// Step 1: Register Custom Post Type
function custom_post_type() {
    $labels = array(
        'name'                => _x( 'Paypal Links', 'Post Type General Name', 'twentytwentyone' ),
        'singular_name'       => _x( 'Paypal Link', 'Post Type Singular Name', 'twentytwentyone' ),
        'menu_name'           => __( 'Paypal Links', 'twentytwentyone' ),
        'parent_item_colon'   => __( 'Links', 'twentytwentyone' ),
        'all_items'           => __( 'All Paypal Links', 'twentytwentyone' ),
        'view_item'           => __( 'View Link', 'twentytwentyone' ),
        'add_new_item'        => __( 'Add New Link', 'twentytwentyone' ),
        'add_new'             => __( 'Add New', 'twentytwentyone' ),
        'edit_item'           => __( 'Edit Paypal Link', 'twentytwentyone' ),
        'update_item'         => __( 'Update Paypal Link', 'twentytwentyone' ),
        'search_items'        => __( 'Search Paypal Link', 'twentytwentyone' ),
        'not_found'           => __( 'Not Found', 'twentytwentyone' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentytwentyone' ),
    );
    
// Set other options for Custom Post Type
    
    $args = array(
        'label'               => __( 'Paypal Links', 'twentytwentyone' ),
        'description'         => __( 'Paypal payment links', 'twentytwentyone' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'custom-fields', ),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => false,
        'capability_type'     => 'post',
        'show_in_rest' => true,
		);
    register_post_type('paypal_links', $args);
}
add_action('init', 'custom_post_type');

// Step 2: Add Custom Meta Box
function add_paypal_fields_meta_box() {
    add_meta_box(
        'paypal-fields-meta-box', // Meta box ID
        'Paypal Fields', // Meta box title
        'render_paypal_fields_meta_box', // Callback function to render the meta box contents
        'paypal_links', // Post type to display the meta box
        'normal', // Context: normal, advanced, or side
        'high' // Priority: high, core, default, or low
    );
}
add_action('add_meta_boxes', 'add_paypal_fields_meta_box');

// Step 3: Render Meta Box Contents
function render_paypal_fields_meta_box($post) {
	?>
	<style>
		.inside * {
			width: 100%;
			margin-bottom: 10px;
		}
	</style>
	<?php
    // Retrieve existing meta values if they exist
    $paypal_link_url = get_post_meta($post->ID, 'paypal_link_url', true);
	$paypal_link_text = get_post_meta($post->ID, 'paypal_link_text', true);

    wp_nonce_field( 'paypal_fields_meta_box_nonce', 'paypal_fields_meta_box_nonce' );

    ?>
    <label for="paypal_link_url">Paypal Link URL:</label>
    <input type="text" id="paypal_link_url" name="paypal_link_url" value="<?php echo esc_attr($paypal_link_url); ?>" >
	<label for="paypal_link_text">Paypal Link Text:</label>
    <input type="text" id="paypal_link_text" name="paypal_link_text" value="<?php echo esc_attr($paypal_link_text); ?>" style="margin: 0;">
    <?php
}

// Step 4: Save Custom Field Data
function save_custom_field_data($post_id) {
    // Check if this is an autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // Check if the nonce is set
    if (!isset($_POST['paypal_fields_meta_box_nonce']) || !wp_verify_nonce($_POST['paypal_fields_meta_box_nonce'], 'paypal_fields_meta_box_nonce')) {
        return;
    }
    // Check if the current user has permission to edit the post
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    // Save the meta data
    if (isset($_POST['paypal_link_url'])) {
        update_post_meta($post_id, 'paypal_link_url', sanitize_text_field($_POST['paypal_link_url']));
    }
	if (isset($_POST['paypal_link_text'])) {
        update_post_meta($post_id, 'paypal_link_text', sanitize_text_field($_POST['paypal_link_text']));
    }
}
add_action('save_post', 'save_custom_field_data');