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 Hooks
$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);
// Hide "Additional Information" heading if order_comments is disabled
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']) && $cf['id'] === 'order_comments') {
if (!isset($cf['visible']) || !$cf['visible']) {
return false;
}
}
}
}
}
return $enable;
}
}
if (aio_get_option('optWcMergeNames') == 1) {
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'] = array('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'] = array('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'; // Legacy fallback
}
if ($direct_checkout_mode === 'redirect') {
add_filter('woocommerce_add_to_cart_redirect', 'aio_tools_direct_checkout_redirect');
} elseif ($direct_checkout_mode === 'lightbox') {
add_action('wp_footer', 'aio_tools_render_quick_buy_lightbox');
}
}
function aio_tools_render_quick_buy_lightbox() {
if ( ! function_exists( 'is_woocommerce' ) ) return;
?>
get_status() == 'processing' || $order->get_status() == 'on-hold') {
$order->update_status('completed');
}
}
// Custom Place Order Button Text
if (!empty(aio_get_option('txtPlaceOrderText'))) {
add_filter('woocommerce_order_button_text', 'aio_tools_custom_place_order_text');
}
function aio_tools_custom_place_order_text($text) {
return esc_html(aio_get_option('txtPlaceOrderText'));
}
// Sticky Add To Cart
if (!empty(aio_get_option('opt-woo-sticky-cart'))) {
add_action('wp_footer', 'aio_tools_render_sticky_add_to_cart');
}
function aio_tools_render_sticky_add_to_cart() {
if ( ! function_exists( 'is_product' ) || ! is_product() ) return;
global $product;
if (!$product) return;
?>
get_image('thumbnail', array('class' => 'aio-sc-img'))); ?>
get_name()); ?>
get_price_html()); ?>
= 5) {
wp_send_json_error(esc_html__('You are ordering too fast. Please try again after 5 minutes.', 'aio-tools'));
}
set_transient($rate_key, $requests + 1, 5 * MINUTE_IN_SECONDS);
$product_id = isset($_POST['product_id']) ? absint($_POST['product_id']) : 0;
$qty = isset($_POST['qty']) ? absint($_POST['qty']) : 1;
$name = isset($_POST['name']) ? sanitize_text_field(wp_unslash($_POST['name'])) : '';
$phone = isset($_POST['phone']) ? sanitize_text_field(wp_unslash($_POST['phone'])) : '';
$address = isset($_POST['address']) ? sanitize_text_field(wp_unslash($_POST['address'])) : '';
if (!$product_id || !$name || !$phone) {
wp_send_json_error(esc_html__('Please fill in all required fields (Name, Phone).', 'aio-tools'));
}
$product = wc_get_product($product_id);
if (!$product || !$product->is_in_stock()) {
wp_send_json_error(esc_html__('Product is out of stock or does not exist.', 'aio-tools'));
}
$order = wc_create_order();
$order->add_product($product, $qty);
$address_data = array(
'first_name' => $name,
'phone' => $phone,
'address_1' => $address,
'country' => 'VN'
);
$order->set_address($address_data, 'billing');
$order->set_address($address_data, 'shipping');
$order->set_payment_method('cod');
$order->set_payment_method_title(esc_html__('Cash on Delivery (COD)', 'aio-tools'));
$order->calculate_totals();
$order->update_status('processing', esc_html__('Order created via AIO Quick Buy.', 'aio-tools'), true);
wp_send_json_success(array(
'message' => esc_html__('Order placed successfully! We will contact you soon.', 'aio-tools'),
'redirect' => $order->get_checkout_order_received_url()
));
}
function aio_tools_CustomDisplayWooCheckoutFields($fields) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return $fields;
}
$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)) {
$ordered_billing_fields = array();
$priority = 10;
foreach ($custom_fields as $cf) {
$id = isset($cf['id']) ? $cf['id'] : '';
if (empty($id)) continue;
$group = 'billing';
if ($id === 'order_comments') $group = 'order';
// Hide field if visible is false
if (!isset($cf['visible']) || !$cf['visible']) {
unset($fields[$group][$id]);
continue;
}
// Create field if it doesn't exist (Custom Field)
if (!isset($fields[$group][$id])) {
$fields[$group][$id] = array(
'type' => 'text',
'class' => array('form-row-wide'),
'clear' => true
);
}
// Update Label & Placeholder
if (isset($cf['label'])) {
$fields[$group][$id]['label'] = sanitize_text_field($cf['label']);
}
if (isset($cf['placeholder'])) {
$fields[$group][$id]['placeholder'] = sanitize_text_field($cf['placeholder']);
}
// Update Required status
if (isset($cf['required'])) {
$fields[$group][$id]['required'] = !empty($cf['required']);
}
// Update Width Class
if (isset($cf['width'])) {
$classes = isset($fields[$group][$id]['class']) && is_array($fields[$group][$id]['class']) ? $fields[$group][$id]['class'] : array();
$classes = array_diff($classes, array('form-row-wide', 'form-row-first', 'form-row-last'));
if ($cf['width'] === 'half-left') $classes[] = 'form-row-first';
elseif ($cf['width'] === 'half-right') $classes[] = 'form-row-last';
else $classes[] = 'form-row-wide';
$fields[$group][$id]['class'] = $classes;
}
// Set Priority for ordering
$fields[$group][$id]['priority'] = $priority;
$priority += 10;
// Collect for ordering
if ($group === 'billing') {
$ordered_billing_fields[$id] = $fields['billing'][$id];
}
}
// Replace billing fields with our ordered ones
if (!empty($ordered_billing_fields)) {
// Keep any fields that weren't in our UI list at the bottom
foreach ($fields['billing'] as $k => $v) {
if (!isset($ordered_billing_fields[$k])) {
$ordered_billing_fields[$k] = $v;
}
}
$fields['billing'] = $ordered_billing_fields;
}
return $fields;
}
}
// Legacy fallback (from old settings)
if(aio_get_option( 'opt-first-name' ) == 1) { unset($fields['billing']['billing_first_name']); unset($fields['shipping']['shipping_first_name']); }
if(aio_get_option( 'opt-last-name' ) == 1) { unset($fields['billing']['billing_last_name']); unset($fields['shipping']['shipping_last_name']); }
if(aio_get_option( 'opt-company' ) == 1) { unset($fields['billing']['billing_company']); unset($fields['shipping']['shipping_company']); }
if(aio_get_option( 'opt-address-1' ) == 1) { unset($fields['billing']['billing_address_1']); unset($fields['shipping']['shipping_address_1']); }
if(aio_get_option( 'opt-address-2' ) == 1) { unset($fields['billing']['billing_address_2']); unset($fields['shipping']['shipping_address_2']); }
if(aio_get_option( 'opt-city' ) == 1) { unset($fields['billing']['billing_city']); unset($fields['shipping']['shipping_city']); }
if(aio_get_option( 'opt-postcode' ) == 1) { unset($fields['billing']['billing_postcode']); unset($fields['shipping']['shipping_postcode']); }
if(aio_get_option( 'opt-country' ) == 1) { unset($fields['billing']['billing_country']); unset($fields['shipping']['shipping_country']); }
if(aio_get_option( 'opt-state' ) == 1) { unset($fields['billing']['billing_state']); unset($fields['shipping']['shipping_state']); }
if(aio_get_option( 'opt-phone' ) == 1) { unset($fields['billing']['billing_phone']); }
if(aio_get_option( 'opt-email' ) == 1) { unset($fields['billing']['billing_email']); }
return $fields;
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'aio_tools_display_custom_checkout_fields_in_admin', 10, 1 );
function aio_tools_display_custom_checkout_fields_in_admin($order) {
$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;
if ($is_custom && !empty($id)) {
$value = $order->get_meta('_' . $id);
if (!empty($value)) {
echo '' . 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;
if ($is_custom && !empty($id) && isset($_POST[$id])) {
$order->update_meta_data( '_' . $id, sanitize_text_field($_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');
wp_dequeue_script('wc-cart-fragments');
}
}
}
/* ==========================================================================
WCP-2. FAKE SALES POPUP
========================================================================== */
if ( !empty(aio_get_option('optEnableWcFakeSales')) ) {
add_action('wp_footer', 'aio_tools_render_fake_sales_popup');
}
function aio_tools_render_fake_sales_popup() {
if (!function_exists('wc_get_products')) return;
$sales_data = get_transient('aio_fake_sales_products');
if ( false === $sales_data ) {
$products = wc_get_products([
'status' => 'publish',
'limit' => 50,
'orderby' => 'date',
'order' => 'DESC'
]);
if (empty($products)) return;
$sales_data = [];
$names = ['Nguyễn Văn Tuấn', 'Trần Thị Thủy Tiên', 'Lê Hoàng Hải Đăng', 'Phạm Minh Duy', 'Hoàng Thanh Tùng'];
$cities = ['Hà Nội', 'TP.HCM', 'Đà Nẵng', 'Hải Phòng', 'Cần Thơ', 'Bình Dương', 'Đồng Nai'];
foreach ($products as $product) {
$sales_data[] = [
'name' => $names[array_rand($names)],
'city' => $cities[array_rand($cities)],
'product' => $product->get_name(),
'url' => $product->get_permalink(),
'image' => wp_get_attachment_image_url($product->get_image_id(), 'thumbnail'),
'time' => wp_rand(1, 59) . ' phút trước'
];
}
set_transient('aio_fake_sales_products', $sales_data, 2 * HOUR_IN_SECONDS);
}
if (count($sales_data) > 10) {
$random_keys = array_rand($sales_data, 10);
$filtered_data = array_intersect_key($sales_data, array_flip($random_keys));
$sales_data = array_values($filtered_data);
}
?>
×
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() {
?>
is_in_stock() ) return;
$stock = $product->get_stock_quantity();
if ( ! $stock || $stock > 10 ) {
$stock = wp_rand(2, 7);
}
$percentage = max(10, min(100, ($stock / 20) * 100));
?>
cart ? WC()->cart->get_cart_contents_count() : 0;
$fragments['.aio-cart-count'] = '' . esc_html($count) . '';
return $fragments;
}
function aio_tools_render_side_cart() {
if ( ! function_exists( 'is_woocommerce' ) || ! function_exists( 'WC' ) ) return;
if ( is_cart() || is_checkout() ) return; // Không hiện ở trang cart/checkout
$count = WC()->cart ? WC()->cart->get_cart_contents_count() : 0;
?>