Integrate CPT fields into Woocommerce checkout pag

2019-09-03 08:44发布

问题:

I need help integrating Custom Post types into Woocommerce checkout page - I need to populate all the custom fields related to a CPT based on the selection made from the selct field - which populates CPT cars for the user logged in -

 woocommerce_form_field( 'car_sel', array(
    'type'          => 'select',
    'class'         => 'populate-cars',
    'label'         => __('Car for this Event'),
    'options' => my_acf_load_field( array($carname) ),
    ), $checkout->get_value( 'car_sel' ));
function my_acf_load_field( $field )
 {

  get_currentuserinfo();
$id = get_current_user_id();

$args = array(
    'author'       => $id,
    'posts_per_page'   => -1,
    'orderby'          => 'post_title',
    'order'            => 'ASC',
    'post_type'        => 'car',
    'post_status'      => 'publish' );

$posts = get_posts( $args );

foreach ( $posts as $post ) {
    $carname[$post->post_title] = $post->post_title;

}


// Important: return the field
return $carname;

}

Now I need to populate fields like make, model & cc into Woocommerce field and pass them onto the email - I also want to change the variables depending on the select - So I need onchange functionality

Can anyone help me? Struggling to get meta values based on post title - nothing seems to be working :(

回答1:

I have written very extensively about customizing the WooCommerce checkout. Here's the first part (displaying the form field on the checkout adapted to your select options. Depending on if you start getting really long lists of cars, you may want to look into the Transient API to cache the $posts values. Also for reference, wp_list_pluck() is awesome.

function kia_get_car_names_checkout_options(){

    $args = array(
        'author'           => get_current_user_id(),
        'posts_per_page'   => -1,
        'orderby'          => 'post_title',
        'order'            => 'ASC',
        'post_type'        => 'car',
        'post_status'      => 'publish' );

    $posts = get_posts( $args );

    $car_names = wp_list_pluck( $posts, 'post_title', 'ID' ); 

    return $car_names;
}

// Add a new checkout field
function kia_filter_checkout_fields($fields){
    $fields['extra_fields'] = array(
            'another_field' => array(
                'type' => 'select',
                'options' => kia_get_car_names_checkout_options(),
                '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' );