Skip to content

Commit fdd391e

Browse files
Introduced wc_get_pay_buttons() function
1 parent ced2076 commit fdd391e

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

includes/wc-template-functions.php

+26
Original file line numberDiff line numberDiff line change
@@ -3664,3 +3664,29 @@ function woocommerce_product_reviews_tab() {
36643664
wc_deprecated_function( 'woocommerce_product_reviews_tab', '2.4' );
36653665
}
36663666
}
3667+
3668+
/**
3669+
* Display pay buttons HTML.
3670+
*
3671+
* @since 3.9.0
3672+
*/
3673+
function wc_get_pay_buttons() {
3674+
$supported_gateways = array();
3675+
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
3676+
3677+
foreach ( $available_gateways as $gateway ) {
3678+
if ( $gateway->supports( 'pay_button' ) ) {
3679+
$supported_gateways[] = $gateway->get_pay_button_id();
3680+
}
3681+
}
3682+
3683+
if ( ! $supported_gateways ) {
3684+
return;
3685+
}
3686+
3687+
echo '<div class="woocommerce-pay-buttons">';
3688+
foreach ( $supported_gateways as $pay_button_id ) {
3689+
echo sprintf( '<div class="woocommerce-pay-button__%1$s %1$s" id="%1$s"></div>', esc_attr( $pay_button_id ) );
3690+
}
3691+
echo '</div>';
3692+
}

includes/wc-template-hooks.php

+7
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,17 @@
207207
* @see woocommerce_checkout_coupon_form()
208208
* @see woocommerce_order_review()
209209
* @see woocommerce_checkout_payment()
210+
* @see wc_checkout_privacy_policy_text()
211+
* @see wc_terms_and_conditions_page_content()
212+
* @see wc_get_pay_buttons()
210213
*/
211214
add_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_login_form', 10 );
212215
add_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
213216
add_action( 'woocommerce_checkout_order_review', 'woocommerce_order_review', 10 );
214217
add_action( 'woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
215218
add_action( 'woocommerce_checkout_terms_and_conditions', 'wc_checkout_privacy_policy_text', 20 );
216219
add_action( 'woocommerce_checkout_terms_and_conditions', 'wc_terms_and_conditions_page_content', 30 );
220+
add_action( 'woocommerce_checkout_before_customer_details', 'wc_get_pay_buttons', 30 );
217221

218222
/**
219223
* Cart widget
@@ -227,10 +231,13 @@
227231
*
228232
* @see woocommerce_cross_sell_display()
229233
* @see woocommerce_cart_totals()
234+
* @see wc_get_pay_buttons()
230235
* @see woocommerce_button_proceed_to_checkout()
236+
* @see wc_empty_cart_message()
231237
*/
232238
add_action( 'woocommerce_cart_collaterals', 'woocommerce_cross_sell_display' );
233239
add_action( 'woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10 );
240+
add_action( 'woocommerce_proceed_to_checkout', 'wc_get_pay_buttons', 10 );
234241
add_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
235242
add_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
236243

tests/unit-tests/templates/functions.php

+36
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,40 @@ public function test_wc_query_string_form_fields() {
142142
$expected_html = '<input type="hidden" name="test_something" value="something else" />';
143143
$this->assertEquals( $expected_html, $actual_html );
144144
}
145+
146+
/**
147+
* Test test_wc_get_pay_buttons().
148+
*/
149+
public function test_wc_get_pay_buttons() {
150+
// Test default.
151+
ob_start();
152+
wc_get_pay_buttons();
153+
$actual_html = ob_get_clean();
154+
155+
$this->assertEquals( '', $actual_html );
156+
157+
// Include a payment gateway that supports "pay button".
158+
add_filter(
159+
'woocommerce_payment_gateways',
160+
function( $gateways ) {
161+
$gateways[] = 'WC_Mock_Payment_Gateway';
162+
163+
return $gateways;
164+
}
165+
);
166+
WC()->payment_gateways()->init();
167+
168+
// Test pay buttons HTML.
169+
ob_start();
170+
wc_get_pay_buttons();
171+
$actual_html = ob_get_clean();
172+
173+
$gateway = new WC_Mock_Payment_Gateway();
174+
$expected_html = sprintf(
175+
'<div class="woocommerce-pay-buttons"><div class="woocommerce-pay-button__%1$s %1$s" id="%1$s"></div></div>',
176+
$gateway->get_pay_button_id()
177+
);
178+
179+
$this->assertEquals( $expected_html, $actual_html );
180+
}
145181
}

0 commit comments

Comments
 (0)