Nhiều a/e có hỏi làm thế nào để thêm kí tự trước và sau giá sản phẩm trong Woocommerce, bài viết này sẽ hướng dẫn giải quyết vấn đề của a/e….. bắt đầu thôi

Nôm na dễ hiểu đơn vị bán quần áo (giá / bộ), cát sỏi (giá/khối), gạch (giá/ viên)…….. làm thế nào để thêm text như hình…
Bước 1: Thêm cài đặt vào Woocommerce.

Chỉ cần dán đoạn code sau vào file functions.php của theme là được.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* * Prefix and sufix to price * Author: https://hungwordpress.com */ /*Add default setting*/ function hwp_woocommerce_general_settings( $array ) { $array[] = array( 'name' => __( 'Prefix and suffix to price', 'woocommerce' ), 'type' => 'title', 'desc' => '', 'id' => 'woocommerce_presuffix_settings' ); $array[] = array( 'title' => __( 'Prefix', 'woocommerce' ), 'desc' => __( 'Add prefix to price. Leave blank to disable.', 'woocommerce' ), 'id' => 'hwp_woocommerce_price_prefix', 'desc_tip' => true, 'type' => 'text', ); $array[] = array( 'title' => __( 'Suffix', 'woocommerce' ), 'desc' => __( 'Add suffix to price. Leave blank to disable.', 'woocommerce' ), 'id' => 'hwp_woocommerce_price_suffix', 'desc_tip' => true, 'type' => 'text', ); $array[] = array( 'type' => 'sectionend', 'id' => 'woocommerce_presuffix_settings'); return $array; }; add_filter( 'woocommerce_general_settings', 'hwp_woocommerce_general_settings', 10, 1 ); |
Bước 2: Thêm metabox vào mỗi sản phẩm.
Bước 1: giúp chúng ta set cho tổng thể toàn site ví dụ bạn bán quần áo thì set chung được. Vậy nếu trên website bán nhiều mặt hàng thì cần tạo thêm metabox cho phần thêm mới mỗi sản phẩm…

