我正在尝试将自定义文本字段添加到Woocommerce变体属性元中,但是当我将下面的代码添加到我的主题functions.php中时,“变体”选项卡会继续加载并且根本不加载变体...我不知道知道导致错误的是什么。有人可以帮忙!
这是我的代码(添加到functions.php)
function create_price_variations($loop, $variation_data, $variation)
{
$args = array(
'post_type' => 'product_variation',
'post_status' => array('private', 'publish'),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts($args);
$i = 0;
foreach ($variations as $variation) {
$variation_id = absint($variation->ID);
$variable_id = $this['variation_id'];
$variation_post_status = esc_attr($variation->post_status);
$variation_data = get_post_meta($variation_id);
$variation_data['variation_post_id'] = $variation_id;
// Get specific data from the certain custom fields using get_post_meta( $post_id, $key, $single );
$expiry[$i] = get_post_meta($variation->ID, '_price_expiry_date', true);
$i++;
}
woocommerce_wp_text_input(array(
'id' => '_price_expiry_date[' . $loop . ']',
'label' => 'Date Till This Price will Remain',
'type' => 'date'
));
woocommerce_wp_select(
array(
'id' => '_select[' . $variation->ID . ']',
'label' => __('My Select Field', 'woocommerce'),
'description' => __('Choose a value.', 'woocommerce'),
'options' => array(
'one' => __('Option 1', 'woocommerce'),
'two' => __('Option 2', 'woocommerce'),
'three' => __('Option 3', 'woocommerce')
)
)
);
}
我正在使用此钩子添加此功能!
add_action('woocommerce_product_after_variable_attributes','create_price_variations', 10, 2);
答案 0 :(得分:0)
您需要单独保存自定义字段,例如
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
function save_variation_settings_fields( $post_id ) {
// Text Field
$text_field = $_POST['_text_field'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
}
// Number Field
$number_field = $_POST['_number_field'][ $post_id ];
if( ! empty( $number_field ) ) {
update_post_meta( $post_id, '_number_field', esc_attr( $number_field ) );
}
// Textarea
$textarea = $_POST['_textarea'][ $post_id ];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_attr( $textarea ) );
}
// Select
$select = $_POST['_select'][ $post_id ];
if( ! empty( $select ) ) {
update_post_meta( $post_id, '_select', esc_attr( $select ) );
}
// Checkbox
$checkbox = isset( $_POST['_checkbox'][ $post_id ] ) ? 'yes' : 'no';
update_post_meta( $post_id, '_checkbox', $checkbox );
// Hidden field
$hidden = $_POST['_hidden_field'][ $post_id ];
if( ! empty( $hidden ) ) {
update_post_meta( $post_id, '_hidden_field', esc_attr( $hidden ) );
}
}