Replace place order text via WC_Gateway

This commit is contained in:
Alex P 2023-11-16 09:38:35 +02:00
parent 3672c5a651
commit 91c6e55f3d
No known key found for this signature in database
GPG key ID: 54487A734A204D71
10 changed files with 14 additions and 191 deletions

View file

@ -1,45 +0,0 @@
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

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

View file

@ -9,7 +9,6 @@ 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;
@ -149,15 +148,6 @@ 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

@ -1,116 +0,0 @@
<?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,7 +9,6 @@ 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;
@ -72,11 +71,6 @@ 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,7 +7,6 @@ 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

@ -98,7 +98,8 @@ return array(
$logger,
$api_shop_country,
$container->get( 'api.endpoint.order' ),
$container->get( 'api.factory.paypal-checkout-url' )
$container->get( 'api.factory.paypal-checkout-url' ),
$container->get( 'wcgateway.place-order-button-text' )
);
},
'wcgateway.credit-card-gateway' => static function ( ContainerInterface $container ): CreditCardGateway {
@ -143,7 +144,8 @@ return array(
$container->get( 'onboarding.environment' ),
$container->get( 'vaulting.repository.payment-token' ),
$container->get( 'woocommerce.logger.woocommerce' ),
$container->get( 'api.factory.paypal-checkout-url' )
$container->get( 'api.factory.paypal-checkout-url' ),
$container->get( 'wcgateway.place-order-button-text' )
);
},
'wcgateway.disabler' => static function ( ContainerInterface $container ): DisableGateways {

View file

@ -150,6 +150,7 @@ class CardButtonGateway extends \WC_Payment_Gateway {
* @param PaymentTokenRepository $payment_token_repository The payment token repository.
* @param LoggerInterface $logger The logger.
* @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID.
* @param string $place_order_button_text The text for the standard "Place order" button.
*/
public function __construct(
SettingsRenderer $settings_renderer,
@ -164,7 +165,8 @@ class CardButtonGateway extends \WC_Payment_Gateway {
Environment $environment,
PaymentTokenRepository $payment_token_repository,
LoggerInterface $logger,
callable $paypal_checkout_url_factory
callable $paypal_checkout_url_factory,
string $place_order_button_text
) {
$this->id = self::ID;
$this->settings_renderer = $settings_renderer;
@ -181,6 +183,7 @@ class CardButtonGateway extends \WC_Payment_Gateway {
$this->payment_token_repository = $payment_token_repository;
$this->logger = $logger;
$this->paypal_checkout_url_factory = $paypal_checkout_url_factory;
$this->order_button_text = $place_order_button_text;
$this->supports = array(
'refunds',

View file

@ -190,6 +190,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
* @param string $api_shop_country The api shop country.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param callable(string):string $paypal_checkout_url_factory The function return the PayPal checkout URL for the given order ID.
* @param string $place_order_button_text The text for the standard "Place order" button.
*/
public function __construct(
SettingsRenderer $settings_renderer,
@ -207,7 +208,8 @@ class PayPalGateway extends \WC_Payment_Gateway {
LoggerInterface $logger,
string $api_shop_country,
OrderEndpoint $order_endpoint,
callable $paypal_checkout_url_factory
callable $paypal_checkout_url_factory,
string $place_order_button_text
) {
$this->id = self::ID;
$this->settings_renderer = $settings_renderer;
@ -226,6 +228,7 @@ class PayPalGateway extends \WC_Payment_Gateway {
$this->logger = $logger;
$this->api_shop_country = $api_shop_country;
$this->paypal_checkout_url_factory = $paypal_checkout_url_factory;
$this->order_button_text = $place_order_button_text;
if ( $this->onboarded ) {
$this->supports = array( 'refunds', 'tokenization' );