Add filters for place order button mode and text replacement script

This commit is contained in:
Alex P 2023-11-15 09:44:26 +02:00
parent b2ba72c06c
commit c71e312f18
No known key found for this signature in database
GPG key ID: 54487A734A204D71
7 changed files with 240 additions and 0 deletions

View file

@ -0,0 +1,45 @@
import {
getCurrentPaymentMethod,
ORDER_BUTTON_SELECTOR,
PaymentMethods
} from "../Helper/CheckoutMethodState";
class PlaceOrderButtonBootstrap {
constructor(config) {
this.config = config;
this.defaultButtonText = null;
}
init() {
jQuery(document.body).on('updated_checkout payment_method_selected', () => {
this.updateUi();
});
this.updateUi();
}
updateUi() {
const button = document.querySelector(ORDER_BUTTON_SELECTOR);
if (!button) {
return;
}
if (!this.defaultButtonText) {
this.defaultButtonText = button.innerText;
if (!this.defaultButtonText) {
return;
}
}
const currentPaymentMethod = getCurrentPaymentMethod();
if ([PaymentMethods.PAYPAL, PaymentMethods.CARD_BUTTON].includes(currentPaymentMethod)) {
button.innerText = this.config.buttonText;
} else {
button.innerText = this.defaultButtonText;
}
}
}
export default PlaceOrderButtonBootstrap

View file

@ -0,0 +1,8 @@
import PlaceOrderButtonBootstrap from "./modules/ContextBootstrap/PlaceOrderButtonBootstrap";
document.addEventListener(
'DOMContentLoaded',
() => {
const placeOrderButtonBootstrap = new PlaceOrderButtonBootstrap(PpcpPlaceOrderButton);
placeOrderButtonBootstrap.init();
});

View file

