Created
March 29, 2023 14:33
-
-
Save nfsarmento/c2218583f300a401e9f7c857f4573fb6 to your computer and use it in GitHub Desktop.
Force registering when buying specific products on WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* | |
* Force registering when buying specific products on WooCommerce | |
* | |
* */ | |
add_action( 'woocommerce_after_checkout_validation' , 'ns_restict_registration_for_some_products', 10, 2 ); | |
function ns_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', __( 'As a member you will need to create an account in order to access important membership information. Please scroll down and check the box "Create an account".', 'text-domain' ) ); | |
} | |
} | |
} | |
} | |
function get_resticted_product_ids() { | |
//specific product ids | |
return array(3439); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment