WooCommerce优惠券限制选项卡中保存自定义字段(自定义帖子类型)的问题

发布于 2025-01-21 22:15:31 字数 8 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2025-01-28 22:15:31

continue

Some comments/suggestions regarding your code attempt/question:

  • Using $value = get_post_meta( $post->ID, '_select', true ); is not necessary
  • In this answer I have used post type 'product' (since this is present by default in WooCommerce, adjust where necessary)

So you get:

// Add new field - usage restriction tab
function action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) {
    // Set post type
    $post_type = 'product';

    // Get posts
    $my_c_posts = get_posts( array(
        'posts_per_page'    => -1,
        'orderby'           => 'date',
        'order'             => 'DESC',
        'post_type'         => $post_type,
        'post_status'       => 'publish',
    ) );

    // Default value
    $options[''] = __( 'Select a value', 'woocommerce' );

    // Get post title
    foreach ( $my_c_posts as $key => $post ) {
        $options[$key] = $post->post_title;
    }

    // Output field
    echo '<div class="options_group">';

    woocommerce_wp_select( array(
        'id'      => '_select',
        'label'   => __( 'Select tour', 'woocommerce' ),
        'options' =>  $options,
    ) );

    echo '</div>';
}
add_action( 'woocommerce_coupon_options_usage_restriction', 'action_woocommerce_coupon_options_usage_restriction', 10, 2 );

// Save
function action_woocommerce_coupon_options_save( $post_id, $coupon ) {
    // Isset
    if ( isset ( $_POST['_select'] ) ) {
        $coupon->update_meta_data( '_select', sanitize_text_field( $_POST['_select'] ) );
        $coupon->save();
    }
}
add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文