woocommerce_wp_select产品属性条款中的选项数组
问题描述:
我正尝试在woocommerce中创建一个下拉列表框,但是它已使用数据库中的数据填充。woocommerce_wp_select产品属性条款中的选项数组
我有大部分的代码工作,但部分填充列表框正在杀死我。这是我迄今为止所拥有的。
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); // Display Extra Fields on General Tab Section
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); // Save Fields
function woo_add_custom_general_fields() {
global $woocommerce, $post, $product;
echo '<div class="options_group">';
// Select
$options = array(
'hide_empty' => false,
'order' => 'ASC',
'fields' => 'names'
);
$DogBreeds = get_terms('pa_breed', $options);
foreach ($DogBreeds as $key => $value) {
$theArray = "'{$value}' => __('{$value}' , 'woocommerce'), ";
}
woocommerce_wp_select(
array(
'id' => '_select',
'label' => __('My Select Field', 'woocommerce'),
'options' => $theArray //this is where I am having trouble
)
);
echo $theArray;
echo '<pre>';
var_dump($DogBreeds);
echo '</pre>';
echo '</div>';
}
// Save Fields
function woo_add_custom_general_fields_save($post_id){
// Select
$woocommerce_select = $_POST['_select'];
if(!empty($woocommerce_select))
update_post_meta($post_id, '_select', esc_attr($woocommerce_select));
else {
update_post_meta($post_id, '_select', Null);
}
}
这应该从WooCommerce中的“ATTRIBUTES”部分提取信息。我创建了一个品种属性,并在那里放了几个狗品种。
任何方向非常感谢!
我知道阵列上的'选项'部分是完全错误的,但我把它放在那里,所以你会知道我在做什么。
答
我已经重新访问了你的代码。主要问题在于选择<option>
阵列。
你会在代码中看到的变化:
// Display Extra Fields on General Tab Section
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');
function woo_add_custom_general_fields() {
global $post;
// Set HERE the product attribute taxonomy
$taxonomy = 'pa_breed';
// Get the selected value <== <== (updated)
$value = get_post_meta($post->ID, '_select', true);
if(empty($value)) $value = '';
$dog_breeds = get_terms($taxonomy, array(
'hide_empty' => false,
'order' => 'ASC',
'fields' => 'names'
));
$options[''] = __('Select a value', 'woocommerce'); // default value
foreach ($dog_breeds as $key => $term)
$options[$term] = $term; // <=== <=== <=== Here the correct array
echo '<div class="options_group">';
woocommerce_wp_select(array(
'id' => '_select',
'label' => __('My Select Field', 'woocommerce'),
'options' => $options, //this is where I am having trouble
'value' => $value,
));
echo '</div>';
}
// Save Fields
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');
function woo_add_custom_general_fields_save($post_id){
// Select
$woocommerce_select = $_POST['_select'];
if(!empty($woocommerce_select))
update_post_meta($post_id, '_select', esc_attr($woocommerce_select));
else {
update_post_meta($post_id, '_select', '');
}
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
在WooCommerce 3+中测试并正常工作。你会得到类似这样的东西(与你的品种):
感谢卢瓦克!!!!有用! :) 问题... $ value数组的原因是什么?直到我删除了$ value数组之前,这段代码并不适用于我。 – JoeDostie
我复制了代码并粘贴,它不会保存到数据库中。行: $ value = update_post_meta($ post-> ID,'_select',true); if(empty($ value))$ value =''; 和 'value'=> $ value, 必须从代码中删除才能发布到数据库。 否则它的作品完美。如果这些线路需要在那里,我没有得到它。 – JoeDostie
@JoeDostie **代码更新:**我的部分出现了一个小错误...我纠正了这个问题...... **现在它正在工作**(用'get_post_meta()'代替'update_post_meta()') – LoicTheAztec