[英]woocommerce checkout if statement
我試圖在Wordpress的客戶的woocommerce結帳頁面上創建一個選擇框,以詢問客戶在何處聽說過該網站。 我已經盡力創建它了,現在嘗試選擇“其他”時彈出一個文本框。 到目前為止,這還沒有發生。
這是我現在用於下拉列表和文本框的代碼:
/**
* Add how did you hear about us field to the checkout
**/
add_action('woocommerce_after_order_notes', 'how_did_you_hear');
function how_did_you_hear( $checkout ) {
echo '<div id="how_did_you_hear">';
woocommerce_form_field( 'hear_about_us', array(
'type' => 'select',
'class' => array('how_did_you_hear_about_us form-row-wide'),
'label' => __('How did you hear about us?'),
'required' => true,
'onChange' =>'howdidOnchange();',
'options' => array(
'' => __('-- Choose an option --', 'woocommerce'),
'Google' => __('Google', 'woocommerce'),
'Facebook' => __('Facebook', 'woocommerce'),
'Twitter' => __('Twitter', 'woocommerce'),
'Mother & Baby Magazine' => __('Mother & Baby Magazine', 'woocommerce'),
'eBay' => __('eBay', 'woocommerce'),
'Count the Kicks' => __('Count the Kicks', 'woocommerce'),
'Other (please specify)' => __('Other (please specify)', 'woocommerce'))
), $checkout->get_value( 'hear_about_us' ));
echo '</div>';
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'how_did_you_hear_process');
function how_did_you_hear_process() {
global $woocommerce;
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'how_did_you_hear_update_order_meta');
function how_did_you_hear_update_order_meta( $order_id ) {
if ($_POST['hear_about_us']) update_post_meta( $order_id, 'How did you hear about us?', esc_attr($_POST['hear_about_us']));
}
//Add text box if other is selected
if ($_POST['hear_about_us'] == 'Other (please specify)') {
add_action('woocommerce_after_order_notes', 'how_did_other');
function how_did_other( $checkout ) {
echo '<div id="how_did_other">';
woocommerce_form_field( 'other_please_specify', array(
'type' => 'text',
'class' => array('other-please-specify form-row-wide'),
'label' => __('Other (please specify)'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'other_please_specify' ));
echo '</div>';
}
/**
* Process the checkout
**/
add_action('woocommerce_checkout_process', 'how_did_other_process');
function how_did_other_process() {
global $woocommerce;
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'how_did_other_update_order_meta');
function how_did_other_update_order_meta( $order_id ) {
if ($_POST['other_please_specify']) update_post_meta( $order_id, 'Other (please specify)', esc_attr($_POST['other_please_specify']));
}
}
當不是woocommerce時,我已經閱讀了一些方法來執行此操作,但是我也無法使其正常工作,因此這似乎是最好的方法-盡管我可能會錯。
我還注意到,在網站上的代碼中,選擇占位符后,它顯示為<option value selected="selected">
但是當我選擇其他選項時,它仍只顯示在占位符上,所以我不知道這是否與問題有關。
$_POST['hear_about_us']
僅在提交結帳頁面后設置,並且在加載結帳頁面時運行woocommerce_after_order_notes
掛鈎。 因此,您已經在運行后將how_did_other
添加到了該掛鈎中。
最簡單的方法是最初包含另一個文本框,但使用style="display:none;"
隱藏該文本框style="display:none;"
,並使用javascript進行顯示
var selectInput = $('.how_did_you_hear_about_us');
var otherInput = $('.other-please-specify');
selectInput.change(function() {
if(selectInput.val()==='Other (please specify)') {
otherInput.show();
} else {
otherInput.hide();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.