is_type( 'variable' ) ) { $prices = $product->get_variation_prices( true ); $min_price = current( $prices['price'] ); if ( 0 == $min_price ) { $max_price = end( $prices['price'] ); $min_reg_price = current( $prices['regular_price'] ); $max_reg_price = end( $prices['regular_price'] ); if ( $min_price !== $max_price ) { $price = wc_format_price_range( $cfp_html, $max_price ); $price .= $product->get_price_suffix(); } elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) { $price = wc_format_sale_price( wc_price( $max_reg_price ), $cfp_html ); $price .= $product->get_price_suffix(); } else { $price = $cfp_html; } } } elseif ( '' === $product->get_price() || 0 == $product->get_price() ) { $price = '' . $cfp_html . ''; } return $price; } // Frontend Checkout Fields Customizer $custom_fields_json = aio_get_option( 'optWcCheckoutFields' ); $has_custom_fields = ! empty( $custom_fields_json ) && '[]' !== $custom_fields_json; if ( $has_custom_fields ) { add_filter( 'woocommerce_checkout_fields', 'aio_tools_CustomDisplayWooCheckoutFields', 99999 ); add_filter( 'woocommerce_enable_order_notes_field', 'aio_tools_disable_order_notes_heading', 9999 ); function aio_tools_disable_order_notes_heading( $enable ) { $custom_fields_json = aio_get_option( 'optWcCheckoutFields' ); if ( ! empty( $custom_fields_json ) ) { $custom_fields = json_decode( $custom_fields_json, true ); if ( is_array( $custom_fields ) ) { foreach ( $custom_fields as $cf ) { if ( isset( $cf['id'] ) && 'order_comments' === $cf['id'] ) { if ( ! isset( $cf['visible'] ) || ! $cf['visible'] ) { return false; } } } } } return $enable; } } if ( 1 == aio_get_option( 'optWcMergeNames' ) ) { add_filter( 'woocommerce_checkout_fields', 'aio_tools_merge_first_last_name', 100000 ); } function aio_tools_merge_first_last_name( $fields ) { if ( isset( $fields['billing']['billing_first_name'] ) ) { $fields['billing']['billing_first_name']['label'] = __( 'Full Name', 'aio-tools' ); $fields['billing']['billing_first_name']['placeholder'] = __( 'Enter your full name', 'aio-tools' ); $fields['billing']['billing_first_name']['class'] = [ 'form-row-wide' ]; } if ( isset( $fields['billing']['billing_last_name'] ) ) { unset( $fields['billing']['billing_last_name'] ); } if ( isset( $fields['shipping']['shipping_first_name'] ) ) { $fields['shipping']['shipping_first_name']['label'] = __( 'Full Name', 'aio-tools' ); $fields['shipping']['shipping_first_name']['placeholder'] = __( 'Enter your full name', 'aio-tools' ); $fields['shipping']['shipping_first_name']['class'] = [ 'form-row-wide' ]; } if ( isset( $fields['shipping']['shipping_last_name'] ) ) { unset( $fields['shipping']['shipping_last_name'] ); } return $fields; } // Direct Checkout Modes if ( ! empty( aio_get_option( 'opt-woo-direct-checkout' ) ) || ! empty( aio_get_option( 'opt-direct-checkout' ) ) ) { $direct_checkout_mode = aio_get_option( 'optDirectCheckoutMode', '' ); if ( empty( $direct_checkout_mode ) ) { $direct_checkout_mode = 'redirect'; } if ( 'redirect' === $direct_checkout_mode ) { add_filter( 'woocommerce_add_to_cart_redirect', 'aio_tools_direct_checkout_redirect' ); } elseif ( 'lightbox' === $direct_checkout_mode ) { add_action( 'wp_footer', 'aio_tools_render_quick_buy_lightbox' ); add_action( 'wp_ajax_aio_get_quick_buy_nonce', 'aio_tools_get_quick_buy_nonce_ajax' ); add_action( 'wp_ajax_nopriv_aio_get_quick_buy_nonce', 'aio_tools_get_quick_buy_nonce_ajax' ); add_action( 'wp_ajax_aio_quick_buy_process', 'aio_tools_quick_buy_process' ); add_action( 'wp_ajax_nopriv_aio_quick_buy_process', 'aio_tools_quick_buy_process' ); } } function aio_tools_get_quick_buy_nonce_ajax() { nocache_headers(); wp_send_json_success( [ 'nonce' => wp_create_nonce( 'aio-quick-buy' ) ] ); } function aio_tools_render_quick_buy_lightbox() { if ( ! function_exists( 'is_woocommerce' ) ) { return; } $prefill_name = ''; $prefill_phone = ''; $prefill_addr = ''; if ( is_user_logged_in() ) { $uid = get_current_user_id(); $first = get_user_meta( $uid, 'billing_first_name', true ); $last = get_user_meta( $uid, 'billing_last_name', true ); $prefill_name = trim( $first . ' ' . $last ); if ( '' === $prefill_name ) { $u = wp_get_current_user(); $prefill_name = $u ? $u->display_name : ''; } $prefill_phone = (string) get_user_meta( $uid, 'billing_phone', true ); $prefill_addr = (string) get_user_meta( $uid, 'billing_address_1', true ); } ?>
' . esc_html( $cf['label'] ) . ': ' . esc_html( $value ) . '
'; } } } } add_action( 'woocommerce_checkout_create_order', 'aio_tools_save_custom_checkout_fields_to_order', 10, 2 ); function aio_tools_save_custom_checkout_fields_to_order( $order, $data ) { $custom_fields_json = aio_get_option( 'optWcCheckoutFields' ); if ( empty( $custom_fields_json ) ) { return; } $custom_fields = json_decode( $custom_fields_json, true ); if ( ! is_array( $custom_fields ) ) { return; } foreach ( $custom_fields as $cf ) { $id = isset( $cf['id'] ) ? $cf['id'] : ''; $is_custom = isset( $cf['is_custom'] ) ? $cf['is_custom'] : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing if ( $is_custom && ! empty( $id ) && isset( $_POST[ $id ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing $order->update_meta_data( '_' . $id, sanitize_text_field( wp_unslash( $_POST[ $id ] ) ) ); } } } /* ========================================================================== WCP-1. ASSET UNLOADER ========================================================================== */ if ( ! empty( aio_get_option( 'optEnableWcUnloader' ) ) ) { add_action( 'wp_enqueue_scripts', 'aio_tools_wc_asset_unloader', 99 ); } function aio_tools_wc_asset_unloader() { if ( function_exists( 'is_woocommerce' ) ) { if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() && ! is_account_page() ) { wp_dequeue_style( 'woocommerce-layout' ); wp_dequeue_style( 'woocommerce-smallscreen' ); wp_dequeue_style( 'woocommerce-general' ); wp_dequeue_style( 'wc-blocks-vendors-style' ); wp_dequeue_style( 'wc-blocks-style' ); wp_dequeue_script( 'wc-add-to-cart' ); wp_dequeue_script( 'woocommerce' ); if ( empty( aio_get_option( 'optWcSideCart' ) ) ) { wp_dequeue_script( 'wc-cart-fragments' ); } } } } /* ========================================================================== WCP-2. SOCIAL PROOF SALES POPUP (Real Recent Orders OR Virtual Mode) ========================================================================== */ if ( ! empty( aio_get_option( 'optEnableWcFakeSales' ) ) ) { add_action( 'wp_footer', 'aio_tools_render_fake_sales_popup' ); add_action( 'woocommerce_new_order', function () { delete_transient( 'aio_sales_popup_data_real' ); } ); } function aio_tools_render_fake_sales_popup() { if ( ! function_exists( 'wc_get_products' ) ) { return; } if ( function_exists( 'is_checkout' ) && is_checkout() ) { return; } $source = aio_get_option( 'optWcSalesSource', 'virtual' ); if ( 'real' === $source && function_exists( 'wc_get_orders' ) ) { $sales_data = get_transient( 'aio_sales_popup_data_real' ); if ( false === $sales_data ) { $sales_data = []; $orders = wc_get_orders( [ 'limit' => 15, 'status' => [ 'wc-processing', 'wc-completed' ], 'orderby' => 'date', 'order' => 'DESC', ] ); foreach ( $orders as $ord ) { $items = $ord->get_items(); if ( empty( $items ) ) { continue; } $first_item = reset( $items ); $prod = is_callable( [ $first_item, 'get_product' ] ) ? $first_item->get_product() : null; if ( ! $prod ) { continue; } $cust_name = trim( $ord->get_billing_first_name() ); $cust_city = trim( $ord->get_billing_city() ?: $ord->get_billing_state() ); $date_obj = $ord->get_date_created(); $time_label = $date_obj ? sprintf( /* translators: %s: human-readable time difference (e.g. "12 mins") */ __( '%s ago', 'aio-tools' ), human_time_diff( $date_obj->getTimestamp(), time() ) ) : __( 'Recently', 'aio-tools' ); $sales_data[] = [ 'headline' => $cust_city ? sprintf( /* translators: 1: customer first name, 2: city name */ __( '%1$s in %2$s purchased', 'aio-tools' ), $cust_name ?: __( 'A customer', 'aio-tools' ), $cust_city ) : sprintf( /* translators: %s: customer first name */ __( '%s purchased', 'aio-tools' ), $cust_name ?: __( 'A customer', 'aio-tools' ) ), 'product' => $prod->get_name(), 'url' => $prod->get_permalink(), 'image' => wp_get_attachment_image_url( $prod->get_image_id(), 'thumbnail' ), 'time' => $time_label, ]; } set_transient( 'aio_sales_popup_data_real', $sales_data, 30 * MINUTE_IN_SECONDS ); } } else { $sales_data = get_transient( 'aio_fake_sales_products' ); if ( false === $sales_data ) { $products = wc_get_products( [ 'status' => 'publish', 'limit' => 30, 'orderby' => 'date', 'order' => 'DESC', ] ); if ( empty( $products ) ) { return; } $raw_names = trim( (string) aio_get_option( 'optFakeSalesNames', '' ) ); $names = $raw_names ? array_values( array_filter( array_map( 'trim', preg_split( '/[\r\n,]+/', $raw_names ) ) ) ) : [ 'Nguyễn Văn Tuấn', 'Trần Thủy Tiên', 'Lê Hải Đăng', 'Phạm Minh Duy', 'Hoàng Thanh Tùng' ]; $raw_cities = trim( (string) aio_get_option( 'optFakeSalesCities', '' ) ); $cities = $raw_cities ? array_values( array_filter( array_map( 'trim', preg_split( '/[\r\n,]+/', $raw_cities ) ) ) ) : [ 'Hà Nội', 'TP.HCM', 'Đà Nẵng', 'Hải Phòng', 'Cần Thơ' ]; $sales_data = []; foreach ( $products as $product ) { $mins = wp_rand( 1, 55 ); $picked_name = $names[ array_rand( $names ) ]; $picked_city = $cities[ array_rand( $cities ) ]; $sales_data[] = [ 'headline' => sprintf( /* translators: 1: customer name, 2: city name */ __( '%1$s in %2$s purchased', 'aio-tools' ), $picked_name, $picked_city ), 'product' => $product->get_name(), 'url' => $product->get_permalink(), 'image' => wp_get_attachment_image_url( $product->get_image_id(), 'thumbnail' ), 'time' => sprintf( /* translators: %d: minutes ago */ __( '%d mins ago', 'aio-tools' ), $mins ), ]; } set_transient( 'aio_fake_sales_products', $sales_data, 2 * HOUR_IN_SECONDS ); } } if ( empty( $sales_data ) ) { return; } if ( count( $sales_data ) > 10 ) { $random_keys = (array) array_rand( $sales_data, 10 ); $filtered_data = array_intersect_key( $sales_data, array_flip( $random_keys ) ); $sales_data = array_values( $filtered_data ); } ?> query_vars['order-received'] ) ? absint( $wp->query_vars['order-received'] ) : 0; $order = $order_id ? wc_get_order( $order_id ) : false; // Never redirect failed payments away from the retry payment screen if ( $order && $order->has_status( 'failed' ) ) { return; } // Ensure Auto-Complete Orders still runs if both features are enabled if ( $order && ! empty( aio_get_option( 'opt-woo-auto-complete' ) ) && 'processing' === $order->get_status() ) { $order->update_status( 'completed', __( 'Auto-completed by AIO Tools.', 'aio-tools' ) ); } $url = trim( (string) aio_get_option( 'optWcThankYouUrl', '' ) ); if ( '' === $url ) { return; } if ( 0 === strpos( $url, '/' ) ) { $url = home_url( $url ); } if ( $order_id ) { $url = add_query_arg( 'order_id', $order_id, $url ); } // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect wp_redirect( esc_url_raw( $url ) ); exit; } /* ========================================================================== WCP-4. EMPTY CART REDIRECT ========================================================================== */ if ( ! empty( aio_get_option( 'optWcEmptyCartRedirect' ) ) ) { add_action( 'template_redirect', 'aio_tools_empty_cart_redirect' ); } function aio_tools_empty_cart_redirect() { if ( function_exists( 'is_cart' ) && function_exists( 'WC' ) && is_cart() && WC()->cart && WC()->cart->is_empty() ) { wp_safe_redirect( wc_get_page_permalink( 'shop' ) ); exit; } } /* ========================================================================== WCP-5. TRUST BADGES ========================================================================== */ if ( ! empty( aio_get_option( 'optWcTrustBadges' ) ) ) { add_action( 'woocommerce_after_add_to_cart_button', 'aio_tools_render_trust_badges' ); } function aio_tools_render_trust_badges() { ?>