Skip to content

Commit

Permalink
Display ECE button with trial subscription in the cart
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarcosta99 committed Aug 22, 2024
1 parent 039cbd5 commit 1eaa345
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { Elements } from '@stripe/react-stripe-js';
* Internal dependencies
*/
import ExpressCheckoutComponent from './express-checkout-component';
import { getExpressCheckoutButtonAppearance } from 'wcpay/express-checkout/utils';
import {
getExpressCheckoutButtonAppearance,
getExpressCheckoutData,
} from 'wcpay/express-checkout/utils';
import '../express-checkout-element.scss';

const ExpressCheckoutContainer = ( props ) => {
Expand All @@ -21,7 +24,9 @@ const ExpressCheckoutContainer = ( props ) => {
const options = {
mode: 'payment',
paymentMethodCreation: 'manual',
amount: billing.cartTotal.value,
amount: getExpressCheckoutData( 'has_trial_subscription' )
? 1300 // TODO: Find a way to get the cart total with trial subscription.
: billing.cartTotal.value,
currency: billing.currency.code.toLowerCase(),
appearance: getExpressCheckoutButtonAppearance(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public function scripts() {
'product' => $this->express_checkout_helper->get_product_data(),
'total_label' => $this->express_checkout_helper->get_total_label(),
'is_checkout_page' => $this->express_checkout_helper->is_checkout(),
'has_trial_subscription' => $this->express_checkout_helper->has_trial_subscription(),
];

WC_Payments::register_script_with_dependencies( 'WCPAY_EXPRESS_CHECKOUT_ECE', 'dist/express-checkout', [ 'jquery', 'stripe' ] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,28 @@ public function is_product_subscription( WC_Product $product ): bool {
|| 'variable-subscription' === $product->get_type();
}

/**
* Returns true if cart contains a subscription product with a trial period, false otherwise.
*
* @psalm-suppress UndefinedClass
*/
public function has_trial_subscription(): bool {
if ( ! class_exists( 'WC_Subscriptions_Product' ) ) {
return false;
}

// TODO: WC_Subscriptions_Cart may provide a better way to check for trial subscriptions.
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );

if ( WC_Subscriptions_Product::get_trial_length( $product ) > 0 ) {
return true;
}
}

return false;
}

/**
* Checks whether Payment Request Button should be available on this page.
*
Expand Down Expand Up @@ -436,9 +458,12 @@ public function should_show_express_checkout_button() {
}

// Cart total is 0 or is on product page and product price is 0.
// Exclude pay-for-order pages from this check.
// Exclude pay-for-order pages and trial subscriptions from this check.
if (
( ! $this->is_product() && ! $this->is_pay_for_order_page() && 0.0 === (float) WC()->cart->get_total( 'edit' ) ) ||
( ! $this->is_product()
&& ! $this->is_pay_for_order_page()
&& ! $this->has_trial_subscription() // TODO: double-check this condition.
&& 0.0 === (float) WC()->cart->get_total( 'edit' ) ) ||
( $this->is_product() && 0.0 === (float) $this->get_product()->get_price() )

) {
Expand Down Expand Up @@ -528,9 +553,10 @@ public function has_allowed_items_in_cart() {
*
* @psalm-suppress UndefinedClass
*/
if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $_product ) && $_product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $_product ) > 0 ) {
return false;
}
// TODO: Double-check this condition.
// if ( class_exists( 'WC_Subscriptions_Product' ) && WC_Subscriptions_Product::is_subscription( $_product ) && $_product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $_product ) > 0 ) {
// return false;
// }
}

// We don't support multiple packages with Payment Request Buttons because we can't offer a good UX.
Expand Down Expand Up @@ -766,7 +792,8 @@ private function is_product_supported() {
if ( is_null( $product )
|| ! is_object( $product )
|| ! in_array( $product->get_type(), $this->supported_product_types(), true )
|| ( class_exists( 'WC_Subscriptions_Product' ) && $product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $product ) > 0 ) // Trial subscriptions with shipping are not supported.
// TODO: Double-check this condition.
// || ( class_exists( 'WC_Subscriptions_Product' ) && $product->needs_shipping() && WC_Subscriptions_Product::get_trial_length( $product ) > 0 ) // Trial subscriptions with shipping are not supported.
|| ( class_exists( 'WC_Pre_Orders_Product' ) && WC_Pre_Orders_Product::product_is_charged_upon_release( $product ) ) // Pre Orders charge upon release not supported.
|| ( class_exists( 'WC_Composite_Products' ) && $product->is_type( 'composite' ) ) // Composite products are not supported on the product page.
|| ( class_exists( 'WC_Mix_and_Match' ) && $product->is_type( 'mix-and-match' ) ) // Mix and match products are not supported on the product page.
Expand Down

0 comments on commit 1eaa345

Please sign in to comment.