對於你的問題的第二部分:如果您僅使用簡單的產品類型(沒有變化),那麼您可以添加自定義價格字段的產品數據頁,並使用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;
}
您可以創建和產品頁面類似這樣的保存自定義字段:
//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));
}
-----------------對於產品Variations- ---------------------------
警告:變量產品要複雜得多,我對此答案的信心不足因爲我對上面的簡單產品有所瞭解,但這是我目前的理解。我有一些微型購物車顯示問題,我不得不在使用這種方法時(我將在後面解釋),但總計在迷你購物車和普通購物車中均可正確計算。
首先,我們希望新的字段添加到每個變種現有產品的變化選項卡上:
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>';
}
我們還需要動態地添加它們,每當用戶編輯產品頁面上增加了新的變種:
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>';
}
然後我們將更改保存到自定義字段中的變化元數據:
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]));
}
}
}
}
現在,我們有我們的自定義變化的元數據,我們可以訪問它的自定義價格模塊中,像這樣:
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;
}
現在,我認爲部分應該擁有的一切,除了微型車顯示工作。出於某種原因,我似乎無法弄清楚如何訪問變體元數據,以迫使它在迷你購物車中正確顯示 - 就像我發現迷你購物車顯示屏正在生成的位置,但我遇到了麻煩獲得正確的上下文路徑來訪問自定義變量,所以我最終必須在template-tags.php中執行此操作,並將自定義數組數組傳遞給我的自定義價格函數中的可選參數。就我們應該怎樣做而言,這感覺非常「錯誤」,但它完成了工作。我非常樂於聽到這個問題的「正確」解決方案。
在模板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>
請檢查答案 –