diff --git a/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js b/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js index 4296b1045..cbf1c3c5e 100644 --- a/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js +++ b/modules/ppcp-button/resources/js/modules/ActionHandler/SingleProductActionHandler.js @@ -18,6 +18,34 @@ class SingleProductActionHandler { this.errorHandler = errorHandler; } + subscriptionsConfiguration() { + return { + createSubscription: (data, actions) => { + return actions.subscription.create({ + 'plan_id': this.config.subscription_plan_id + }); + }, + onApprove: (data, actions) => { + fetch(this.config.ajax.approve_subscription.endpoint, { + method: 'POST', + credentials: 'same-origin', + body: JSON.stringify({ + nonce: this.config.ajax.approve_subscription.nonce, + order_id: data.orderID, + subscription_id: data.subscriptionID + }) + }).then((res)=>{ + return res.json(); + }).then((data) => { + location.href = this.config.redirect; + }); + }, + onError: (err) => { + console.error(err); + } + } + } + configuration() { return { diff --git a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js index a36c1d408..376962772 100644 --- a/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js +++ b/modules/ppcp-button/resources/js/modules/ContextBootstrap/SingleProductBootstap.js @@ -110,6 +110,14 @@ class SingleProductBootstap { this.errorHandler, ); + if( + PayPalCommerceGateway.data_client_id.has_subscriptions + && PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled + ) { + this.renderer.render(actionHandler.subscriptionsConfiguration()); + return; + } + this.renderer.render( actionHandler.configuration() ); diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php index 90e8b8179..e0be6cab6 100644 --- a/modules/ppcp-button/src/Assets/SmartButton.php +++ b/modules/ppcp-button/src/Assets/SmartButton.php @@ -1380,13 +1380,23 @@ class SmartButton implements SmartButtonInterface { * @return string */ private function paypal_subscription_id(): string { + if($this->subscription_helper->current_product_is_subscription() ) { + $product = wc_get_product(); + assert( $product instanceof WC_Product ); + + if ( $product->get_type() === 'subscription' && $product->meta_exists( 'ppcp_subscription_plan' ) ) { + return $product->get_meta( 'ppcp_subscription_plan' )['id']; + } + + } + $items = WC()->cart->get_cart_contents(); foreach ( $items as $item ) { $product = wc_get_product( $item['product_id'] ); assert( $product instanceof WC_Product ); if ( $product->get_type() === 'subscription' && $product->meta_exists( 'ppcp_subscription_plan' ) ) { - return $product->get_meta( 'ppcp_subscription_plan' )->id; + return $product->get_meta( 'ppcp_subscription_plan' )['id']; } }