Skip to content

Commit

Permalink
Override the place order button text on the block checkout with subsc…
Browse files Browse the repository at this point in the history
…ription-context overrides (#587)

* Override the place order button text on the block checkout with subscription-context overrides

* Add variable block comment for the cart_handlers property

* Add changelog entry

* Fix phpcs

* Fix assignment phpcs error

* Add missing , from last array element

* Remove ambiguous keys from the cart handler getter

* Add the resubscribe cart callback to the list of known place order button overrides
  • Loading branch information
james-allan authored Mar 28, 2024
1 parent 96e5ddd commit b7ff045
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
10 changes: 10 additions & 0 deletions assets/src/js/filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __, sprintf } from '@wordpress/i18n';
import { registerCheckoutFilters } from '@woocommerce/blocks-checkout';
import { getSetting } from '@woocommerce/settings';

/**
* Internal dependencies
Expand Down Expand Up @@ -120,5 +121,14 @@ export const registerFilters = () => {

return pricePlaceholder;
},
placeOrderButtonLabel: ( label ) => {
const subscriptionsData = getSetting( 'subscriptions_data' );

if ( subscriptionsData?.place_order_override ) {
return subscriptionsData?.place_order_override;
}

return label;
},
} );
};
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fix - Improved alignment of subscription product pricing fields on the edit product screen for consistency with other fields.
* Fix - Avoid setting empty meta keys on subscriptions when changing the customer's default payment method.
* Fix - Use a more scalable way to filter the orders admin list table by parent orders on HPOS stores.
* Fix - Resolved an issue that prevented subscription custom place order button labels from working on the block checkout.
* Update - Change the update all subscriptions checkbox displayed on the change payment method page to be enabled by default.

= 6.8.0 - 2024-02-08 =
Expand Down
42 changes: 39 additions & 3 deletions includes/class-wc-subscriptions-core-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ class WC_Subscriptions_Core_Plugin {
*/
protected static $instance = null;

/**
* An array of cart handler objects.
*
* Use @see WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( '{class}' ) to fetch a cart handler instance.
* eg WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Renewal' ).
*
* @var WCS_Cart_Renewal[]
*/
protected $cart_handlers = [];

/**
* Initialise class and attach callbacks.
*/
Expand Down Expand Up @@ -121,9 +131,9 @@ public function init() {
WCS_PayPal_Standard_Change_Payment_Method::init();
WC_Subscriptions_Tracker::init();
WCS_Upgrade_Logger::init();
new WCS_Cart_Renewal();
new WCS_Cart_Resubscribe();
new WCS_Cart_Initial_Payment();
$this->add_cart_handler( new WCS_Cart_Renewal() );
$this->add_cart_handler( new WCS_Cart_Resubscribe() );
$this->add_cart_handler( new WCS_Cart_Initial_Payment() );
WCS_Download_Handler::init();
WCS_Limiter::init();
WCS_Admin_System_Status::init();
Expand Down Expand Up @@ -319,6 +329,32 @@ public function get_gateways_handler_class() {
return 'WC_Subscriptions_Core_Payment_Gateways';
}

/**
* Gets the cart handler instance.
*
* @param string $class The class name of the cart handler. eg 'WCS_Cart_Renewal'.
* @return WCS_Cart_Renewal|null The cart handler instance or null if not found.
*/
public function get_cart_handler( $class ) {
if ( ! isset( $this->cart_handlers[ $class ] ) ) {
return null;
}

return $this->cart_handlers[ $class ];
}

/**
* Adds a cart handler instance.
*
* This is used to add cart handlers for different cart types. For example, renewal, resubscribe, initial, switch etc.
* To access a cart handler instance, use WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( $class ).
*
* @param WCS_Cart_Renewal $cart_handler An instance of a cart handler.
*/
protected function add_cart_handler( $cart_handler ) {
$this->cart_handlers[ get_class( $cart_handler ) ] = $cart_handler;
}

/**
* Registers Subscriptions order types.
*
Expand Down
33 changes: 33 additions & 0 deletions includes/class-wcs-blocks-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function get_editor_script_handles() {
public function get_script_data() {
return array(
'woocommerce-subscriptions-blocks' => 'active',
'place_order_override' => $this->get_place_order_button_text_override(),
);
}

Expand All @@ -97,4 +98,36 @@ protected function get_file_version( $file ) {
}
return \WC_Subscriptions_Core_Plugin::instance()->get_library_version();
}

/**
* Fetches the place order button text if it has been overridden by one of Woo Subscription's methods.
*
* @return string|null The overridden place order button text or null if it hasn't been overridden.
*/
protected function get_place_order_button_text_override() {
$default = null;
$order_button_text = $default;

// Check if any of our button text override functions (hooked onto 'woocommerce_order_button_text') change the default text.
$callbacks = [
[ 'WC_Subscriptions_Checkout', 'order_button_text' ],
[ \WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Renewal' ), 'order_button_text' ],
[ \WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Switch' ), 'order_button_text' ],
[ \WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Resubscribe' ), 'order_button_text' ],
];

foreach ( $callbacks as $callback ) {
if ( ! is_callable( $callback ) ) {
continue;
}

$order_button_text = call_user_func( $callback, $default );

if ( $order_button_text !== $default ) {
break;
}
}

return $order_button_text;
}
}

0 comments on commit b7ff045

Please sign in to comment.