Đối với phần 2 của câu hỏi của bạn: Nếu bạn chỉ sử dụng các loại sản phẩm đơn giản (không có biến thể) sau đó bạn có thể thêm các trường giá tùy chỉnh để trang dữ liệu sản phẩm và lọc giá bằng cách sử dụng woocommerce_get_price_html.
add_filter('woocommerce_get_price_html','so24863612_custom_price');
function so24863612_custom_price(){
global $post;
$_postID = $post->ID;
$product = get_product($_postID);
$UK_price = get_post_meta($_postID, '_UK_price', true); //loads custom meta data
$return_price = $product->get_regular_price(); //default to regular price
if (!empty($UK_price)) {
$return_price = $UK_price;
}
return $return_price;
}
Bạn có thể tạo và lưu các lĩnh vực tùy chỉnh trên trang sản phẩm như thế này:
//Display custom fields on product data page in admin
add_action('woocommerce_product_options_general_product_data', 'so24963039_display_custom_general_tab_fields');
function so24963039_display_custom_general_tab_fields() {
global $woocommerce, $post;
$UK_price = get_post_meta($post->ID, '_UK_price', true);
woocommerce_wp_text_input(
array(
'id' => '_UK_price',
'label' => __('UK Price (£)', 'woocommerce'),
'value' => $UK_price,
'desc_tip' => 'false'
)
);
}
//Save custom fields to access via get_post_meta
add_action('woocommerce_process_product_meta', 'so24963039_save_custom_general_tab_fields');
function so24963039_save_custom_general_tab_fields ($post_id) {
$woocommerce_UK_price = $_POST['_UK_price'];
if(!empty($woocommerce_UK_price))
update_post_meta($post_id, '_UK_price', esc_attr($woocommerce_UK_price));
}
----------------- Đối với sản phẩm với Variations- ---------------------------
CẢNH BÁO: Các sản phẩm biến phức tạp hơn nhiều và tôi gần như không tự tin trong câu trả lời này như tôi với phần sản phẩm đơn giản ở trên, nhưng đây là cách hiểu hiện tại của tôi. Tôi đã có một số vấn đề hiển thị giỏ hàng nhỏ mà tôi đã phải hack xung quanh khi sử dụng phương pháp này (mà tôi sẽ giải thích ở cuối), nhưng tổng số được tính toán chính xác trong cả giỏ hàng nhỏ và giỏ hàng thông thường.
Đầu tiên chúng tôi muốn thêm lĩnh vực mới để mỗi biến trên tab biến thể của sản phẩm hiện có:
add_action('woocommerce_product_after_variable_attributes', 'so24963039_variable_fields', 10, 2); //Display Fields
function so24963039_variable_fields($loop, $variation_data) {
echo '<tr><td>';
woocommerce_wp_text_input(
array(
'id' => '_variant_UK_price['.$loop.']',
'label' => __('UK Price (£)', 'woocommerce'),
'desc_tip' => 'false',
'value' => $variation_data['_variant_UK_price'][0]
)
);
echo '</td></tr>';
}
Chúng tôi cũng cần phải thêm chúng tự động bất cứ khi nào người dùng thêm các biến thể mới trên trang chỉnh sửa sản phẩm:
add_action('woocommerce_product_after_variable_attributes_js', 'so24963039_variable_fields_js'); //JS to add fields for dynamically added new variations
function so24963039_variable_fields_js(){ //add fields to new variations that get added
echo '<tr><td>';
woocommerce_wp_text_input(
array(
'id' => '_variant_UK_price[ + loop + ]',
'label' => __('UK Price (£)', 'woocommerce'),
'desc_tip' => 'false',
'value' => $variation_data['_variant_UK_price'][0]
)
);
echo '</td></tr>';
}
Sau đó, chúng tôi lưu thay đổi đối với các lĩnh vực tùy chỉnh trong các dữ liệu meta biến:
add_action('woocommerce_process_product_meta_variable', 'so24963039_save_variable_fields', 10, 1); //Save variation fields
function so24963039_save_variable_fields($post_id) {
if (isset($_POST['variable_sku'])) {
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
// Variant Tier 1 Price
$_variant_UK_price = $_POST['_variant_UK_price'];
for ($i = 0; $i < sizeof($variable_sku); $i++) {
$variation_id = (int) $variable_post_id[$i];
if (isset($_variant_UK_price[$i])) {
update_post_meta($variation_id, '_variant_UK_price', stripslashes($_variant_UK_price[$i]));
}
}
}
}
Bây giờ chúng ta có dữ liệu meta biến tùy chỉnh của chúng tôi, chúng ta có thể truy cập vào nó trong module giá tùy chỉnh như sau:
add_filter('woocommerce_get_price_html','so24863612_custom_price');
function so24863612_custom_price(){
global $post;
$_postID = $post->ID;
$product = get_product($_postID);
$product_type = $product->product_type;
$UK_price = get_post_meta($_postID, '_UK_price', true); //covers simple products
if($product_type == 'variation'){ //override with variant prices
$UK_price = get_post_meta($_postID, '_variant_$UK_price', true);
}
$return_price = $product->get_regular_price(); //default to regular price
if (!empty($UK_price)) {
$return_price = $UK_price;
}
return $return_price;
}
Bây giờ, tôi tin rằng một phần nên có tất cả mọi thứ làm việc ngoại trừ màn hình mini-giỏ hàng. Đối với một số lý do, có vẻ như tôi không thể tìm ra cách truy cập vào dữ liệu meta biến đổi để buộc nó hiển thị đúng cách trong giỏ hàng nhỏ - như tôi thấy nơi màn hình giỏ hàng nhỏ đang được tạo nhưng tôi gặp sự cố nhận được đường dẫn ngữ cảnh thích hợp để truy cập biến tùy chỉnh vì vậy tôi đã phải làm điều đó trong template-tags.php và chuyển một mảng các giá trị tùy chỉnh tới một tham số tùy chọn trong hàm giá tùy chỉnh của tôi. Điều này cảm thấy rất 'sai' về cách chúng ta nên làm mọi thứ, nhưng nó được hoàn thành công việc. Tôi rất cởi mở khi nghe giải pháp 'đúng' cho phần này của vấn đề.
Trong template-tags.php:
<div class="small-7 large-7 columns"><?php
$product_title = $_product->get_title();
echo '<a class="cart_list_product_title" href="'.get_permalink($cart_item['product_id']).'">' . apply_filters('woocommerce_cart_widget_product_title', $product_title, $_product) . '</a>';
echo '<div class="cart_list_product_price">';
//original line: echo woocommerce_price($_product->get_price());
/*Custom Price Override Block*/
$_productID = $_product->id;
$product_type = $_product->product_type;
if($product_type == 'variation') {
$custom_field_data = $_product->product_custom_fields;
$regular_price = $custom_field_data['_regular_price'];
$custom_UK_price = $custom_field_data['_variant_UK_price'];
$custom_variant_prices = [$regular_price[0], $custom_UK_price[0]];
echo so24863612_get_custom_price($_productID, $custom_variant_prices);
} else {
echo so24863612_get_custom_price($_productID);
}
/*End Custom Price Override Block*/
echo ' /</div>';
echo '<div class="cart_list_product_quantity">'.__('Quantity', 'woocommerce').': '.$cart_item['quantity'].'</div>';
?></div>
Vui lòng kiểm tra câu trả lời –