简体   繁体   English

woocommerce结帐if语句

[英]woocommerce checkout if statement

I'm trying to create a select box on my client's woocommerce checkout page on Wordpress that asks where the customer heard about the site. 我试图在Wordpress的客户的woocommerce结帐页面上创建一个选择框,以询问客户在何处听说过该网站。 I've got as far as creating it and am now trying to have a text box pop up when "other" is selected. 我已经尽力创建它了,现在尝试选择“其他”时弹出一个文本框。 So far, it's not happening. 到目前为止,这还没有发生。

Here's the code I have for the drop down and the text box at the moment: 这是我现在用于下拉列表和文本框的代码:

/**
* 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']));
}
}

I've read a few ways to do this when it's not woocommerce, but I can't get that to work either, so this seemed like the best approach - although I could be wrong. 当不是woocommerce时,我已经阅读了一些方法来执行此操作,但是我也无法使其正常工作,因此这似乎是最好的方法-尽管我可能会错。

I've also noticed that in the code on the site, when the placeholder is selected, it shows as <option value selected="selected"> but when I select the other options, that is still only shown on the placeholder, so I don't know if that has something to do with the problem either. 我还注意到,在网站上的代码中,选择占位符后,它显示为<option value selected="selected">但是当我选择其他选项时,它仍只显示在占位符上,所以我不知道这是否与问题有关。

$_POST['hear_about_us'] will only be set after the checkout page has been submitted, and the woocommerce_after_order_notes hook runs when the checkout page is loaded. $_POST['hear_about_us']仅在提交结帐页面后设置,并且在加载结帐页面时运行woocommerce_after_order_notes挂钩。 So you're adding how_did_other to the hook after it has already run. 因此,您已经在运行后将how_did_other添加到了该挂钩中。

The simplest way to do it is to include the other text box initially but hidden with style="display:none;" 最简单的方法是最初包含另一个文本框,但使用style="display:none;"隐藏该文本框style="display:none;" , and reveal it with javascript ,并使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM