【问题标题】:Auto-populate in backend a WooCommerce Custom Product setting selector在后端自动填充 WooCommerce 自定义产品设置选择器
【发布时间】:2017-12-11 05:08:24
【问题描述】:

关于我之前在Retrieving WC Custom Field 成功回答的问题,我现在有选择字段并且还想添加自动完成的自定义字段(我还没有对此进行研究)

问题: 1.) 如何自动填充自定义选择字段?

//Adding the custom field select
woocommerce_wp_select( 
array( 
'id' => '_select', 
'label' => __( 'SIM Type', 'woocommerce' ), 
'options' => array(
'one' => __( 'Regular', 'woocommerce' ),
'two' => __( 'Nano', 'woocommerce' ),
'three' => __( 'Micro', 'woocommerce' )
)
)
);

//Saving
$woocommerce_select = $_POST['_select'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );

// Display Custom Field Value
echo get_post_meta( $post->ID, '_select', true );

【问题讨论】:

    标签: php wordpress woocommerce dropdown product


    【解决方案1】:

    更新:以编程方式在选择字段中设置 <option>

    1) 您需要存储选项键值的关联数组:

    // The associative array to store (once)
    $options_array = array(
        '' => __( 'Select a value', 'woocommerce' ), // default empty value
        'one' => __( 'Regular', 'woocommerce' ),
        'two' => __( 'Nano', 'woocommerce' ),
        'three' => __( 'Micro', 'woocommerce' )
    );
    
    // Serialize the array as a string
    $option_str = maybe_serialize( $options_array );
    
    // Save this array in Wordpress options
    update_option( 'my_custom_selector_options', $option_str );
    

    2) 获取和反序列化您的选择器选项:

    // Get your options select data
    $select_options_str = get_option( 'my_custom_selector_options' );
    
    // Unserialize this data:
    $select_options_arr = maybe_unserialize( $select_options_str );
    
    // Get the saved  selected 'value' if it exist
    $value = get_post_meta( $post->ID, '_select', true );
    if( empty( $value ) ) $value = ''; // When 'value' is not defined
    
    // 
    woocommerce_wp_select(
        array(
            'id' => '_select',
            'label' => __( 'SIM Type', 'woocommerce' ),
            'options' => $select_options_arr,
            'value' => $value,
        )
    );
    

    所以现在您的字段选择器选项已由您从 WordPress 选项中获得的数据填充。


    要自动填充 woocommerce_wp_select(),您必须以这种方式添加 'value' key

    ## 1. The select (dropdown)
    
    // Get the 'value' data if it exist
    $value = get_post_meta( $post->ID, '_select', true );
    if( empty( $value ) ) $value = ''; // When 'value' is not defined
    
    woocommerce_wp_select(
        array(
            'id' => '_select',
            'label' => __( 'SIM Type', 'woocommerce' ),
            'options' => array(
                '' => __( 'Select a value', 'woocommerce' ), // Added a default empty value
                'one' => __( 'Regular', 'woocommerce' ),
                'two' => __( 'Nano', 'woocommerce' ),
                'three' => __( 'Micro', 'woocommerce' )
            ),
            'value' => $value, // <===  ===  ===  ===  ===  HERE set the 'value' key (autofill)
        )
    );
    
    ## ---------------------------------
    
    ## 2. SAVING
    
    $woocommerce_select = $_POST['_select'];
    // The Default empty value is not saved (added in this condition below)
    if( !empty( $woocommerce_select ) || $woocommerce_select  != '' ) 
        update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
    

    快速测试:
    要查看它的实际效果,请将 'value' =&gt; $value, 替换为 'value' =&gt; 'two',
    然后选择的值将是:Nano ...

    【讨论】:

    • 嗨,谢谢@LoicTheAztec,但我要问的是如何在选择中像 for 循环一样以编程方式添加“常规”、“纳米”、“微”等值。以及如何存储和获取?也是通过meta吗?谢谢;)
    • 谢谢您,我会为默认值执行此操作。 ;) 它仍然会有所帮助。 ;)
    猜你喜欢
    • 2019-07-17
    • 1970-01-01
    • 2019-07-02
    • 1970-01-01
    • 2018-04-16
    • 2018-03-04
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    相关资源
    最近更新 更多