【问题标题】:Adding custom WooCommerce dropdown checkout field from a CTP从 CTP 添加自定义 WooCommerce 下拉结帐字段
【发布时间】:2022-01-20 06:44:08
【问题描述】:

我正在尝试从我的自定义帖子类型“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;
}

【问题讨论】:

    标签: php wordpress woocommerce checkout


    【解决方案1】:
    • 由于我不使用您使用的自定义帖子类型,因此我的答案基于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 );
    

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2015-10-08
      相关资源
      最近更新 更多