【问题标题】:Fill select field options from custom product field values in Woocommerce checkout从 Woocommerce 结帐中的自定义产品字段值中填写选择字段选项
【发布时间】:2018-10-29 16:40:45
【问题描述】:

编辑:已解决。我用解决方案编辑了帖子。 =)

我正在尝试使用自定义产品字段中的值填充结帐页面上的选择字段。大部分都像一个魅力,但在结帐页面上检索价值存在问题。

创建并保存自定义字段

// Display Fields in Backend
add_action( 'woocommerce_product_options_general_product_data', 'srd_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'srd_add_custom_general_fields_save' );

// Create Fields
function srd_add_custom_general_fields() {

  global $woocommerce, $post;

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

    woocommerce_wp_textarea_input(
        array(
            'id'          => '_einstiegsorte',
            'label'       => __( 'Einstiegsorte', 'woocommerce' ),
            'placeholder' => '',
            'description' => __( '', 'woocommerce' )
        )
      );
    }

保存数据

function srd_add_custom_general_fields_save( $post_id ){

$woocommerce_textarea = $_POST['_einstiegsorte'];

        if( !empty( $woocommerce_textarea ) )
        update_post_meta( $post_id, '_einstiegsorte', esc_html( $woocommerce_textarea ) );

单个产品和存档的输出数据

add_action( 'woocommerce_after_shop_loop_item_title', 'custom_fields_ausgabe_archive', 2 );
add_action( 'woocommerce_single_product_summary', 'custom_fields_ausgabe', 6);

function custom_fields_ausgabe(){

    $list_items = get_post_meta(get_the_ID(), '_einstiegsorte', true);
     if($list_items){?>

        $list_items = explode("\n", $list_items);
            echo '<ul>';
                foreach($list_items as $list_item) {
                    echo '<li>' . $list_item . '</li>';
                }
            echo '</ul>';
    }
}

将选择字段添加到结帐页面并填充数据“_einstiegsorte”

add_action('woocommerce_before_checkout_form', 'einstiegswahl_select');
function einstiegswahl_select() {

    foreach ( WC()->cart->get_cart() as $cart_item ) {
        $item = $cart_item['data'];
        if(!empty($item)){
            $product = new WC_product($item->id);

            //$pd_numbers = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true );      

            $list_items['choices'] = array();   

            $list_items = get_post_meta( $cart_item[ 'product_id' ], '_einstiegsorte', true );
            if($list_items){
                 $list_items = explode("\n", $list_items);
                    echo '<select name=einstiegswahl>';
                foreach($list_items as $list_item) {
                    echo '<option>' . $list_item . '</option>';
                }
            echo '</select>';
            }}}}

【问题讨论】:

    标签: php wordpress woocommerce checkout custom-fields


    【解决方案1】:

    试试这个:

    add_action('woocommerce_before_checkout_form', 'display_einstieg_meta');
    function display_einstieg_meta() {
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            echo get_post_meta( $cart_item['data']->get_id(), '_einstiegsorte', true ); 
        }
    }
    

    它现在应该可以工作了。注意:$cart_item['data'] 已经是 WC_Product 对象实例...


    所以您可以在购物车中有很多商品,您的选择字段代码将是:

    add_action( 'woocommerce_before_checkout_billing_form', 'custom_einstiegswahl');
    function custom_einstiegswahl(){
    
        echo '<select name=einstiegswahl>';
    
        // Loop through cart items
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            // Get the custom field data
            $einstiegsorte = get_post_meta( $cart_item['data']->get_id(), '_einstiegsorte', true );
            if( ! empty($einstiegsorte) ){
                // if it's multiline we split it in an array
                $select_field_items = explode( '\n', $einstiegsorte );
                // If the array has more than one item
                if( sizeof( $select_field_items ) > 1 ){
                    foreach( $select_field_items as $value )
                        echo '<option value="'. $value .'">' . $value . '</option>';
                } 
                // If there is only one line
                else {
                    // we clean it
                    $value = str_replace('\n', '', $einstiegsorte);
                    echo '<option value="'. $value .'">' . $value . '</option>';
                }
            }
        }
    
        echo '</select>';
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 1970-01-01
      • 2018-07-21
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多