Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Wordpress » [WooCommerce] Hiển thị giá khác nhau dựa trên khu vực mua sắm

[WooCommerce] Hiển thị giá khác nhau dựa trên khu vực mua sắm

Thứ Ba, 16/10/2018 by Hoàng Quách

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Bạn muốn hiển thị các mức giá khác nhau tùy thuộc vào quốc gia mà khách hàng của bạn đang sống. Đây là một chức năng rất quan trọng, nếu phải bao gồm thuế suất, hoặc phí xuất khẩu.

Trong bài này, bạn sẽ học cách có thể hiển thị một mức giá khác cho sản phẩm của mình, tùy thuộc vào nơi khách hàng mua sắm. Đoạn mã sau sẽ hiển thị thêm một trường giá tùy chỉnh mới.

add_action( 'woocommerce_product_options_pricing', 'tl_add_can_price_box' );
function tl_add_can_price_box() {
    woocommerce_wp_text_input( array( 'id' => 'can_price', 'class' => 'wc_input_price_extra_info short', 'label' => __( 'Price in Canadian , 'woocommerce' ) ) );
}

Bạn cần lưu lại trường này, khi cập nhật sản phẩm.

// This one is for saving above fields
add_action('woocommerce_process_product_meta', 'tl_save_can_price', 2, 2);
function tl_save_can_price($post_id, $post) {
    update_post_meta($post_id, 'can_price', stripslashes($_POST['can_price']));
}

Tiếp đến, mình sẽ thêm chức năng kiểm tra quốc gia của khách hàng và thêm điều kiện hiển thị giá mặc định hoặc giá tùy chỉnh.

// Here is the logic for price
if(!is_admin()) {
    add_filter( 'woocommerce_get_price', 'change_price', 10, 2 );
    function change_price($price, $product) {
        global $woocommerce;
        $customer_country = $woocommerce->customer->get_country();
        if($customer_country == "CA") {
            return get_post_meta($product->id, 'can_price', true); 
        } else {
            return $price;
        }
        
    }
}

Nếu bạn có nhiều quốc gia có giá tùy chỉnh, bạn có thể thêm một quốc gia khác trong điều kiện của mình:

// Here is the logic for price
if(!is_admin()) {
    add_filter( 'woocommerce_get_price', 'change_price', 10, 2 );
    function change_price($price, $product) {
        global $woocommerce;
        $customer_country = $woocommerce->customer->get_country();
        if($customer_country == "CA") {
            return get_post_meta($product->id, 'can_price', true);
        } elseif ($customer_country == "US") {
            return get_post_meta($product->id, 'us_price', true);
        } else {
            return $price;
        }
        
    }
}

Đây là toàn bộ code bạn sẽ thêm vào tệp functions.php

add_action( 'woocommerce_product_options_pricing', 'tl_add_can_price_box' );
function tl_add_can_price_box() {
    woocommerce_wp_text_input( array( 'id' => 'can_price', 'class' => 'wc_input_price_extra_info short', 'label' => __( 'Price in Canadian , 'woocommerce' ) ) );
}
// This one is for saving above fields
add_action('woocommerce_process_product_meta', 'tl_save_can_price', 2, 2);
function tl_save_can_price($post_id, $post) {
    update_post_meta($post_id, 'can_price', stripslashes($_POST['can_price']));
}

// Here is the logic for price
if(!is_admin()) {
    add_filter( 'woocommerce_get_price', 'change_price', 10, 2 );
    function change_price($price, $product) {
        global $woocommerce;
        $customer_country = $woocommerce->customer->get_country();
        if($customer_country == "CA") {
            return get_post_meta($product->id, 'can_price', true); 
        } else {
            return $price;
        }
        
    }
}

Chúc bạn thành công.

Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Hoangweb trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Wordpress Tìm kiếm: Woocommerce

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.