在产品数据“常规”设置选项卡中添加下拉到产品编辑页面

问题描述 投票:2回答:1

我试图弄清楚如何修改单个产品选项,以便产品管理员可以从下拉列表中选择产品的条件,即新的/使用过的。

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

// 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>';
}
php wordpress woocommerce product hook-woocommerce
1个回答
1
投票

有一些错误和错误,所以我重新审视了一下你的代码。现在你必须用woocommerce_wp_text_input()替换woocommerce_wp_select()才能获得一个选择字段,这样:

// 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文件中。

经过测试和工作。

最好避免元键中的大写,它们应该以下划线开头。

© www.soinside.com 2019 - 2024. All rights reserved.