Học WordPress + AI / WordPress cho người VIỆT !
WPShare247

Học wordpress cho người mới bắt đầu

  • Bắt đầu
    • Học WordPress
    • Elementor
    • WooCommerce
    • Tạo WordPress
    • Học WordPress kiểu mới 2025
    • Học PHP cơ bản
    • Tạo Website bằng WordPress
    • Quản trị WordPress
    • Sửa lỗi WordPress
    • Theme wordpress
    • Plugin WordPress
    • Hàm PHP
    • Seo WordPress
    • Bảo mật WordPress
    • Html & Css
    • Javascript và jQuery
    • Hướng dẫn lập trình
  • Video
    • Video TikTok
    • Video Shorts
  • Kho plugin
  • Tải Web Miễn Phí
  • Công cụ
    • Kiểm tra web WordPress
    • WordPress theme gì?
    • Check IP Website
    • Kiểm tra Tên Miền
    • Kiểm Tra SSL
    • Tạo File Disavow Google
    • Tạo QR Code
  • Giới thiệu
    • Khuyến Mãi
  • Đăng nhập
X
☰

Trang chủ » WooCommerce » Hướng dẫn cách custom Thêm Tùy Chọn giá cho Sản Phẩm WooCommerce

📌 Mới: Bạn có thể 🧠 đặt câu hỏi cho AI về nội dung bài viết này ở cuối trang!

Hướng dẫn cách custom Thêm Tùy Chọn giá cho Sản Phẩm WooCommerce

Xuất bản vào 16/09/2024 bởi Chinsu Man Xem: 2379
Shares
ChatGPT ChatGPT
Share
Share
Pin

CHƯƠNG TRÌNH GIẢM 50% GIÁ TẤT CẢ CÁC GÓI HOSTING WORDPRESS => Link giảm 50%

Bài viết hướng dẫn có chứa CODE - Copy và sử dụng chúng sẽ tiện hơn cho bạn.

Plugin WooCommerce cho phép chúng ta dễ dàng thêm các tùy chọn giá cho bất kì sản phẩm nào như hình. Còn cách thực hiện như thế nào, bạn xem chi tiết đoạn code phần dưới nhé.

Hướng dẫn cách custom Thêm Tùy Chọn giá cho Sản Phẩm WooCommerce

🌱 Nếu thấy bài viết hữu ích, bạn có thể ủng hộ tác giả bằng cách nhấn vào quảng cáo bên dưới — như một donate miễn phí. Bạn không mất gì nhưng chúng tôi sẽ có thêm chi phí để duy trì và vận hành website. Cảm ơn bạn! 🙏

Sau khi Add to cart thành công, chi tiết giá mới sản phẩm sẽ thể hiện như hình bên dưới. Bạn hoàn toàn có thể custom css theo ý mình cho đẹp và bắt mắt hơn.

Hướng dẫn cách custom Thêm Tùy Chọn giá cho Sản Phẩm WooCommerce

1. Một số Hook WooCommerce dùng trong chức năng này

  • Action woocommerce_before_add_to_cart_button : Thêm một hoặc nhiều field mới vào trong Form add to cart của chi tiết sản phẩm
  • Filter woocommerce_add_cart_item_data : Request – tính lại giá từng item giỏ hàng (cart item)
  • Action woocommerce_before_calculate_totals : Tính lại giá mới cho cart item
  • Filter woocommerce_cart_item_price : Hiển thị giá custom cart item trong trang giỏ hàng

Còn cụ thể Hook WooCommerce nói riêng hay WordPress nói chung là gì? Tôi sẽ giải thích chi tiết cho các bạn ở những bài viết tiếp theo.

2. Đoạn code thêm tùy chọn giá cho Sản Phẩm ( Custom Price Options)

Sao chép đoạn code sau và nhúng vào file functions.php trong theme WordPress bạn đang dùng.

// Bước 1: Thêm một hoặc nhiều fied mới vào trong Form add to cart của chi tiết sản phẩm
add_action( 'woocommerce_before_add_to_cart_button', 'wpshare247_product_price_field', 5 );
function wpshare247_product_price_field(){
    ?>
    <div class="wpshare247-custom-product-field">
        <div>Giá thêm 1:</div>
        <input type="text" name="wpshare247-field-custom-price-1" value="250">
        <div>Giá thêm 2:</div>
        <input type="text" name="wpshare247-field-custom-price-2" value="310">
    </div>
    <?php
}

// Bước 2: Request - tính lại giá từng item giỏ hàng (cart item)
add_filter('woocommerce_add_cart_item_data', 'wpshare247_add_custom_field_data', 20, 2 );
function wpshare247_add_custom_field_data( $cart_item_data, $product_id ){
    $custom_price = 0;
    if(isset($_POST['wpshare247-field-custom-price-1'])){
        $custom_price_1 = (float) sanitize_text_field( $_POST['wpshare247-field-custom-price-1'] );
        $custom_price += $custom_price_1;
    }

    if(isset($_POST['wpshare247-field-custom-price-2'])){
        $custom_price_2 = (float) sanitize_text_field( $_POST['wpshare247-field-custom-price-2'] );
        $custom_price += $custom_price_2;
    }

    if( !$custom_price ) return $cart_item_data;


    $text_custom_price = '<ul>';
    if($custom_price_1){
        $text_custom_price .= '<li>- Giá thêm 1: +'.wc_price($custom_price_1).'</li>';
    }
    if($custom_price_2){
        $text_custom_price .= '<li>- Giá thêm 2: +'.wc_price($custom_price_2).'</li>';
    }
    $text_custom_price .= '</ul>';

    //--------------
    $product    = wc_get_product($product_id); 

    if($product->is_type( 'simple' )){
        $base_price = (float) $product->get_price(); // Product reg price
    }else{
         $available_variations = $product->get_available_variations(); 
         $arr_key = array_keys($available_variations[0]['attributes']);
         $var_key = $arr_key[0];
         $var_key_choose = $_POST[ $var_key ];
         
         if( $available_variations ){
             foreach($available_variations as $varit){
                $arr_val =  array_values($varit['attributes']);
                $val = $arr_val[0];
                if($val == $var_key_choose){
                    $base_price = $varit['display_price'];
                }
            }
         }
    }

    // Tính lại giá mới
    $new_price = $base_price + $custom_price;

    // Set the custom amount in cart object
    $cart_item_data['wpshare247_custom_data']['base_price'] = (float) $base_price; 
    $cart_item_data['wpshare247_custom_data']['extra_charge'] = (float) $custom_price; 
    $cart_item_data['wpshare247_custom_data']['new_price'] = (float) $new_price;
    $cart_item_data['wpshare247_custom_data']['text_custom_price'] = $text_custom_price;
    $cart_item_data['wpshare247_custom_data']['unique_key'] = md5( microtime() . rand() ); // Make each item unique

    return $cart_item_data;
}

// Bước 3: Tính lại giá mới cho cart item
add_action( 'woocommerce_before_calculate_totals', 'wpshare247_price_add_custom_price', 20, 1 );
function wpshare247_price_add_custom_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        if( isset($cart_item['wpshare247_custom_data']['new_price']) )
            $cart_item['data']->set_price( (float) $cart_item['wpshare247_custom_data']['new_price'] );
    }
}

// Bước 4: Hiển thị giá custom cart item trong trang giỏ hàng
add_filter('woocommerce_cart_item_price', 'wpshare247_display_cart_items_custom_price_details', 20, 3 );
function wpshare247_display_cart_items_custom_price_details( $product_price, $cart_item, $cart_item_key ){
    $text_custom_price = $cart_item['wpshare247_custom_data']['text_custom_price'];
    if( isset($cart_item['wpshare247_custom_data']['extra_charge']) ) {
        $product = $cart_item['data'];

        $product_price  = wc_price( $cart_item['wpshare247_custom_data']['base_price'] );


        $product_price .= '<div class="wpshare247-custom-item-container" style="border: 1px solid #f00;padding: 5px;margin-top: 2px;">';
            $product_price .= '<div>Phí custom sản phẩm</div>';

            if($text_custom_price)
            $product_price .= '<div class="text_custom_price">'.$text_custom_price.'</div>';

        	$product_price .= 
    		'<div class="wpshare247-charge">+' . wc_price( $cart_item['wpshare247_custom_data']['extra_charge'] ).'</div>';
    	$product_price .= '</div>';
    }
    return $product_price;
}

 

CHƯƠNG TRÌNH GIẢM 50% GIÁ TẤT CẢ CÁC GÓI HOSTING WORDPRESS => Link giảm 50%

❓ Hỏi AI về nội dung bài viết

Tóm tắt nội dung chính Gợi ý mục quan trọng Giải thích mục 1 rõ hơn Viết lại đoạn văn cho dễ hiểu Rút gọn văn bản mà vẫn đủ ý Thêm ví dụ minh hoạ cho đoạn 1 Lọc keyword từ khóa bài viết Lọc thuật ngữ Dịch bài viết sang: Tiếng Anh
Nếu bài viết chưa giải đáp hết thắc mắc của bạn, hãy hỏi WPSHARE247 AI – trợ lý thông minh sẵn sàng hỗ trợ ngay trong nội dung bài viết.

Bạn muốn tích hợp chức năng Hỏi AI trực tiếp dưới mỗi bài viết trên website? Hãy liên hệ dịch vụ thiết kế web tại Website366.com để được hỗ trợ triển khai nhanh chóng và chuyên nghiệp.

