使用 woocommerce 预订动态询问人名。?

我正在使用 woo commerce bookings 插件来预订旅行,但我想根据动态人数在结帐页面上单独获取每个人的类型名称。我发现堆栈溢出的帖子对我不起作用,它一直说未定义的偏移量 0 作为结帐页面上的错误。


//* Add a new checkout field

add_filter( 'woocommerce_checkout_fields', 'ppp_filter_checkout_fields' );

function ppp_filter_checkout_fields($fields){

$fields['extra_fields'] = array(

'participant_details' => array(

    'type' => 'participant_details',

    'required'      => false,

    'label' => __( 'Participant Details' )

    ),

);


// Add a "persons" hidden input field

foreach( WC()->cart->get_cart() as $cart_item ) {

    $persons = $cart_item['booking']['_persons'][0];

}

echo '<input type="hidden" name="persons" value="' . $persons . '">';


return $fields;

}


//* Add the field to the checkout

add_filter( 'woocommerce_form_field_participant_details', 'ppp_filter_checkout_field_group', 10, 4 );

function ppp_filter_checkout_field_group( $field, $key, $args, $value ){

$op_cart_count = WC()->cart->get_cart_contents_count();


$items = WC()->cart->get_cart();


foreach($items as $item) {

    $person = $item['booking']['_persons'][0];

}


if ($person > 1) {


    $html = '';


    $html .= "<h3>Deelnemers</h3>";


    for ( $i = 1; $i < $person; $i++) {

        $counter = $i + 1;


        $html .= "Deelnemer ". $counter . "<br>";


        $html .= woocommerce_form_field( "participant_details[$i][full_name]", array(

            "type" => "text",

            "return" => true,

            "value" => "",

            "required"      => true,

            "label" => __( "Naam" )

            )

        );

        $html .= woocommerce_form_field( "participant_details[$i][email_address]", array(

            "type" => "email",

            "return" => true,

            "value" => "",

            "required"      => true,

            "label" => __( "Emailadres" )

            )

        );


    }

    return $html;

}

}


这是我试图实现这一目标的网站。 网站


慕哥6287543
浏览 106回答 1
1回答

子衿沉夜

&nbsp;// Add checkout custom text fields&nbsp; &nbsp; &nbsp;add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_text_fields', 20, 1 );&nbsp; &nbsp;function add_checkout_custom_text_fields( $checkout) {$index = 0;// 1st Loop through cart itemsforeach(WC()->cart->get_cart() as $cart_item){&nbsp; &nbsp; $index++;&nbsp; &nbsp; // 2nd Loop through each unit related to item quantity&nbsp; &nbsp; for($i = 1; $i <= $cart_item['booking']['Adults']; $i++){&nbsp; &nbsp; &nbsp; &nbsp; woocommerce_form_field("Adult[$index][$i]", array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type' =>'text',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class'=>array('my-field-class form-row-wide'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label'=>__('Adult Name')." ($i)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'placeholder'=>__('Enter adult name'),&nbsp; &nbsp; &nbsp; &nbsp; ), $checkout->get_value("Adult[$index][$i]"));&nbsp; &nbsp; }&nbsp; &nbsp; for($i = 1; $i <= $cart_item['booking']['Childs']; $i++){&nbsp; &nbsp; &nbsp; &nbsp; woocommerce_form_field("Child[$index][$i]", array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'type' =>'text',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'class'=>array('my-field-class form-row-wide'),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'label'=>__('Child Name')." ($i)",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'placeholder'=>__('Enter child name'),&nbsp; &nbsp; &nbsp; &nbsp; ), $checkout->get_value("Child[$index][$i]"));&nbsp; &nbsp; }}&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP