WooCommerce优惠券限制选项卡中保存自定义字段(自定义帖子类型)的问题
continue
I'm trying to add a new field in the usage restriction of coupons.
Here is my code:
function add_coupon_cpt_field() {
$value = get_post_meta( $post->ID, '_select', true );
if( empty( $value ) ) $value = '';
$my_c_posts = get_posts( array(
'posts_per_page' => -1,
'orderby'=> 'date',
'order'=> 'DESC',
'post_type'=> 'tour',
'post_status'=> 'publish',
) );
$options[''] = __( 'Select a value', 'woocommerce'); // default value
foreach ($my_c_posts as $key => $post)
$options[$key] = $post->post_title;
echo '<div class="options_group">';
woocommerce_wp_select( array(
'id' => '_select',
'label' => __( 'Select Tour', 'woocommerce' ),
'options' => $options,
'value' => $value,
) );
echo '</div>';
}
add_action( 'woocommerce_coupon_options_usage_restriction', 'add_coupon_cpt_field', 10, 0 );
// Save Fields
add_action( 'woocommerce_coupon_options_save', 'custom_posts_fields_save' );
function custom_posts_fields_save( $post_id ){
$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', '' );
}
}
The titles are displayed but when I save the coupon, the selection is not saved. Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
continue
Some comments/suggestions regarding your code attempt/question:
$value = get_post_meta( $post->ID, '_select', true );
is not necessarySo you get: