Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个if语句,它们分别工作,但不在一起,PHP_Php_Wordpress_Woocommerce_Conditional Statements_Custom Fields - Fatal编程技术网

两个if语句,它们分别工作,但不在一起,PHP

两个if语句,它们分别工作,但不在一起,PHP,php,wordpress,woocommerce,conditional-statements,custom-fields,Php,Wordpress,Woocommerce,Conditional Statements,Custom Fields,我在WordPress和Woo commerce的一个分机中工作。我在结帐页面上有两个自定义字段,当用户输入值存在于数据库中时,这两个字段都是保存和报告通知,当数据库中不存在时,这两个字段都是。这两个字段各自工作,但当这两个字段都处于活动状态时,无论输入哪个字段,只有第一个条件会引发错误 我正在使用的代码 // process the checkout function jm_custom_checkout_field_process() { // Access the dat

我在WordPress和Woo commerce的一个分机中工作。我在结帐页面上有两个自定义字段,当用户输入值存在于数据库中时,这两个字段都是保存和报告通知,当数据库中不存在时,这两个字段都是。这两个字段各自工作,但当这两个字段都处于活动状态时,无论输入哪个字段,只有第一个条件会引发错误

我正在使用的代码

// process the checkout
function jm_custom_checkout_field_process() {

        // Access the database 
        global $wpdb; 

        // query the database
        $meta_key = '_create_new_group';
        $groupnames = $wpdb->get_col($wpdb->prepare("
            SELECT meta_value 
            FROM $wpdb->postmeta 
            WHERE meta_key = %s" 
            ,$meta_key
        ));

        // get value from user
        $newgroupname = $_POST['create_new_group'];

        // get value from user
        $existing_groupname = $_POST['add_to_existing_group'];

        // check if user input is in array
        if( in_array($newgroupname, $groupnames ) ) {
           wc_add_notice( __( 'Group name already taken.' ), 'error' );
        }
        // check if user input is in array 
        if( ! in_array($existing_groupname, $groupnames ) ) {
            wc_add_notice( __( 'Group name does not exist.' ), 'error' );
        }
}
add_action('woocommerce_checkout_process', 'jm_custom_checkout_field_process');


// save the extra field when checkout is processed
function jm_save_extra_checkout_fields( $order_id, $posted ){

    // Check if there is user input and save
    if( isset( $posted['add_to_existing_group'] ) ) {
        update_post_meta( $order_id, '_create_new_group', sanitize_text_field( $posted['add_to_existing_group'] ) );            
        }    

        // Check if there is user input and save
    if( isset( $posted['create_new_group'] ) ) {
        update_post_meta( $order_id, '_create_new_group', sanitize_text_field( $posted['create_new_group'] ) );//
    } 
}
add_action( 'woocommerce_checkout_update_order_meta', 'jm_save_extra_checkout_fields', 10, 2 );

有人有什么建议吗?

假设调用
wc\u add\u notice()
会导致退出执行路径,您可以尝试以下操作:

$blnShowFirstNotice = false;
$blnShowSecondNotice = false;

// check if user input is in array
if ($newgroupname) 
{
    if( in_array($newgroupname, $groupnames ) ) 
    {
       $blnShowFirstNotice = true;
    }
}
// check if user input is in array 
if ($existing_groupname) 
{
    if( !in_array($existing_groupname, $groupnames ) ) 
    {
        $blnShowSecondNotice = true;
    }
}

if($blnShowFirstNotice==true && $blnShowSecondNotice==false)
{
    wc_add_notice( __( 'Group name already taken.' ), 'error' );
}
else if($blnShowFirstNotice==false && $blnShowSecondNotice==true)
{           
    wc_add_notice( __( 'Group name does not exist.' ), 'error' );
}
else if($blnShowFirstNotice==true && $blnShowSecondNotice==true)
{
    wc_add_notice( __( 'Group name already taken.' . 'Group name does not exist.' ), 'error' );
}

我在
wc\u add\u notice()
上找不到任何好的文档,但我打赌每个请求只允许一个通知。也许你只输出第一个通知?这也是我的想法,但至少
wc\u add\u notice
没有退出代码:据我所知,条件也在检查空输入?所以它抛出了一个错误,因为它在数据库中的特定键下找不到空值?@John EW,我真的不知道关于WooCommerce或wc_add_的任何信息……这只是我尝试绕过它的第一件事。你提供的很好,谢谢。我是新来的,这对我来说是相当多的东西,我知道这是类似的东西,但在这一点上无法达到。我更新了您的答案,因为代码需要检查是否有任何值输入,就是这样,它现在可以按照我希望的方式工作。再次感谢