mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Create PayPal subscription plan from WC subscription product (WIP)
This commit is contained in:
parent
3c1ad831c1
commit
760a6d06f7
3 changed files with 65 additions and 3 deletions
|
@ -9,6 +9,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Subscription;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Subscriptions;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
@ -135,6 +137,58 @@ class SubscriptionModule implements ModuleInterface {
|
|||
return $data;
|
||||
}
|
||||
);
|
||||
|
||||
add_action('woocommerce_update_product', function($product_id) use($c) {
|
||||
$product = wc_get_product( $product_id );
|
||||
if($product->get_type() === 'subscription') {
|
||||
if(!$product->meta_exists('ppcp_subscription_product_id')) {
|
||||
$products_endpoint = $c->get('api.endpoint.catalog-products');
|
||||
assert($products_endpoint instanceof CatalogProducts);
|
||||
|
||||
$subscription_product = $products_endpoint->create($product->get_title());
|
||||
$product->update_meta_data('ppcp_subscription_product_id', $subscription_product->id);
|
||||
$product->save();
|
||||
|
||||
$subscriptions_endpoint = $c->get('api.endpoint.subscriptions');
|
||||
assert($subscriptions_endpoint instanceof Subscriptions);
|
||||
|
||||
$billing_cycles = array(
|
||||
'frequency' => array(
|
||||
'interval_unit' => $product->get_meta('_subscription_period'),
|
||||
'interval_count' => $product->get_meta('_subscription_period_interval'),
|
||||
),
|
||||
'tenure_type' => 'REGULAR',
|
||||
'sequence' => 1,
|
||||
'total_cycles' => $product->get_meta('_subscription_length'),
|
||||
'pricing_scheme' => array(
|
||||
'fixed_price' => array(
|
||||
'value' => $product->get_meta('_subscription_price'),
|
||||
'currency_code' => 'USD',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$payment_preferences = array(
|
||||
'auto_bill_outstanding' => true,
|
||||
'setup_fee' => array(
|
||||
'value' => $product->get_meta('_subscription_sign_up_fee'),
|
||||
'currency_code' => 'USD',
|
||||
),
|
||||
'setup_fee_failure_action' => 'CONTINUE',
|
||||
'payment_failure_threshold' => 3
|
||||
);
|
||||
|
||||
$subscription_plan = $subscriptions_endpoint->create_plan(
|
||||
$subscription_product->id,
|
||||
$billing_cycles,
|
||||
$payment_preferences
|
||||
);
|
||||
|
||||
$product->update_meta_data('ppcp_subscription_plan', $subscription_plan->id);
|
||||
$product->save();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue