自定义WooCommerce签出文件在验证时生成JSON错误

时间:2017-04-11 作者:Andyh81

我为WooCommerce签出创建了两个自定义必需的复选框。一切正常,但它突然开始显示以下错误
SyntaxError: Unexpected token < in JSON at position 4

我使用以下代码创建字段:

add_action(\'woocommerce_checkout_after_terms_and_conditions\', \'my_custom_checkout_field\');

function my_custom_checkout_field( $checkout ) {

woocommerce_form_field( 
    \'delivery\', array(
    \'type\'          => \'checkbox\',
    \'class\'         => array(\'input-checkbox\'),
    \'label\'         => __(\'Location outside major city areas may require additional business days for delivery.\'),
    \'required\'  => true,
    )

);

woocommerce_form_field( 
    \'customer\', array(
    \'type\'          => \'checkbox\',
    \'class\'         => array(\'input-checkbox\'),
    \'label\'         => __(\'If the customer provides a work or home address of others instead of her/his own address, the Company will NOT be responsible if any problem occurs when delivering the goods.\'),
    \'required\'  => true,
    )

);}
以下代码用于处理新签出字段:

add_action(\'woocommerce_checkout_process\', \'my_custom_checkout_field_process\');

  function my_custom_checkout_field_process() {

if (!$_POST[\'delivery\']){
    wc_add_notice( sprintf(__(\'You must accept Additional Shipping Conditions.\', \'themename\')) ,\'error\'  );
}    
if (!$_POST[\'customer\']){
    wc_add_notice( sprintf(__(\'You must accept responsibility for address misplacement.\', \'themename\')) ,\'error\'  );
}
}
它一定与$\\u POST有关,因为如果我删除if语句,它将显示正常的错误消息,而不是JSON错误。

任何想法都会很好。

1 个回复
最合适的回答,由SO网友:Andyh81 整理而成

我想出来了。修复的是我需要在条件中使用isset。

旧代码:

if (!$_POST[\'delivery\']){
    wc_add_notice( sprintf(__(\'You must accept Additional Shipping Conditions.\', \'themename\')) ,\'error\'  );
} 

if (!$_POST[\'customer\']){
    wc_add_notice( sprintf(__(\'You must accept responsibility for address misplacement.\', \'themename\')) ,\'error\'  );
}
使用isset更正代码:

if (!isset($_POST[\'delivery\'])){
    wc_add_notice( sprintf(__(\'You must accept Additional Shipping Conditions.\', \'themename\')) ,\'error\'  );
} 

if (!isset($_POST[\'customer\'])){
    wc_add_notice( sprintf(__(\'You must accept responsibility for address misplacement.\', \'themename\')) ,\'error\'  );
}

相关推荐