Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 自定义购物车输入字段_Wordpress_Woocommerce - Fatal编程技术网

Wordpress 自定义购物车输入字段

Wordpress 自定义购物车输入字段,wordpress,woocommerce,Wordpress,Woocommerce,我想向woocommerce购物车页面添加额外的输入字段,但我发现购物车页面有一个钩子woocommerce\u continue\u to\u checkout 我还必须更新和获取自定义字段的值,我有问题: 1) 如何保存datepicker\u checkout输入字段 2) 当我调用insert\u input\u text\u to\u checkout函数从woocommerce\u procedue\u to\u checkouthook时,如何获取此字段的值 此代码仅用于向购物车页

我想向woocommerce购物车页面添加额外的输入字段,但我发现购物车页面有一个钩子
woocommerce\u continue\u to\u checkout

我还必须更新和获取自定义字段的值,我有问题:

1) 如何保存
datepicker\u checkout
输入字段

2) 当我调用
insert\u input\u text\u to\u checkout
函数从
woocommerce\u procedue\u to\u checkout
hook时,如何获取此字段的值

此代码仅用于向购物车页面添加额外内容:

add_action( 'woocommerce_proceed_to_checkout', 'insert_input_text_to_checkout' );
function insert_input_text_to_checkout($checkout)
{
    woocommerce_form_field( 'datepicker_checkout', array
    (
        'type'         => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'id'            => 'datepicker_checkout',
        'label'         => __('Select Date'),
        'placeholder'   => __('dd/mm/yyyy'),
        'required'     => true,
    )/*, how to get value of 'datepicker_checkout' ? */);
}
在我的教程中,这里有一个将字段添加到签出的示例

// Add a new checkout field
function kia_filter_checkout_fields($fields){
    $fields['extra_fields'] = array(
            'some_field' => array(
                'type' => 'text',
                'required'      => true,
                'label' => __( 'Some field' )
                ),
            'another_field' => array(
                'type' => 'select',
                'options' => array( 'a' => __( 'apple' ), 'b' => __( 'bacon' ), 'c' => __( 'chocolate' ) ),
                'required'      => true,
                'label' => __( 'Another field' )
                )
            );

    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );

// display the extra field on the checkout form
function kia_extra_checkout_fields(){ 

    $checkout = WC()->checkout(); ?>

    <div class="extra-fields">
    <h3><?php _e( 'Additional Fields' ); ?></h3>

    <?php 
    // because of this foreach, everything added to the array in the previous function will display automagically
    foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>

            <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

        <?php endforeach; ?>
    </div>

<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );

// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
    // don't forget appropriate sanitization if you are using a different field type
    if( isset( $posted['some_field'] ) ) {
        update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
    }
    if( isset( $posted['another_field'] ) && in_array( $posted['another_field'], array( 'a', 'b', 'c' ) ) ) {
        update_post_meta( $order_id, '_another_field', $posted['another_field'] );
    }
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );

// display the extra data on order recieved page and my-account order review
function kia_display_order_data( $order_id ){  ?>
    <h2><?php _e( 'Additional Info' ); ?></h2>
    <table class="shop_table shop_table_responsive additional_info">
        <tbody>
            <tr>
                <th><?php _e( 'Some Field:' ); ?></th>
                <td><?php echo get_post_meta( $order_id, '_some_field', true ); ?></td>
            </tr>
            <tr>
                <th><?php _e( 'Another Field:' ); ?></th>
                <td><?php echo get_post_meta( $order_id, '_another_field', true ); ?></td>
            </tr>
        </tbody>
    </table>
<?php }
add_action( 'woocommerce_thankyou', 'kia_display_order_data', 20 );
add_action( 'woocommerce_view_order', 'kia_display_order_data', 20 );


// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){  ?>
    <div class="order_data_column">
        <h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
        <?php 
            echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>';
            echo '<p><strong>' . __( 'Another field' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
    </div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
//添加新的签出字段
函数kia\u filter\u checkout\u字段($fields){
$fields['extra_fields']=数组(
“某些字段”=>数组(
'类型'=>'文本',
“必需”=>true,
'label'=>\('Some field')
),
“另一个_字段”=>数组(
'类型'=>'选择',
'options'=>array('a'=>。'apple'),'b'=>。'bacon'),'c'=>。'cocolate'),
“必需”=>true,
“标签”=>(另一个字段)
)
);
返回$fields;
}
添加_过滤器('woocommerce_checkout_fields'、'kia_filter_checkout_fields');
//在签出窗体上显示额外字段
函数kia_extra_checkout_fields(){
$checkout=WC()->checkout();?>

你想在结帐或购物车页面上做什么,以及如何首先清除我想在购物车页面上添加一个日期选择器字段。谢谢。有没有办法在购物车页面上保存/编辑这些自定义字段?我想用JavaScript和缓存/cookies来做这件事,但也许没有这个方法是真的。哎呀,我显然没有读你的问题v很抱歉,你可以把datepicker字段放在结帐中。这可能是最简单的方法。