2
現在,我可以將1個自定義選擇菜單添加到我的產品變體區域。WooCommerce產品變體設置選項卡中的多個選擇自定義字段
但是,我想添加一個額外的選擇菜單的產品變化,但不知道如何。
這是我對我的孩子主題functions.php
生成一個菜單代碼:
// Add Variation Settings
add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3);
// Save Variation Settings
add_action('woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2);
/**
* Create new fields for variations
*
*/
function variation_settings_fields($loop, $variation_data, $variation) {
// Select
woocommerce_wp_select(
array(
'id' => '_select[' . $variation->ID . ']',
'label' => __('My Select Field', 'woocommerce'),
'description' => __('Choose a value.', 'woocommerce'),
'value' => get_post_meta($variation->ID, '_select', true),
'options' => array(
'one' => __('Option 1', 'woocommerce'),
'two' => __('Option 2', 'woocommerce'),
'three' => __('Option 3', 'woocommerce')
)
)
);
}
/**
* Save new fields for variations
*
*/
function save_variation_settings_fields($post_id) {
// Select
$select = $_POST['_select'][ $post_id ];
if(! empty($select)) {
update_post_meta($post_id, '_select', esc_attr($select));
}
}
非常感謝。只要我在30分鐘內回到我的辦公桌,我會試用這個:-) – michaelmcgurk
它工作得很好 - 只是後續。如何在前端顯示此值? – michaelmcgurk
@michaelmcgurk更新了我的答案,關於使用... – LoicTheAztec