Merge pull request #1814 from woocommerce/PCP-2128-order-page-messages

Display Pay Later messages before the payment methods on the Pay for Order page (2128)
This commit is contained in:
Emili Castells 2023-11-03 10:48:04 +01:00 committed by GitHub
commit 2c2d8106d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -395,14 +395,16 @@ class SmartButton implements SmartButtonInterface {
return false;
}
$get_hook = function ( string $location ): ?array {
$default_pay_order_hook = 'woocommerce_pay_order_before_submit';
$get_hook = function ( string $location ) use ( $default_pay_order_hook ): ?array {
switch ( $location ) {
case 'checkout':
return $this->messages_renderer_hook( $location, 'woocommerce_review_order_before_payment', 10 );
case 'cart':
return $this->messages_renderer_hook( $location, $this->proceed_to_checkout_button_renderer_hook(), 19 );
case 'pay-now':
return $this->messages_renderer_hook( 'pay_order', 'woocommerce_pay_order_before_submit', 10 );
return $this->messages_renderer_hook( $location, $default_pay_order_hook, 10 );
case 'product':
return $this->messages_renderer_hook( $location, $this->single_product_renderer_hook(), 30 );
case 'shop':
@ -425,6 +427,28 @@ class SmartButton implements SmartButtonInterface {
$hook['priority']
);
// Looks like there are no hooks like woocommerce_review_order_before_payment on the pay for order page, so have to move using JS.
if ( $location === 'pay-now' && $hook['name'] === $default_pay_order_hook &&
/**
* The filter returning true if Pay Later messages should be displayed before payment methods
* on the pay for order page, like in checkout.
*/
apply_filters(
'woocommerce_paypal_payments_put_pay_order_messages_before_payment_methods',
true
)
) {
add_action(
'ppcp_after_pay_order_message_wrapper',
function () {
echo '
<script>
document.querySelector("#payment").before(document.querySelector("#ppcp-messages"))
</script>';
}
);
}
return true;
}
@ -681,11 +705,12 @@ class SmartButton implements SmartButtonInterface {
/**
* Renders the HTML for the credit messaging.
*/
public function message_renderer() {
public function message_renderer(): void {
$product = wc_get_product();
$location = $this->location();
$location = $this->location();
$location_hook = $this->location_to_hook( $location );
if (
$location === 'product' && is_a( $product, WC_Product::class )
@ -697,7 +722,17 @@ class SmartButton implements SmartButtonInterface {
return;
}
/**
* A hook executed before rendering of the PCP Pay Later messages wrapper.
*/
do_action( "ppcp_before_{$location_hook}_message_wrapper" );
echo '<div id="ppcp-messages" data-partner-attribution-id="Woo_PPCP"></div>';
/**
* A hook executed after rendering of the PCP Pay Later messages wrapper.
*/
do_action( "ppcp_after_{$location_hook}_message_wrapper" );
}
/**
@ -1406,18 +1441,20 @@ class SmartButton implements SmartButtonInterface {
* @return array An array with 'name' and 'priority' keys.
*/
private function messages_renderer_hook( string $location, string $default_hook, int $default_priority ): array {
$location_hook = $this->location_to_hook( $location );
/**
* The filter returning the action name that will be used for rendering Pay Later messages.
*/
$hook = (string) apply_filters(
"woocommerce_paypal_payments_${location}_messages_renderer_hook",
"woocommerce_paypal_payments_${location_hook}_messages_renderer_hook",
$default_hook
);
/**
* The filter returning the action priority that will be used for rendering Pay Later messages.
*/
$priority = (int) apply_filters(
"woocommerce_paypal_payments_${location}_messages_renderer_priority",
"woocommerce_paypal_payments_${location_hook}_messages_renderer_priority",
$default_priority
);
return array(
@ -1731,4 +1768,18 @@ class SmartButton implements SmartButtonInterface {
}
/**
* Converts the location name into the name used in hooks.
*
* @param string $location The location.
* @return string
*/
private function location_to_hook( string $location ): string {
switch ( $location ) {
case 'pay-now':
return 'pay_order';
default:
return $location;
}
}
}