/** * WooCommerce Customizations * Version: 1.2 (Mandatory WhatsApp Validation) */ if (!defined('ABSPATH')) { exit; } /** * Change the default "Sale!" text to "SALE" */ add_filter('woocommerce_sale_flash', 'tdb_custom_sale_flash'); function tdb_custom_sale_flash($html) { return 'SALE'; } /** * Handle custom redirect parameter for Buy Now functionality */ add_action('template_redirect', 'tdb_custom_add_to_cart_redirect'); function tdb_custom_add_to_cart_redirect() { if (!isset($_REQUEST['add-to-cart']) || !isset($_REQUEST['redirect'])) return; if ($_REQUEST['redirect'] !== 'checkout') return; if (!function_exists('wc_get_checkout_url') || !function_exists('WC')) return; wp_safe_redirect(wc_get_checkout_url()); exit; } /** * Remove default WC breadcrumbs (we use custom tdb_breadcrumbs in footer) */ remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20); /** * Related Products: Filter by Category & EXCLUDE Credits */ add_filter('woocommerce_related_products', 'tdb_filter_related_products_by_category', 10, 3); function tdb_filter_related_products_by_category($related_posts, $product_id, $args) { $terms = wp_get_post_terms($product_id, 'product_cat'); if (empty($terms)) return $related_posts; $term_ids = wp_list_pluck($terms, 'term_id'); $query_args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => 4, 'post__not_in' => array($product_id), 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $term_ids, 'operator' => 'IN' ), array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array('credits'), 'operator' => 'NOT IN' ) ), 'fields' => 'ids' ); return get_posts($query_args); } /** * Performance: Dequeue unnecessary WC assets on non-essential pages * (But keeping CSS for layout safety) */ add_action('wp_enqueue_scripts', 'tdb_dequeue_woocommerce_assets', 99); function tdb_dequeue_woocommerce_assets() { if (function_exists('is_woocommerce') && !is_woocommerce() && !is_cart() && !is_checkout() && !is_account_page()) { wp_dequeue_script('wc-add-to-cart'); wp_dequeue_script('wc-cart-fragments'); } } /** * Checkout & Profile Fields */ function tdb_custom_checkout_fields($fields) { if (isset($fields['billing']['billing_phone'])) { $fields['billing']['billing_phone']['required'] = true; $fields['billing']['billing_phone']['label'] = 'WhatsApp Number'; $fields['billing']['billing_phone']['priority'] = 120; } unset($fields['billing']['billing_company']); unset($fields['billing']['billing_address_1']); unset($fields['billing']['billing_address_2']); unset($fields['billing']['billing_city']); unset($fields['billing']['billing_state']); return $fields; } add_filter('woocommerce_checkout_fields', 'tdb_custom_checkout_fields'); /** * Checkout: Validate Phone Number (WhatsApp) */ function tdb_validate_checkout_phone() { $phone = isset($_POST['billing_phone']) ? sanitize_text_field($_POST['billing_phone']) : ''; // Remove spaces and dashes for checking $clean_phone = preg_replace('/[\s\-]/', '', $phone); // Check if empty if (empty($clean_phone)) { wc_add_notice(__('Please enter your WhatsApp number.', 'woocommerce'), 'error'); return; } // Check for International Prefix (+ or 00) if (strpos($clean_phone, '+') !== 0 && strpos($clean_phone, '00') !== 0) { wc_add_notice(__('Please enter your WhatsApp number with the country code (e.g., +44 7123...).', 'woocommerce'), 'error'); return; } // Check Length (Min 7 digits excluding prefix) $digits_only = preg_replace('/^(\+|00)/', '', $clean_phone); $digits_only = preg_replace('/[^0-9]/', '', $digits_only); if (strlen($digits_only) < 7 || strlen($digits_only) > 15) { wc_add_notice(__('Please enter a valid phone number length (7-15 digits).', 'woocommerce'), 'error'); } } add_action('woocommerce_checkout_process', 'tdb_validate_checkout_phone'); /** * My Account: Save WhatsApp Number */ function tdb_save_account_whatsapp_number( $user_id ) { if ( isset( $_POST['billing_phone'] ) ) { $phone = sanitize_text_field( $_POST['billing_phone'] ); update_user_meta( $user_id, 'billing_phone', $phone ); } } add_action( 'woocommerce_save_account_details', 'tdb_save_account_whatsapp_number', 10, 1 ); /** * My Account: Validate WhatsApp Number */ function tdb_validate_account_whatsapp_number_field( $errors ) { if ( isset( $_POST['billing_phone'] ) ) { $phone = preg_replace('/[^0-9]/', '', $_POST['billing_phone']); if ( strlen( $phone ) < 7 ) { $errors->add( 'billing_phone_error', __( 'Please enter a valid WhatsApp number (at least 7 digits).', 'woocommerce' ) ); } } } add_action( 'woocommerce_save_account_details_errors', 'tdb_validate_account_whatsapp_number_field', 10, 1 );