Adding custom WooCommerce dropdown checkout field from a CTP(从CTP添加自定义WooCommerce下拉检出字段)
本文介绍了从CTP添加自定义WooCommerce下拉检出字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的自定义帖子类型fundraiser
";中的所有帖子列表向我的WooCommerce结账页面添加一个下拉列表。
我显示了选择字段,但未填充CPT帖子标题的选项。
我做错了什么?以下是我的代码:
add_action( 'woocommerce_after_order_notes', 'fundraiser_checkout_field' );
function fundraiser_checkout_field( $checkout )
{
$options = array();
$options[0] = "Please Select a Fundraiser";
$posts = array();
$args = array('post_type'=>'fundraiser', 'posts_per_page'=>-1,'order'=>'asc');
$query = New WP_Query($args);
if($query->have_posts()):while($query->have_posts()):$query->the_post();
$id = $posts['post_title'];
foreach($results as $result) {
$options[$result->id] = $result->nome;
}
endwhile;endif;wp_reset_postdata();
echo '<div id="fundraiser_checkout_field"><h2>' . __('Fundraiser') . '</h2>';
woocommerce_form_field( 'fundraiser_field', array(
'type' => 'select',
'class' => array('fundraiser form-row-wide'),
'label' => __('Select a Fundraiser'),
'required' => true,
'options' => $options,
), $checkout->get_value( 'fundraiser_field' ) );
echo '</div>';
;
return $checkout;
}
推荐答案
由于我没有使用您使用的自定义帖子类型,我的回答基于
post type product
,将$post_type = 'product';
替换为$post_type = 'fundraiser';
您的代码包含一些小错误,例如
while
循环中不需要foreach
function fundraiser_checkout_field( $checkout ) {
// Empty array
$options = array();
// First value
$options[0] = __( 'Please Select a Fundraiser', 'woocommerce' );
// Post type
$post_type = 'product';
// Args
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'order' => 'asc'
);
// Query args
$query = New WP_Query($args);
// WP_Query loop
if ( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
$options[ $query->post->ID ] = $query->post->post_title;
endwhile;
wp_reset_postdata();
endif;
// Output
echo '<div id="fundraiser_checkout_field"><h2>' . __( 'Fundraiser', 'woocommerce' ) . '</h2>';
woocommerce_form_field( 'fundraiser_field', array(
'type' => 'select',
'class' => array('fundraiser form-row-wide'),
'label' => __( 'Select a Fundraiser', 'woocommerce' ),
'required' => true,
'options' => $options,
), $checkout->get_value( 'fundraiser_field' ) );
echo '</div>';
}
add_action( 'woocommerce_after_order_notes', 'fundraiser_checkout_field', 10, 1 );
这篇关于从CTP添加自定义WooCommerce下拉检出字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:从CTP添加自定义WooCommerce下拉检出字段


基础教程推荐
猜你喜欢
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01