Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions includes/class-donations.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static function init() {
add_action( 'wp_loaded', [ __CLASS__, 'process_donation_form' ], 99 );
add_action( 'woocommerce_checkout_update_order_meta', [ __CLASS__, 'woocommerce_checkout_update_order_meta' ] );
add_filter( 'woocommerce_billing_fields', [ __CLASS__, 'woocommerce_billing_fields' ] );
add_filter( 'pre_option_woocommerce_enable_guest_checkout', [ __CLASS__, 'disable_guest_checkout' ] );
add_filter( 'amp_skip_post', [ __CLASS__, 'should_skip_amp' ], 10, 2 );
}
}
Expand Down Expand Up @@ -766,6 +767,22 @@ public static function woocommerce_billing_fields( $form_fields ) {
return $form_fields;
}

/**
* If Reader Activation is enabled, the reader will be registered upon donation.
* Disable the guest checkout option in the checkout form.
*
* @param string $value Value of the guest checkout option from WC settings. Can be 'yes' or 'no'.
*
* @return string Filtered value.
*/
public static function disable_guest_checkout( $value ) {
if ( Reader_Activation::is_enabled() ) {
$value = 'no';
}

return $value;
}

/**
* Update WC order with the client id from hidden form field.
*
Expand Down
49 changes: 35 additions & 14 deletions includes/reader-revenue/class-woocommerce-connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class WooCommerce_Connection {
* @codeCoverageIgnore
*/
public static function init() {
add_action( 'admin_init', [ __CLASS__, 'disable_woocommerce_setup' ] );
\add_action( 'admin_init', [ __CLASS__, 'disable_woocommerce_setup' ] );
\add_action( 'woocommerce_checkout_order_created', [ __CLASS__, 'register_reader' ] );
}

/**
Expand Down Expand Up @@ -55,6 +56,22 @@ private static function get_donation_order_item( $frequency, $amount = 0 ) {
return $item;
}

/**
* Ensure that donors are registered.
*
* @param WC_Order $order Order object.
*/
public static function register_reader( $order ) {
if ( Reader_Activation::is_enabled() ) {
$email_address = $order->get_billing_email();
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$full_name = "$first_name $last_name";

Reader_Activation::register_reader( $email_address, $full_name );
}
}

/**
* If the site is using woocommerce-memberships, create a new user and a
* membership, if the donation type calls for it.
Expand All @@ -73,7 +90,7 @@ public static function set_up_membership( $email_address, $full_name, $frequency
$membership_plans = $wc_memberships_membership_plans->get_membership_plans();
$order_items = [ self::get_donation_order_item( $frequency ) ];
foreach ( $membership_plans as $plan ) {
$access_granting_product_ids = wc_memberships_get_order_access_granting_product_ids( $plan, '', $order_items );
$access_granting_product_ids = \wc_memberships_get_order_access_granting_product_ids( $plan, '', $order_items );
if ( ! empty( $access_granting_product_ids ) ) {
$should_create_account = true;
break;
Expand All @@ -82,24 +99,28 @@ public static function set_up_membership( $email_address, $full_name, $frequency
if ( $should_create_account ) {
if ( Reader_Activation::is_enabled() ) {
$user_id = Reader_Activation::register_reader( $email_address, $full_name );
if ( is_wp_error( $user_id ) ) {
if ( \is_wp_error( $user_id ) ) {
return $user_id;
}
if ( ! absint( $user_id ) ) {
$user_id = null;
}
} else {
Logger::log( 'This order will result in a membership, creating account for user.' );
$user_login = sanitize_title( $full_name );
$user_id = wc_create_new_customer( $email_address, $user_login, '', [ 'display_name' => $full_name ] );
if ( is_wp_error( $user_id ) ) {
return $user_id;
}

// Log the new user in.
wp_set_current_user( $user_id, $user_login );
wp_set_auth_cookie( $user_id );
return $user_id;
}

Logger::log( 'This order will result in a membership, creating account for user.' );
$user_login = \sanitize_title( $full_name );
$user_id = \wc_create_new_customer( $email_address, $user_login, '', [ 'display_name' => $full_name ] );

if ( is_wp_error( $user_id ) ) {
return $user_id;
}

// Log the new user in.
\wp_set_current_user( $user_id, $user_login );
\wp_set_auth_cookie( $user_id );

return $user_id;
}
}
Expand All @@ -116,7 +137,7 @@ public static function create_transaction( $order_data ) {

$item = self::get_donation_order_item( $frequency, $order_data['amount'] );
if ( false === $item ) {
return new WP_Error( 'newspack_woocommerce', __( 'Missing donation product.', 'newspack' ) );
return new \WP_Error( 'newspack_woocommerce', __( 'Missing donation product.', 'newspack' ) );
}

$order = wc_create_order();
Expand Down