将下拉列表添加到产品数据“常规"中的产品编辑页面.设置标签 [英] Add a drop down to product edit pages in product data "General" settings tab

查看:79
本文介绍了将下拉列表添加到产品数据“常规"中的产品编辑页面.设置标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出如何修改单一产品选项,以便产品管理员可以从下拉列表中选择产品的状况,即新的/使用过的.

I am trying to figure out how to modify the singe product options so the product admin can pick from the drop down list the condition of product, i.e. new/ used.

以下是允许产品管理员手动输入产品条件的代码.

Below is a code that allows product admin to enter the condition of product manually.

// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
    global $woocommerce, $post;

    echo '<div class="options_group">';

    woocommerce_wp_text_input( array( // Text Field type
        'id'          => '_Stan', 
        'label'       => __( 'Stan', 'woocommerce' ), 
        'placeholder' => 'i.e: nowa; uzywana...',
        'desc_tip'    => 'true',
        'description' => __( 'Podaj stan plyty.', 'woocommerce' ) 
    ) );

    echo '</div>'; // Closing </div> tag HERE
}

// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields' );
function woo_save_custom_general_fields( $post_id ){

    // Saving "Conditions" field key/value
    $Stan_field = $_POST['_Stan'];
    if( !empty( $Stan_field ) )
        update_post_meta( $post_id, '_Stan', esc_attr( $Stan_field ) );


}
add_action('woocommerce_single_product_summary', 'woo_display_custom_general_fields_values', 45);
function woo_display_custom_general_fields_values() {
    global $product;

    echo '<p class="custom-Stan">Stan: ' . get_post_meta( $product->id, '_Stan', true ) . '</p>';
}

推荐答案

有一些错误和错误,所以我再次介绍了您的代码.现在,您必须将woocommerce_wp_text_input()替换为woocommerce_wp_select(),以获取选择字段,方法是:

There is some errors and mistakes so I have revisited a little bit your code. Now you will have to replace woocommerce_wp_text_input() by woocommerce_wp_select() to get a select field instead, this way:

// Enabling and Displaying Fields in backend
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {

    echo '<div class="options_group">';

    woocommerce_wp_select( array( // Text Field type
        'id'          => '_Stan',
        'label'       => __( 'Stan', 'woocommerce' ),
        'description' => __( 'Podaj stan plyty.', 'woocommerce' ),
        'desc_tip'    => true,
        'options'     => array(
            ''        => __( 'Select product condition', 'woocommerce' ),
            'Nowa'    => __('Nowa', 'woocommerce' ),
            'Uzywana' => __('Uzywana', 'woocommerce' ),
        )
    ) );

    echo '</div>';
}

// Save Fields values to database when submitted (Backend)
add_action( 'woocommerce_process_product_meta', 'woo_save_custom_general_fields', 30, 1 );
function woo_save_custom_general_fields( $post_id ){

    // Saving "Conditions" field key/value
    $posted_field_value = $_POST['_Stan'];
    if( ! empty( $posted_field_value ) )
        update_post_meta( $post_id, '_Stan', esc_attr( $posted_field_value ) );


}

// Display In front end
add_action( 'woocommerce_product_meta_start', 'woo_display_custom_general_fields_values', 50 );
function woo_display_custom_general_fields_values() {
    global $product;

    // compatibility with WC +3
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    echo '<span class="stan">Stan: ' . get_post_meta( $product_id, '_Stan', true ) . '</span>';
}

代码进入活动子主题(或活动主题)的function.php文件.

Code goes in function.php file of the active child theme (or active theme).

经过测试,可以正常工作.

Tested and works.

最好避免使用元键中的大写字母,而应以下划线开头.

Is better to avoid capitals in meta keys and they should start with an underscore.

这篇关于将下拉列表添加到产品数据“常规"中的产品编辑页面.设置标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