mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 09:08:09 +08:00
Fix Smart Buttons on Elementor checkout
This commit is contained in:
parent
0cc4e1f9ce
commit
709871820f
4 changed files with 81 additions and 40 deletions
|
@ -18,8 +18,8 @@ return function ( string $root_dir ): iterable {
|
||||||
( require "$modules_dir/woocommerce-logging/module.php" )(),
|
( require "$modules_dir/woocommerce-logging/module.php" )(),
|
||||||
( require "$modules_dir/ppcp-admin-notices/module.php" )(),
|
( require "$modules_dir/ppcp-admin-notices/module.php" )(),
|
||||||
( require "$modules_dir/ppcp-api-client/module.php" )(),
|
( require "$modules_dir/ppcp-api-client/module.php" )(),
|
||||||
( require "$modules_dir/ppcp-button/module.php" )(),
|
|
||||||
( require "$modules_dir/ppcp-compat/module.php" )(),
|
( require "$modules_dir/ppcp-compat/module.php" )(),
|
||||||
|
( require "$modules_dir/ppcp-button/module.php" )(),
|
||||||
( require "$modules_dir/ppcp-onboarding/module.php" )(),
|
( require "$modules_dir/ppcp-onboarding/module.php" )(),
|
||||||
( require "$modules_dir/ppcp-session/module.php" )(),
|
( require "$modules_dir/ppcp-session/module.php" )(),
|
||||||
( require "$modules_dir/ppcp-status-report/module.php" )(),
|
( require "$modules_dir/ppcp-status-report/module.php" )(),
|
||||||
|
|
|
@ -95,47 +95,51 @@ trait ContextTrait {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function context(): string {
|
protected function context(): string {
|
||||||
if ( is_product() || wc_post_content_has_shortcode( 'product_page' ) ) {
|
// Default context.
|
||||||
|
$context = 'mini-cart';
|
||||||
|
|
||||||
|
switch ( true ) {
|
||||||
|
case is_product() || wc_post_content_has_shortcode( 'product_page' ):
|
||||||
// Do this check here instead of reordering outside conditions.
|
// Do this check here instead of reordering outside conditions.
|
||||||
// In order to have more control over the context.
|
// In order to have more control over the context.
|
||||||
if ( $this->is_checkout() && ! $this->is_paypal_continuation() ) {
|
if ( $this->is_checkout() && ! $this->is_paypal_continuation() ) {
|
||||||
return 'checkout';
|
$context = 'checkout';
|
||||||
}
|
} else {
|
||||||
|
$context = 'product';
|
||||||
return 'product';
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
// has_block may not work if called too early, such as during the block registration.
|
// has_block may not work if called too early, such as during the block registration.
|
||||||
if ( has_block( 'woocommerce/cart' ) ) {
|
case has_block( 'woocommerce/cart' ):
|
||||||
return 'cart-block';
|
$context = 'cart-block';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case $this->is_cart():
|
||||||
|
$context = 'cart';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case is_checkout_pay_page():
|
||||||
|
$context = 'pay-now';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case has_block( 'woocommerce/checkout' ):
|
||||||
|
$context = 'checkout-block';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case $this->is_checkout() && ! $this->is_paypal_continuation():
|
||||||
|
$context = 'checkout';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case $this->is_add_payment_method_page():
|
||||||
|
$context = 'add-payment-method';
|
||||||
|
break;
|
||||||
|
|
||||||
|
case $this->is_block_editor():
|
||||||
|
$context = 'block-editor';
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $this->is_cart() ) {
|
return apply_filters( 'woocommerce_paypal_payments_context', $context );
|
||||||
return 'cart';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( is_checkout_pay_page() ) {
|
|
||||||
return 'pay-now';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( has_block( 'woocommerce/checkout' ) ) {
|
|
||||||
return 'checkout-block';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $this->is_checkout() && ! $this->is_paypal_continuation() ) {
|
|
||||||
return 'checkout';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $this->is_add_payment_method_page() ) {
|
|
||||||
return 'add-payment-method';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $this->is_block_editor() ) {
|
|
||||||
return 'block-editor';
|
|
||||||
}
|
|
||||||
|
|
||||||
return 'mini-cart';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,6 +15,7 @@ use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
|
use WooCommerce\PayPalCommerce\Compat\Assets\CompatAssets;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
|
||||||
|
use WooCommerce\PayPalCommerce\WcGateway\Helper\CartCheckoutDetector;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,6 +55,7 @@ class CompatModule implements ModuleInterface {
|
||||||
$this->migrate_smart_button_settings( $c );
|
$this->migrate_smart_button_settings( $c );
|
||||||
|
|
||||||
$this->fix_page_builders();
|
$this->fix_page_builders();
|
||||||
|
$this->set_elementor_checkout_context();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -329,4 +331,32 @@ class CompatModule implements ModuleInterface {
|
||||||
$parent = $theme->parent();
|
$parent = $theme->parent();
|
||||||
return ( $parent && $parent->get( 'Name' ) === 'Divi' );
|
return ( $parent && $parent->get( 'Name' ) === 'Divi' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the context for the Elementor checkout page.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function set_elementor_checkout_context(): void {
|
||||||
|
add_action(
|
||||||
|
'wp',
|
||||||
|
function() {
|
||||||
|
$page_id = get_the_ID();
|
||||||
|
if ( $page_id ) {
|
||||||
|
if ( CartCheckoutDetector::has_elementor_checkout( $page_id ) ) {
|
||||||
|
add_filter(
|
||||||
|
'woocommerce_paypal_payments_context',
|
||||||
|
function ( $context ): string {
|
||||||
|
// Default context.
|
||||||
|
if ( 'mini-cart' === $context ) {
|
||||||
|
return 'checkout';
|
||||||
|
}
|
||||||
|
return $context;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,12 +57,19 @@ class CartCheckoutDetector {
|
||||||
/**
|
/**
|
||||||
* Check if the Checkout page is using Elementor.
|
* Check if the Checkout page is using Elementor.
|
||||||
*
|
*
|
||||||
|
* @param int $page_id The ID of the page.
|
||||||
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function has_elementor_checkout(): bool {
|
public static function has_elementor_checkout( int $page_id = 0 ): bool {
|
||||||
// Check if Elementor is installed and activated.
|
// Check if Elementor is installed and activated.
|
||||||
if ( did_action( 'elementor/loaded' ) ) {
|
if ( did_action( 'elementor/loaded' ) ) {
|
||||||
|
if ( $page_id ) {
|
||||||
|
$elementor_widgets = self::get_elementor_widgets( $page_id );
|
||||||
|
} else {
|
||||||
|
// Check the WooCommerce checkout page.
|
||||||
$elementor_widgets = self::get_elementor_widgets( wc_get_page_id( 'checkout' ) );
|
$elementor_widgets = self::get_elementor_widgets( wc_get_page_id( 'checkout' ) );
|
||||||
|
}
|
||||||
|
|
||||||
if ( $elementor_widgets ) {
|
if ( $elementor_widgets ) {
|
||||||
return in_array( 'woocommerce-checkout-page', $elementor_widgets, true );
|
return in_array( 'woocommerce-checkout-page', $elementor_widgets, true );
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue