Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit b7ff045

Browse files
authored
Override the place order button text on the block checkout with subscription-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
1 parent 96e5ddd commit b7ff045

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

assets/src/js/filters/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { __, sprintf } from '@wordpress/i18n';
55
import { registerCheckoutFilters } from '@woocommerce/blocks-checkout';
6+
import { getSetting } from '@woocommerce/settings';
67

78
/**
89
* Internal dependencies
@@ -120,5 +121,14 @@ export const registerFilters = () => {
120121

121122
return pricePlaceholder;
122123
},
124+
placeOrderButtonLabel: ( label ) => {
125+
const subscriptionsData = getSetting( 'subscriptions_data' );
126+
127+
if ( subscriptionsData?.place_order_override ) {
128+
return subscriptionsData?.place_order_override;
129+
}
130+
131+
return label;
132+
},
123133
} );
124134
};

changelog.txt

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

1516
= 6.8.0 - 2024-02-08 =

includes/class-wc-subscriptions-core-plugin.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ class WC_Subscriptions_Core_Plugin {
4646
*/
4747
protected static $instance = null;
4848

49+
/**
50+
* An array of cart handler objects.
51+
*
52+
* Use @see WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( '{class}' ) to fetch a cart handler instance.
53+
* eg WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Renewal' ).
54+
*
55+
* @var WCS_Cart_Renewal[]
56+
*/
57+
protected $cart_handlers = [];
58+
4959
/**
5060
* Initialise class and attach callbacks.
5161
*/
@@ -121,9 +131,9 @@ public function init() {
121131
WCS_PayPal_Standard_Change_Payment_Method::init();
122132
WC_Subscriptions_Tracker::init();
123133
WCS_Upgrade_Logger::init();
124-
new WCS_Cart_Renewal();
125-
new WCS_Cart_Resubscribe();
126-
new WCS_Cart_Initial_Payment();
134+
$this->add_cart_handler( new WCS_Cart_Renewal() );
135+
$this->add_cart_handler( new WCS_Cart_Resubscribe() );
136+
$this->add_cart_handler( new WCS_Cart_Initial_Payment() );
127137
WCS_Download_Handler::init();
128138
WCS_Limiter::init();
129139
WCS_Admin_System_Status::init();
@@ -319,6 +329,32 @@ public function get_gateways_handler_class() {
319329
return 'WC_Subscriptions_Core_Payment_Gateways';
320330
}
321331

332+
/**
333+
* Gets the cart handler instance.
334+
*
335+
* @param string $class The class name of the cart handler. eg 'WCS_Cart_Renewal'.
336+
* @return WCS_Cart_Renewal|null The cart handler instance or null if not found.
337+
*/
338+
public function get_cart_handler( $class ) {
339+
if ( ! isset( $this->cart_handlers[ $class ] ) ) {
340+
return null;
341+
}
342+
343+
return $this->cart_handlers[ $class ];
344+
}
345+
346+
/**
347+
* Adds a cart handler instance.
348+
*
349+
* This is used to add cart handlers for different cart types. For example, renewal, resubscribe, initial, switch etc.
350+
* To access a cart handler instance, use WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( $class ).
351+
*
352+
* @param WCS_Cart_Renewal $cart_handler An instance of a cart handler.
353+
*/
354+
protected function add_cart_handler( $cart_handler ) {
355+
$this->cart_handlers[ get_class( $cart_handler ) ] = $cart_handler;
356+
}
357+
322358
/**
323359
* Registers Subscriptions order types.
324360
*

includes/class-wcs-blocks-integration.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function get_editor_script_handles() {
8282
public function get_script_data() {
8383
return array(
8484
'woocommerce-subscriptions-blocks' => 'active',
85+
'place_order_override' => $this->get_place_order_button_text_override(),
8586
);
8687
}
8788

@@ -97,4 +98,36 @@ protected function get_file_version( $file ) {
9798
}
9899
return \WC_Subscriptions_Core_Plugin::instance()->get_library_version();
99100
}
101+
102+
/**
103+
* Fetches the place order button text if it has been overridden by one of Woo Subscription's methods.
104+
*
105+
* @return string|null The overridden place order button text or null if it hasn't been overridden.
106+
*/
107+
protected function get_place_order_button_text_override() {
108+
$default = null;
109+
$order_button_text = $default;
110+
111+
// Check if any of our button text override functions (hooked onto 'woocommerce_order_button_text') change the default text.
112+
$callbacks = [
113+
[ 'WC_Subscriptions_Checkout', 'order_button_text' ],
114+
[ \WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Renewal' ), 'order_button_text' ],
115+
[ \WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Switch' ), 'order_button_text' ],
116+
[ \WC_Subscriptions_Core_Plugin::instance()->get_cart_handler( 'WCS_Cart_Resubscribe' ), 'order_button_text' ],
117+
];
118+
119+
foreach ( $callbacks as $callback ) {
120+
if ( ! is_callable( $callback ) ) {
121+
continue;
122+
}
123+
124+
$order_button_text = call_user_func( $callback, $default );
125+
126+
if ( $order_button_text !== $default ) {
127+
break;
128+
}
129+
}
130+
131+
return $order_button_text;
132+
}
100133
}

0 commit comments

Comments
 (0)