首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >woocommerce_wp_select保存所选选项并显示在前端

woocommerce_wp_select保存所选选项并显示在前端
EN

Stack Overflow用户
提问于 2021-01-08 09:18:38
回答 1查看 1.2K关注 0票数 1

我面临一个关于woocommerce_wp_select的问题。我正在向单一产品页面添加新的字段。首先,我通过以下代码添加这些选项:

代码语言:javascript
复制
$title_110 = array(
    'id' => 'custom_text_field_title_110',
    'label' => __( 'Awards', 'rasa_store' ),
    'desc_tip' => true,
    'description' => __( 'Select an option.', 'ctwc' ),
    'options'     => array(
        ''        => __( 'Select Option', 'woocommerce' ),
        '0'       => __('This product does not win any awards', 'woocommerce' ),
        '1'       => __('This product win on award.', 'woocommerce' ),
        '2'       => __('This product win 2 award.', 'woocommerce' ),
        '3'       => __('This product win 3 award.', 'woocommerce' ),
        '4'       => __('This product very famous.', 'woocommerce' )
    ),
    ); 

woocommerce_wp_select( $title_110 );

比我拯救它还重要。

代码语言:javascript
复制
$attribute_110 = wc_get_product( $post_id );
$title_top_110 = isset( $_POST['custom_text_field_title_110'] ) ? $_POST['custom_text_field_title_110'] : '';
$attribute_110->update_meta_data( 'custom_text_field_title_110', sanitize_text_field( $title_top_110 ) );
$attribute_110->save();

但是在单一产品页面的首页中,我使用:

代码语言:javascript
复制
$attribute_11 = wc_get_product ( $post->ID );
$title_top_110 = $attribute_11->get_meta( 'custom_text_field_title_110' );
if( $title_top_110  ) {
printf(
'<div class="row">
<div class="col-md-4">
<img class="img-fluid box-10-2" src="%s/img/award-icon.png">
</div>
<div class="col-md-8 box-10">
<p class="card-text box-10-1">%s</p>
</div>
</div>
',
esc_html( get_bloginfo('template_directory')  ),
esc_html( $title_top_110 )
);
}

我看到的不是打印This product does not win any awards,而是0。我正在寻找一种方法来修复它。我测试以下方法,但它们不起作用:

代码语言:javascript
复制
1. Replaced update_post_meta() by get_post_meta()
2. Replaced esc_html to esc_sql
EN

回答 1

Stack Overflow用户

发布于 2021-01-08 15:58:25

您可以根据所做的设置选项,因此此部分应按预期的方式工作。

代码语言:javascript
复制
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_text_field_title_110' );
function woo_add_custom_text_field_title_110(){
    $title_110 = array(
        'id' => 'custom_text_field_title_110',
        'label' => __( 'Awards', 'rasa_store' ),
        'desc_tip' => true,
        'description' => __( 'Select an option.', 'ctwc' ),
        'options'     => array(
            ''        => __( 'Select Option', 'woocommerce' ),
            '0'       => __('This product does not win any awards', 'woocommerce' ),
            '1'       => __('This product win on award.', 'woocommerce' ),
            '2'       => __('This product win 2 award.', 'woocommerce' ),
            '3'       => __('This product win 3 award.', 'woocommerce' ),
            '4'       => __('This product very famous.', 'woocommerce' )
        ),
    ); 
    
    woocommerce_wp_select( $title_110 );
}

拯救田野

代码语言:javascript
复制
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_text_field_title_110_save' );
function woo_add_custom_text_field_title_110_save( $post_id ){
  // Select
  $title_top_110  = $_POST['custom_text_field_title_110'];
  if( !empty( $title_top_110 ) )
      update_post_meta( $post_id, 'custom_text_field_title_110', esc_attr( $title_top_110 ) );
  else {
      update_post_meta( $post_id, 'custom_text_field_title_110',  '' );
  }
}

并将数据输出到单个产品页面(需要修改此代码以将其输出到循环中)

代码语言:javascript
复制
$title_top_110 = get_post_meta( 'custom_text_field_title_110', $post->ID, true );
if( $title_top_110  ) {
  printf(
  '<div class="row">
  <div class="col-md-4">
  <img class="img-fluid box-10-2" src="%1$s/img/award-icon.png">
  </div>
  <div class="col-md-8 box-10">
  <p class="card-text box-10-1">%2$s</p>
  </div>
  </div>
  ',
  esc_html( get_bloginfo('template_directory')  ),
  esc_html( $title_top_110 )
  );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65626294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档