在结帐自定义字段中检索产品自定义字段值

Retrieve product custom field values in checkout custom fields

我在单个产品页面中有一个表单,用户可以在其中插入他的信息(姓名、phone、国家/地区、年龄...),我需要做的是在账单明细中显示此信息。

所以,我所做的是在单个产品的表单中添加了 2 个字段,代码如下:

<div class="col-md-6">
                <div class="form-group">
                    <input class="form-control alt" name="billing_vol" type="text"
                           placeholder="<?php esc_html_e( 'N° Vol : *', 'rentcar' ); ?>" >
                </div>
            </div>
            <div class="form-group">
                <input class="form-control alt" type="text" name="billing_cli_age"
                       placeholder="<?php esc_html_e( 'Age *', 'rentcar' ); ?>"
                     "
                >
            </div><div class="col-md-6">
                <div class="form-group">
                    <?php 
                        global $woocommerce;
                        $countries_obj   = new WC_Countries();
                        $countries   = $countries_obj->__get('countries');
                    ?>

                    <?php 
                        woocommerce_form_field('billing_country', array(
                        'type'       => 'select',
                        'class'      => array( 'chzn-drop' ),
                        'placeholder'    => __('Country'),
                        'options'    => $countries
                        )
                        );
                    ?>

                </div>
            </div>

然后我做了同样的事情在结算页面的结算明细表中,我添加了这 2 个字段。这是代码:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) {

    $fields['billing']['billing_cli_age'] = array(
        'label'         => __('Age ', 'woocommerce'),
        'placeholder'   => _x('Age ', 'placeholder', 'woocommerce'),
        'required'      => false,
        'class'         => array('form-row-wide'),
        'clear'         => true
    );

    $fields['billing']['billing_vol'] = array(
        'label'         => __('N° vol', 'woocommerce'),
        'placeholder'   => _x('N° vol', 'placeholder', 'woocommerce'),
        'required'      => false,
        'class'         => array('form-row-wide'),
        'clear'         => true
    );
    return $fields;
}

现在我卡住了,我不知道如何将用户在表单中添加的信息(我在单个产品中有)发送到结帐页面中的账单明细表单。

我怎样才能做到这一点?

谢谢。

这是获取产品自定义字段的完整功能代码,当添加到购物车将填写我们的结帐自定义字段时,这些字段提交了值。

第一个功能只是为了我的测试目的。但是您的产品自定义字段代码应该在添加到购物车表单中,才能正常工作。

add_action( 'woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button',20 );
function action_before_add_to_cart_button() {
    ?>
        <div class="col-md-6">
            <div>
                <input class="form-control alt" name="billing_vol" type="text"
                       placeholder="<?php esc_html_e( 'N° Vol : *', 'rentcar' ); ?>">
            </div>
            <div>
                <input class="form-control alt" type="text" name="billing_cli_age"
                       placeholder="<?php esc_html_e( 'Age *', 'rentcar' ); ?>">
            </div>
            <div class="form-group">
            <?php
                // Getting the countries (simplified)
                $countries   = WC()->countries->get_countries();

                woocommerce_form_field('billing_country', array(
                    'type'       => 'select',
                    'class'      => array( 'chzn-drop' ),
                    'placeholder'    => __('Country'),
                    'options'    => $countries
                ) );
            ?>
            </div>
        </div>
    <?php
}

这是缺少的挂钩函数,它将传递到购物车您的产品自定义字段值:

// Store the data fields to cart
add_filter( 'woocommerce_add_cart_item_data', 'action_save_my_custom_product_fields', 10, 2 );
function action_save_my_custom_product_fields( $cart_item_data, $product_id ) {
    $bool = false;
    $data = array();
    if( isset( $_REQUEST['billing_vol'] ) ) {
        $cart_item_data['custom_data']['billing_vol'] = $_REQUEST['billing_vol'];
        $data['billing_vol'] = $_REQUEST['billing_vol'];
        $bool = true;
    }
    if( isset( $_REQUEST['billing_cli_age'] ) ) {
        $cart_item_data['custom_data']['billing_cli_age'] = $_REQUEST['billing_cli_age'];
        $data['billing_cli_age'] = $_REQUEST['billing_cli_age'];
        $bool = true;
    }
    if( isset( $_REQUEST['billing_country'] ) ) {
        $cart_item_data['custom_data']['billing_country'] = $_REQUEST['billing_country'];
        $data['billing_country'] = $_REQUEST['billing_country'];
        $bool = true;
    }
    if( $bool ) {
        // below statement make sure every add to cart action as unique line item
        $unique_key = md5( microtime().rand() );
        $cart_item_data['custom_data']['unique_key'] = $unique_key;
        $data['unique_key'] = $unique_key;
        WC()->session->set( 'custom_data', $data );
    }
    return $cart_item_data;
}

由于您可以有多个购物车商品,因此您需要打破循环,只获取第一个商品的自定义值:

add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {

    // We break the loop to get the custom data from 1 cart item
    foreach(WC()->cart->get_cart() as $cart_item) break;

    $data = $cart_item['custom_data'];

    // COUNTRIES: Getting the countries (simplified)
    $countries   = WC()->countries->get_countries();
    $billing_country_key = $data['billing_country'];
    $billing_country_value = $countries[$billing_country_key];

    $fields['billing']['billing_cli_age'] = array(
        'type'          => 'text',
        'label'         => __('Age ', 'woocommerce'),
        'placeholder'   => _x('Age ', 'placeholder', 'woocommerce'),
        'required'      => false,
        'class'         => array('form-row-wide'),
        'clear'         => true,
        'default'       => $data['billing_cli_age'],
    );

    $fields['billing']['billing_vol'] = array(
        'type'          => 'text',
        'label'         => __('N° vol', 'woocommerce'),
        'placeholder'   => _x('N° vol', 'placeholder', 'woocommerce'),
        'required'      => false,
        'class'         => array('form-row-wide'),
        'clear'         => true,
        'default'       => $data['billing_vol'],
    );

    $fields['billing']['billing_country'] = array(
        'type' => 'select',
        'default' => $billing_country_key,
        'options' => array($billing_country_key => $billing_country_value),
    );

    return $fields;
}

代码在您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码已经过测试并且有效。

For country field is quiet complicated to get it work as Javascript/Ajax is also involved in it... With a unique "select" "option" it works, but with multiple select options it doesn't.