@ -9,14 +9,17 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button;
use WooCommerce\PayPalCommerce\Button\Assets\PlaceOrderButtonAssets;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\SimulateCartEndpoint;
use WooCommerce\PayPalCommerce\Button\Helper\CartProductsHelper;
use WooCommerce\PayPalCommerce\Button\Helper\CheckoutFormSaver;
use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint;
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\Button\Validation\CheckoutFormValidator;
use WooCommerce\PayPalCommerce\Button\Endpoint\ValidateCheckoutEndpoint;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Button\Assets\DisabledSmartButton;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
@ -68,8 +71,41 @@ return array(
return $dummy_ids[ $shop_country ] ?? $container->get( 'button.client_id' );
},
// This service may not work correctly when called too early.
'button.context' => static function ( ContainerInterface $container ): string {
$obj = new class() {
use ContextTrait;
/**
* Session handler.
*
* @var SessionHandler
*/
protected $session_handler;
/** Constructor. */
public function __construct() {
// phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundInStatic
$this->session_handler = new SessionHandler();
}
/**
* Wrapper for a non-public function.
*/
public function get_context(): string {
// phpcs:ignore PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundInStatic
return $this->context();
}
};
return $obj->get_context();
},
'button.smart-button' => static function ( ContainerInterface $container ): SmartButtonInterface {
$state = $container->get( 'onboarding.state' );
if ( $container->get( 'wcgateway.use-place-order-button' )
&& in_array( $container->get( 'button.context' ), array( 'checkout', 'pay-now' ), true )
) {
return new DisabledSmartButton();
}
if ( $state->current_state() !== State::STATE_ONBOARDED ) {
return new DisabledSmartButton();
}
@ -113,6 +149,15 @@ return array(
$container->get( 'woocommerce.logger.woocommerce' )
);
},
'button.place-order-assets' => static function ( ContainerInterface $container ): PlaceOrderButtonAssets {
return new PlaceOrderButtonAssets(
$container->get( 'button.url' ),
$container->get( 'ppcp.asset-version' ),
$container->get( 'session.handler' ),
$container->get( 'wcgateway.use-place-order-button' ),
$container->get( 'wcgateway.place-order-button-text' )
);
},
'button.url' => static function ( ContainerInterface $container ): string {
return plugins_url(
'/modules/ppcp-button/',

View file

@ -0,0 +1,116 @@
<?php
/**
* Register and configure the assets for the Place Order button
*
* @package WooCommerce\PayPalCommerce\Button\Assets
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button\Assets;
use WooCommerce\PayPalCommerce\Button\Helper\ContextTrait;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\GatewayRepository;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayUponInvoice\PayUponInvoiceGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
* Class PlaceOrderButtonAssets
*/
class PlaceOrderButtonAssets {
use ContextTrait;
/**
* The URL of this module.
*
* @var string
*/
protected $module_url;
/**
* The assets version.
*
* @var string
*/
protected $version;
/**
* Session handler.
*
* @var SessionHandler
*/
private $session_handler;
/**
* Whether to use the standard "Place order" button.
*
* @var bool
*/
protected $use_place_order;
/**
* The text for the standard "Place order" button.
*
* @var string
*/
protected $button_text;
/**
* Assets constructor.
*
* @param string $module_url The url of this module.
* @param string $version The assets version.
* @param SessionHandler $session_handler The Session handler.
* @param bool $use_place_order Whether to use the standard "Place order" button.
* @param string $button_text The text for the standard "Place order" button.
*/
public function __construct(
string $module_url,
string $version,
SessionHandler $session_handler,
bool $use_place_order,
string $button_text
) {
$this->module_url = $module_url;
$this->version = $version;
$this->session_handler = $session_handler;
$this->use_place_order = $use_place_order;
$this->button_text = $button_text;
}
/**
* Registers the assets.
*/
public function register_assets(): void {
if ( $this->should_load() ) {
wp_enqueue_script(
'ppcp-place-order-button',
trailingslashit( $this->module_url ) . 'assets/js/place-order-button.js',
array(),
$this->version,
true
);
wp_localize_script(
'ppcp-place-order-button',
'PpcpPlaceOrderButton',
array(
'buttonText' => $this->button_text,
)
);
}
}
/**
* Checks if the assets should be loaded.
*/
protected function should_load(): bool {
return $this->use_place_order && in_array( $this->context(), array( 'checkout', 'pay-now' ), true );
}
}

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\Button;
use WooCommerce\PayPalCommerce\Button\Assets\PlaceOrderButtonAssets;
use WooCommerce\PayPalCommerce\Button\Endpoint\ApproveSubscriptionEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\CartScriptParamsEndpoint;
use WooCommerce\PayPalCommerce\Button\Endpoint\SaveCheckoutFormEndpoint;
@ -71,6 +72,11 @@ class ButtonModule implements ModuleInterface {
if ( $smart_button->should_load_ppcp_script() ) {
$smart_button->enqueue();
}
$place_order_assets = $c->get( 'button.place-order-assets' );
assert( $place_order_assets instanceof PlaceOrderButtonAssets );
$place_order_assets->register_assets();
}
);

View file

@ -7,6 +7,7 @@ module.exports = {
target: 'web',
entry: {
button: path.resolve('./resources/js/button.js'),
'place-order-button': path.resolve('./resources/js/place-order-button.js'),
"hosted-fields": path.resolve('./resources/css/hosted-fields.scss'),
"gateway": path.resolve('./resources/css/gateway.scss')
},

View file

@ -1156,6 +1156,25 @@ return array(
);
},
'wcgateway.use-place-order-button' => function ( ContainerInterface $container ) : bool {
/**
* Whether to use the standard "Place order" button with redirect to PayPal instead of the PayPal smart buttons.
*/
return apply_filters(
'woocommerce_paypal_payments_use_place_order_button',
false
);
},
'wcgateway.place-order-button-text' => function ( ContainerInterface $container ) : string {
/**
* The text for the standard "Place order" button, when the "Place order" button mode is enabled.
*/
return apply_filters(
'woocommerce_paypal_payments_place_order_button_text',
__( 'Pay with PayPal', 'woocommerce-paypal-payments' )
);
},
'wcgateway.helper.vaulting-scope' => static function ( ContainerInterface $container ): bool {
try {
$token = $container->get( 'api.bearer' )->bearer();