在WooCommerce购买特定产品时强制注册

时间:2017-10-17 17:28:49

标签: php wordpress woocommerce


我有一个带有woocommerce商店的wordpress网站,我想强制用户在购买特定产品时注册 我该怎么做?
感谢

1 个答案:

答案 0 :(得分:0)

以下是您的查询代码。

add_action( 'woocommerce_after_checkout_validation' , 'restict_registration_for_some_products', 10, 2 );
function restict_registration_for_some_products( $data, $errors ) {

    if( isset( $data['createaccount'] ) && !$data['createaccount'] ) {

        $retricted_ids = get_resticted_product_ids();

        if( isset( $retricted_ids ) && $retricted_ids != null ) {

            $cart_content = WC()->cart->get_cart_contents();

            $cart_ids = wp_list_pluck( $cart_content, 'product_id' );
            $cart_ids = array_values( $cart_ids );
            $common_ids = array_intersect( $retricted_ids, $cart_ids );

            if( isset( $common_ids ) && $common_ids != null ) {
                $errors->add( 'account_registration', __( 'You are not allowed to purchase these products without creating an account.', 'text-domain' ) );
            }
        }
    }
}
function get_resticted_product_ids() {
    //specific product ids
    return array(110,96,70);
}