Shares
ChatGPT ChatGPT
Share
Share
Pin
← Bài cũ hơn → Bài tiếp theo
woocommerce
Bài viết liên quan
  • Thử kết nối API trong WordPress cho người mới học code (cập nhật 2025)

    Thử kết nối API trong WordPress cho người mới học code (cập nhật 2025)

  • Filter không còn hoạt động trong WooCommerce Blocks: Cảnh báo quan trọng dành cho Developer

    Filter không còn hoạt động trong WooCommerce Blocks: Cảnh báo quan trọng dành cho Developer

  • Hướng dẫn cách thêm một Section mới trong WooCommerce Settings

    Hướng dẫn cách thêm một Section mới trong WooCommerce Settings

  • Hướng dẫn cách tích hợp Ui Slider jQuery vào Website WordPress

    Hướng dẫn cách tích hợp Ui Slider jQuery vào Website WordPress

Học thiết kế Web WordPress

Download WordPress

Tải bản WordPress mới nhất

Tiếng Việt: Tải WordPress Mới

Quốc tế: Tải WordPress Mới

Liên kế MXH của WPShare247

Tìm thấy những kiến thức mới từ WPShare247

Giảm 50% Hosting

Xem chi tiết giảm giá 50% gói lưu trự Hosting tại đây => Link giảm 50%

Cách tạo Web bán hàng Bằng Flatsome

https://www.youtube.com/watch?v=gnT8Dn_KiXU

Video Tiktok mới nhất

@hocwordpress SSL bằng Plug-in Wordpress Really Simple SSL giúp bảo mật đúng cách hơn, tránh tình trạng bảo mật ảo HTTPS #wpshare247 #wordpress #plugin #ssl #https ♬ nhạc nền - Học WordPress - WPSHARE247.COM

Cách tạo Website WordPress 2025

https://www.youtube.com/watch?v=HtbtsH8q-KU

Bộ WIDGETS WS247 Đa Năng

Giúp bạn Thiết kế Website WordPress giống mẫu 99% trong thời gian ngắn nhất (Có bản nâng cấp mới V2). Download tại đây

 

Xem chi tiết tại đây  https://wpshare247.com/chia-se-bo-field-widget-giup-ban-thiet-ke-website-wordpress-nhanh-hon

MUA HOSTING – TẶNG BUỔI HỌC WP

Tặng 03 buổi (Mỗi buổi 30 phút) hỗ trợ miễn phí, tất cả các chuyên đề nào về học thiết kế website bằng WordPress khi mua Hosting và Tên miền => tại đây [ Có thể liên hệ bất cứ giờ nào để hỗ trợ học WP ]

Tôi đang cần...
Cách làm blog Plugin Wordpress Bảo mật Wordpress Seo với Wordpress Lỗi Wordpress WooCommerce

MUA HOSTING – TẶNG BUỔI HỌC WP

Chủ đề

  • Code WordPress
  • Hàm WordPress
  • Hướng dẫn WordPress
  • Theme wordpress
  • WooCommerce
  • Seo WordPress
  • Html & Css
  • Javascript và jQuery
  • Plugin WordPress
  • Hướng dẫn lập trình

Tag

add_action (2) add_filter (2) admin code (2) alt (2) Block Theme (2) cart (4) Contact Form 7 (8) css (4) custom post type (2) cài wordpress (2) danh mục sản phẩm (2) elementor (2) elementor free (2) filter (2) flatsome (2) functions.php (2) google font (2) hosting (2) html css (2) hướng dẫn dùng plugin (4) học wordpress (7) javascript (5) jquery (3) localhost (2) Loop (2) mysql (4) ngôn ngữ (2) phpmyadmin (3) plugin (4) plugin wordpress (2) Product (4) SMTP (2) Sản phẩm (3) Template page (2) theme option (2) Theme Wordpress (3) widget (2) woocommerce (25) wordpress (5) WordPress 2025 (2) wp-config (2) wp_enqueue_scripts (2) WP_Query (6) xampp (4) Yoast SEO (2)
Về WPShare247

Học wordpress cho người mới bắt đầu.

WPShare247 là blog chia sẻ các kiến thức về WordPress, sửa chữa lỗi wordpress, giới thiệu theme và plugin wordpress mới nhất. Đào tạo khóa học thiết kế Website bằng WordPress.

Menu
  • Trang chủ
  • Giới thiệu
  • Blog
  • Plugin WordPress
  • Liên hệ
Chủ đề wordpress
  • Sửa lỗi WordPress
  • Plugin WordPress
  • Theme wordpress
  • Seo WordPress
  • WooCommerce

Copyright © 2025 WPSHARE247. All Rights Reserved.
Một sản phẩm của WEB 366

  • Thiết kế web bởi Website366.com
  • Thiết kế website chuyên nghiệp TBAY.VN
  • Thiết kế website bán hàng