Skip to content

Instantly share code, notes, and snippets.

@nfsarmento
Created March 29, 2023 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nfsarmento/c2218583f300a401e9f7c857f4573fb6 to your computer and use it in GitHub Desktop.
Save nfsarmento/c2218583f300a401e9f7c857f4573fb6 to your computer and use it in GitHub Desktop.
Force registering when buying specific products on WooCommerce
<?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