-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fixes and improvements for WooCommerce Subscriptions Gifting (#…
…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
Showing
5 changed files
with
90 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
79 changes: 79 additions & 0 deletions
79
includes/plugins/woocommerce-subscriptions/class-woocommerce-subscriptions-gifting.php
This file contains 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
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(); |
This file contains 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
This file contains 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
This file contains 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