Copy vào dán code này vào file functions.php của theme đang sử dụng là được
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/*Add metabox to product*/ add_action( 'woocommerce_product_options_general_product_data', 'hwp_presuffix_products' ); function hwp_presuffix_products() { //Add metabox prefix to product woocommerce_wp_text_input( array( 'id' => '_product_prefix', 'label' => 'Prefix', 'description' => 'Add prefix to price. Leave blank to default.', 'desc_tip' => 'true', ) ); //Add metabox suffix to product woocommerce_wp_text_input( array( 'id' => '_product_suffix', 'label' => 'Suffix', 'description' => 'Add suffix to price. Leave blank to default.', 'desc_tip' => 'true', ) ); } /*Save metabox prefix and suffix*/ add_action( 'woocommerce_process_product_meta', 'hwp_presuffix_products_save' ); function hwp_presuffix_products_save( $post_id ) { if(get_post_type($post_id) == 'product'){ if ( isset($_POST['_product_prefix']) ) { if ($_POST['_product_prefix'] != "") { update_post_meta($post_id, '_product_prefix', sanitize_text_field($_POST['_product_prefix'])); } else { delete_post_meta($post_id, '_product_prefix'); } } if ( isset($_POST['_product_suffix']) ) { if ($_POST['_product_suffix'] != "") { update_post_meta($post_id, '_product_suffix', sanitize_text_field($_POST['_product_suffix'])); } else { delete_post_meta($post_id, '_product_suffix'); } } } } |
Bước 3: Code hiển thị nội dung vào trước và sau giá
Dán code này vào file functions.php của theme đang sử dụng sẽ thêm giá trị prefix và suffix phía trên vào trước và sau giá của sản phẩm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/*Add to price html*/ add_filter( 'woocommerce_get_price_html', 'az_price_prefix_suffix', 100, 2 ); function az_price_prefix_suffix( $price, $product ){ $prefix = get_option( 'hwp_woocommerce_price_prefix'); $suffix = get_option( 'hwp_woocommerce_price_suffix'); $prefix_product = sanitize_text_field(get_post_meta($product->get_ID(), '_product_prefix', true)); $suffix_product = sanitize_text_field(get_post_meta($product->get_ID(), '_product_suffix', true)); if($prefix_product || (is_numeric($prefix_product) && $prefix_product == 0)) $prefix = $prefix_product; if($suffix_product || (is_numeric($suffix_product) && $suffix_product == 0)) $suffix = $suffix_product; $prefix = ($prefix && $prefix !== 0)?'<span class="hwp_woocommerce_price_prefix">'.$prefix.'</span>':''; $suffix = ($suffix && $suffix !== 0)?'<span class="hwp_woocommerce_price_suffix">'.$suffix.'</span>':''; $price = $prefix.$price.$suffix; return apply_filters( 'hwp_woocommerce_get_price', $price ); } |
Kết quả:

=>>>> Full code
Dành cho ae 1 phát ăn liền dán toàn bộ code sau vào functions.php của theme đang sử dụng là được.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
/* * Prefix and sufix to price * Author: https://hungwordpress.com */ /*Add default setting*/ function hwp_woocommerce_general_settings( $array ) { $array[] = array( 'name' => __( 'Prefix and suffix to price', 'woocommerce' ), 'type' => 'title', 'desc' => '', 'id' => 'woocommerce_presuffix_settings' ); $array[] = array( 'title' => __( 'Prefix', 'woocommerce' ), 'desc' => __( 'Add prefix to price. Leave blank to disable.', 'woocommerce' ), 'id' => 'hwp_woocommerce_price_prefix', 'desc_tip' => true, 'type' => 'text', ); $array[] = array( 'title' => __( 'Suffix', 'woocommerce' ), 'desc' => __( 'Add suffix to price. Leave blank to disable.', 'woocommerce' ), 'id' => 'hwp_woocommerce_price_suffix', 'desc_tip' => true, 'type' => 'text', ); $array[] = array( 'type' => 'sectionend', 'id' => 'woocommerce_presuffix_settings'); return $array; }; add_filter( 'woocommerce_general_settings', 'hwp_woocommerce_general_settings', 10, 1 ); /*Add metabox to product*/ add_action( 'woocommerce_product_options_general_product_data', 'hwp_presuffix_products' ); function hwp_presuffix_products() { //Add metabox prefix to product woocommerce_wp_text_input( array( 'id' => '_product_prefix', 'label' => 'Prefix', 'description' => 'Add prefix to price. Leave blank to default.', 'desc_tip' => 'true', ) ); //Add metabox suffix to product woocommerce_wp_text_input( array( 'id' => '_product_suffix', 'label' => 'Suffix', 'description' => 'Add suffix to price. Leave blank to default.', 'desc_tip' => 'true', ) ); } /*Save metabox prefix and suffix*/ add_action( 'woocommerce_process_product_meta', 'hwp_presuffix_products_save' ); function hwp_presuffix_products_save( $post_id ) { if(get_post_type($post_id) == 'product'){ if ( isset($_POST['_product_prefix']) ) { if ($_POST['_product_prefix'] != "") { update_post_meta($post_id, '_product_prefix', sanitize_text_field($_POST['_product_prefix'])); } else { delete_post_meta($post_id, '_product_prefix'); } } if ( isset($_POST['_product_suffix']) ) { if ($_POST['_product_suffix'] != "") { update_post_meta($post_id, '_product_suffix', sanitize_text_field($_POST['_product_suffix'])); } else { delete_post_meta($post_id, '_product_suffix'); } } } } /*Add to price html*/ add_filter( 'woocommerce_get_price_html', 'az_price_prefix_suffix', 100, 2 ); function az_price_prefix_suffix( $price, $product ){ $prefix = get_option( 'hwp_woocommerce_price_prefix'); $suffix = get_option( 'hwp_woocommerce_price_suffix'); $prefix_product = sanitize_text_field(get_post_meta($product->get_ID(), '_product_prefix', true)); $suffix_product = sanitize_text_field(get_post_meta($product->get_ID(), '_product_suffix', true)); if($prefix_product || (is_numeric($prefix_product) && $prefix_product == 0)) $prefix = $prefix_product; if($suffix_product || (is_numeric($suffix_product) && $suffix_product == 0)) $suffix = $suffix_product; $prefix = ($prefix && $prefix !== 0)?'<span class="hwp_woocommerce_price_prefix">'.$prefix.'</span>':''; $suffix = ($suffix && $suffix !== 0)?'<span class="hwp_woocommerce_price_suffix">'.$suffix.'</span>':''; $price = $prefix.$price.$suffix; return apply_filters( 'hwp_woocommerce_get_price', $price ); } |
Chúc ae thành công!