Skip to content

Commit

Permalink
feat: fixes and improvements for WooCommerce Subscriptions Gifting (#…
Browse files Browse the repository at this point in the history
…3747)

* feat(wcsg): create new class for WCSG functionality

* fix(wcsg): require shipping fields if gifted shippable product

* chore: undo unnecessary change

* fix(wcsg): new gift recipients are without password

* test: fix failing unit test
  • Loading branch information
dkoo authored Feb 18, 2025
1 parent 5e78832 commit 49c4b35
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 28 deletions.
1 change: 1 addition & 0 deletions includes/class-newspack.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ private function includes() {
include_once NEWSPACK_ABSPATH . 'includes/plugins/wc-memberships/class-memberships.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-woocommerce.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/woocommerce-subscriptions/class-woocommerce-subscriptions.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/woocommerce-subscriptions/class-woocommerce-subscriptions-gifting.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-teams-for-memberships.php';
include_once NEWSPACK_ABSPATH . 'includes/plugins/class-newspack-elections.php';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* WooCommerce Subscriptions Gifting Integration class.
*
* @package Newspack
*/

namespace Newspack;

defined( 'ABSPATH' ) || exit;

/**
* Main class.
*/
class WooCommerce_Subscriptions_Gifting {
/**
* Initialize hooks and filters.
*/
public static function init() {
\add_filter( 'wcsg_new_recipient_account_details_fields', [ __CLASS__, 'new_recipient_fields' ] );
\add_filter( 'wcsg_require_shipping_address_for_virtual_products', '__return_false' );
\add_filter( 'default_option_woocommerce_subscriptions_gifting_gifting_checkbox_text', [ __CLASS__, 'default_gifting_checkbox_text' ] );
\add_filter( 'newpack_reader_activation_reader_is_without_password', [ __CLASS__, 'is_reader_without_password' ], 10, 2 );
}

/**
* Check if WooCommerce Subscriptions Gifting is active.
*
* @return bool
*/
public static function is_active() {
return function_exists( 'WC' ) && class_exists( 'WC_Subscriptions' ) && class_exists( 'WCS_Gifting' );
}

/**
* Ensure that only billing address fields enabled in Reader Revenue settings
* are required for new gift recipient accounts.
*
* See: https://github.com/woocommerce/woocommerce-subscriptions-gifting/blob/trunk/includes/class-wcsg-recipient-details.php#L275
*
* @param array $fields Address fields.
* @return array
*/
public static function new_recipient_fields( $fields ) {
// Escape hatch to force required shipping address for virtual products.
if ( apply_filters( 'wcsg_require_shipping_address_for_virtual_products', false ) ) { // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
return $fields;
}
$required_fields = Donations::get_billing_fields();
foreach ( $fields as $field_name => $field_config ) {
if ( 'shipping_' !== substr( $field_name, 0, 9 ) && ! in_array( 'billing_' . $field_name, $required_fields, true ) ) {
unset( $fields[ $field_name ] );
}
}
return $fields;
}

/**
* Filters the default text shown for the gifting checkbox during checkout.
*
* @return string
*/
public static function default_gifting_checkbox_text() {
return __( 'This purchase is a gift', 'newspack' );
}

/**
* New gift recipients don't yet have a password.
*
* @param bool $is_reader_without_password True if the reader has not set a password.
* @param int $user_id The user ID.
*
* @return bool
*/
public static function is_reader_without_password( $is_reader_without_password, $user_id ) {
return 'true' === get_user_meta( $user_id, 'wcsg_update_account', true );
}
}
WooCommerce_Subscriptions_Gifting::init();
9 changes: 8 additions & 1 deletion includes/reader-activation/class-reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,14 @@ public static function is_reader_without_password( $user_or_user_id ) {
return new \WP_Error( 'newspack_is_reader_without_password', __( 'Invalid user.', 'newspack-plugin' ) );
}

return (bool) \get_user_meta( $user->ID, self::WITHOUT_PASSWORD, false );
/**
* Filters whether the user should be considered a reader without a password.
*
* @param bool $is_reader_without_password True if the reader has not set a password.
* @param int $user_id User ID.
* @return bool
*/
return (bool) apply_filters( 'newpack_reader_activation_reader_is_without_password', \get_user_meta( $user->ID, self::WITHOUT_PASSWORD, false ), $user->ID );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class WooCommerce_My_Account {
*/
public static function init() {
\add_filter( 'woocommerce_account_menu_items', [ __CLASS__, 'my_account_menu_items' ], 1000 );
\add_filter( 'wcsg_new_recipient_account_details_fields', [ __CLASS__, 'new_recipient_fields' ] );
\add_filter( 'wcsg_require_shipping_address_for_virtual_products', '__return_false' );
\add_filter( 'woocommerce_default_address_fields', [ __CLASS__, 'required_address_fields' ] );
\add_filter( 'woocommerce_billing_fields', [ __CLASS__, 'required_address_fields' ] );
\add_filter( 'woocommerce_get_checkout_url', [ __CLASS__, 'get_checkout_url' ] );
Expand Down Expand Up @@ -127,7 +125,7 @@ public static function my_account_menu_items( $items ) {
unset( $shipping_address[ $ignored_field ] );
}

if ( empty( array_filter( $billing_address ) ) && empty( array_filter( $billing_address ) ) ) {
if ( empty( array_filter( $billing_address ) ) && empty( array_filter( $shipping_address ) ) ) {
$default_disabled_items[] = 'edit-address';
}

Expand Down Expand Up @@ -550,29 +548,6 @@ public static function required_address_fields( $fields ) {
return $fields;
}

/**
* Ensure that only billing address fields enabled in Reader Revenue settings
* are required for new gift recipient accounts.
*
* See: https://github.com/woocommerce/woocommerce-subscriptions-gifting/blob/trunk/includes/class-wcsg-recipient-details.php#L275
*
* @param array $fields Address fields.
* @return array
*/
public static function new_recipient_fields( $fields ) {
// Escape hatch to force required shipping address for virtual products.
if ( apply_filters( 'wcsg_require_shipping_address_for_virtual_products', false ) ) { // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
return $fields;
}
$required_fields = Donations::get_billing_fields();
foreach ( $fields as $field_name => $field_config ) {
if ( ! in_array( 'billing_' . $field_name, $required_fields, true ) ) {
unset( $fields[ $field_name ] );
}
}
return $fields;
}

/**
* WC's page templates hijacking.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/unit-tests/reader-activation.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function test_register_existing_reader() {
$user_id = self::register_sample_reader();
wp_logout();
$result = self::register_sample_reader(); // Reregister the same email.
$this->assertFalse( $result );
$this->assertTrue( is_wp_error( $result ) );
$this->assertFalse( is_user_logged_in() );
wp_delete_user( $user_id ); // Clean up.
}
Expand Down

0 comments on commit 49c4b35

Please sign in to comment.