【问题标题】:WooCommerce custom place order validationWooCommerce 自定义下订单验证
【发布时间】:2016-05-07 00:23:44
【问题描述】:

当用户在 Woocommerce 中提交订单时,我想做一些额外的检查。尝试过使用不同的 add_filter 方法,但似乎我无法连接到 place_order 过滤器。我试过了:

add_filter('woocommerce_place_order', 'checkFields'); //on ordersubmit
function checkFields(){
  if($this != true){
    //not allowed to place order because of data is not true
  }else{
   //continue order
  }
}

但是当我提交结帐表单时,不会触发上述任何一项。 有什么建议? :)

【问题讨论】:

  • 你到底想钩什么?您是要向数据库中插入一些数据还是只是验证一些数据?
  • 我正在尝试验证插入的额外数据是真还是假。如果为 false,用户将无法提交结帐和下订单。基本上这只是一个关于如何挂钩结帐提交的问题:)
  • 没有亲自尝试过,但如果您正在做一些数据验证,我相信您可以在woocommerce_add_ 上运行过滤器,如woocommerce_add_error 并仅在结帐页面上加载您的功能docs.woothemes.com/wc-apidocs/function-wc_add_notice.html
  • 你试过过滤器了吗?如果您在错误时运行过滤器,它显然不会下订单并添加错误通知
  • @simond 选择付款方式并点击“下订单”后,您是否正在尝试做某事?

标签: wordpress woocommerce checkout add-filter


【解决方案1】:

试试这样的..

add_action('woocommerce_after_checkout_validation', 'rei_after_checkout_validation');

function rei_after_checkout_validation( $posted ) {

    // do all your logics here...
    // adding wc_add_notice with a second parameter of "error" will stop the form...
    // wc_add_notice( __( "OMG! You're not human!", 'woocommerce' ), 'error' );

    if (empty($_POST['captcha'])) {
         wc_add_notice( __( "Captcha is empty!", 'woocommerce' ), 'error' );
    }

}

$posted 是这样的数组..

Array
(
    [terms] => 0
    [createaccount] => 0
    [payment_method] => cheque
    [shipping_method] => 
    [ship_to_different_address] => 
    [billing_first_name] => 
    [billing_last_name] => 
    [billing_company] => 
    [billing_email] => iamhuman@gmail.com
    [billing_phone] => 
    [billing_country] => PH
    [billing_address_1] => 
    [billing_address_2] => 
    [billing_city] => 
    [billing_state] => 
    [billing_postcode] => 
    [order_comments] => 
)

【讨论】:

  • 太棒了!非常感谢,你摇滚! :-)
  • 这对我不起作用?我需要做额外的事情吗?
【解决方案2】:

你可以试试专用的动作钩子:

woocommerce_checkout_process

add_action('woocommerce_checkout_process', 'validate_course_enrolled');

function validate_course_enrolled(){
$cart_items_count = WC()->cart->get_cart_contents_count();
$total_count = $cart_items_count + $quantity;

if( $cart_items_count >= 2 ){
    // Set to false
    $passed = false;
    // Display a message
     wc_add_notice( __( "You can’t buy more than one course", "woocommerce" ), "error" );
}
return $passed;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 2016-03-03
    • 2020-01-11
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多