将CPT字段集成到Woocommerce结帐页面 [英] Integrate CPT fields into Woocommerce checkout page

查看:94
本文介绍了将CPT字段集成到Woocommerce结帐页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将自定义帖子类型集成到Woocommerce结帐页面的帮助-我需要根据从selct字段中所做的选择填充与CPT相关的所有自定义字段-为登录的用户填充CPT汽车-

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;

}

现在我需要填充make,model& cc进入Woocommerce字段并将其传递到电子邮件中-我也想根据选择更改变量-所以我需要onchange功能

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 :(

推荐答案

我写了很多关于自定义WooCommerce结帐。这是第一部分(在已修改的结帐上显示表单字段)到您选择的选项。根据您是否开始获得一长串的汽车清单,您可能需要查看瞬态API 来缓存 $ posts 值。 wp_list_pluck() 很棒。

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' );

这篇关于将CPT字段集成到Woocommerce结帐页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