/*
Theme Name: Glozin Child
Theme URI: https://wpglozin.com/
Author: UIXThemes
Author URI: https://uix.store
Description: Multipurpose WooCommerce WordPress Theme
Version: 1.0.0
Requires at least: 5.0
Tested up to: 5.8
Requires PHP: 7.0
License: GNU General Public License v2+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: glozin
Tags: one-column, two-columns, left-sidebar, right-sidebar, full-width-template, post-formats, theme-options, threaded-comments, translation-ready
Template: glozin
*/

<?php
/**
 * WhatsApp "Order via WhatsApp" button on the Cart and Checkout pages.
 *
 * HOW TO USE:
 * 1. Change the phone number in $whatsapp_number below.
 *    Use international format, digits only — no "+", spaces, or dashes.
 *    Example: a UK number +44 7123 456789  ->  447123456789
 *             a Lebanese number +961 71 123 456 -> 96171123456
 * 2. Paste this whole block at the END of your theme's functions.php
 *    (preferably a CHILD theme, so a theme update doesn't erase it).
 */

if ( ! function_exists( 'mywa_get_number' ) ) {

	/**
	 * Your WhatsApp business number (international format, digits only).
	 */
	function mywa_get_number() {
		return '96171131079'; // <-- CHANGE THIS
	}

	/**
	 * Build the pre-filled WhatsApp message from the current cart.
	 *
	 * @return string Ready-to-use wa.me URL.
	 */
	function mywa_build_url() {

		$lines   = array();
		$lines[] = 'Hello, I would like to order:';
		$lines[] = '';

		// Loop through every item in the cart.
		if ( WC()->cart && ! WC()->cart->is_empty() ) {
			foreach ( WC()->cart->get_cart() as $item ) {
				$product = $item['data'];
				$name    = $product ? $product->get_name() : __( 'Item', 'woocommerce' );
				$qty     = $item['quantity'];
				$price   = wc_price( $item['line_total'] );

				// Strip HTML out of the price so it reads cleanly in WhatsApp.
				$price = wp_strip_all_tags( $price );

				$lines[] = sprintf( '- %s x%d  (%s)', $name, $qty, $price );
			}

			// Cart total.
			$lines[] = '';
			$lines[] = 'Total: ' . wp_strip_all_tags( WC()->cart->get_total() );
		}

		$message = implode( "\n", $lines );

		return 'https://wa.me/' . mywa_get_number() . '?text=' . rawurlencode( $message );
	}

	/**
	 * Output the styled button.
	 */
	function mywa_render_button() {

		// Don't show an empty-cart button.
		if ( ! WC()->cart || WC()->cart->is_empty() ) {
			return;
		}

		$url = esc_url( mywa_build_url() );
		?>
		<a href="<?php echo $url; ?>"
		   class="button mywa-button"
		   target="_blank"
		   rel="noopener noreferrer">
			<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="20" height="20" fill="currentColor" style="vertical-align:middle;margin-right:8px;">
				<path d="M.057 24l1.687-6.163a11.867 11.867 0 0 1-1.587-5.945C.16 5.335 5.495 0 12.05 0a11.82 11.82 0 0 1 8.413 3.488 11.82 11.82 0 0 1 3.48 8.414c-.003 6.557-5.338 11.892-11.893 11.892a11.9 11.9 0 0 1-5.688-1.448L.057 24zm6.597-3.807c1.676.995 3.276 1.591 5.392 1.592 5.448 0 9.886-4.434 9.889-9.885.002-5.462-4.415-9.89-9.881-9.892-5.452 0-9.887 4.434-9.889 9.884a9.86 9.86 0 0 0 1.51 5.26l-.999 3.648 3.978-1.607zm11.387-5.464c-.074-.124-.272-.198-.57-.347-.297-.149-1.758-.868-2.031-.967-.272-.099-.47-.149-.669.149-.198.297-.768.967-.941 1.165-.173.198-.347.223-.644.074-.297-.149-1.255-.462-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.297-.347.446-.521.151-.172.2-.296.3-.495.099-.198.05-.372-.025-.521-.075-.148-.669-1.611-.916-2.206-.242-.579-.487-.501-.669-.51l-.57-.01c-.198 0-.52.074-.792.372s-1.04 1.016-1.04 2.479 1.065 2.876 1.213 3.074c.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.626.712.226 1.36.194 1.872.118.571-.085 1.758-.719 2.006-1.413.248-.695.248-1.29.173-1.414z"/>
			</svg>
			<?php esc_html_e( 'Order via WhatsApp', 'woocommerce' ); ?>
		</a>
		<?php
	}
}

/* -------------------------------------------------------------------------
 *  CART PAGE  — button appears under the cart totals.
 * ---------------------------------------------------------------------- */
add_action( 'woocommerce_after_cart_totals', 'mywa_render_button', 20 );

/* -------------------------------------------------------------------------
 *  CHECKOUT PAGE — button appears under the "Place order" button.
 * ---------------------------------------------------------------------- */
add_action( 'woocommerce_review_order_after_submit', 'mywa_render_button', 20 );

/* -------------------------------------------------------------------------
 *  A little styling so the button is green and full-width.
 *  Remove this block if your theme already styles .button nicely.
 * ---------------------------------------------------------------------- */
add_action( 'wp_head', 'mywa_button_styles' );
function mywa_button_styles() {
	if ( ! function_exists( 'is_cart' ) ) {
		return;
	}
	if ( ! is_cart() && ! is_checkout() ) {
		return;
	}
	?>
	<style>
		.mywa-button {
			display: inline-flex !important;
			align-items: center;
			justify-content: center;
			background-color: #25D366 !important;
			color: #fff !important;
			width: 100%;
			margin-top: 12px;
			padding: 12px 16px;
			border-radius: 6px;
			font-weight: 600;
			text-align: center;
			text-decoration: none;
		}
		.mywa-button:hover {
			background-color: #1da851 !important;
			color: #fff !important;
		}
	</style>
	<?php
}