Created
June 24, 2021 19:20
-
-
Save ajgagnon/bed10fbae8c31f467a65f27fcf492f1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Retrieve the plan ID for an item in the cart | |
* | |
* @access public | |
* @since 2.4 | |
* @return string | |
*/ | |
public function get_plan_id( $subscription = array() ) { | |
$name = get_post_field( 'post_name', $subscription['id'] ); | |
if( isset( $subscription['price_id'] ) && false !== $subscription['price_id'] ) { | |
$name .= ' - ' . edd_get_price_option_name( $subscription['id'], $subscription['price_id'] ); | |
} | |
$plan_id = $name . '_' . $subscription['recurring_amount'] . '_' . $subscription['period']; | |
$plan_id = sanitize_key( $plan_id ); | |
try { | |
$plan = \Stripe\Plan::retrieve( $plan_id ); | |
$currency = strtolower( edd_get_currency() ); | |
if( $plan->currency != $currency ) { | |
$plan_id = $plan_id . '_' . $currency; | |
$args = $this->get_plan_args( $subscription, $name, $plan_id ); | |
try { | |
$plan = \Stripe\Plan::retrieve( $plan_id ); | |
$plan_id = is_array( $plan ) ? $plan['id'] : $plan->id; | |
} catch ( Exception $e ) { | |
$plan_id = $this->create_stripe_plan( $args ); | |
} | |
} | |
} catch ( Exception $e ) { | |
$args = $this->get_plan_args( $subscription, $name, $plan_id ); | |
$plan_id = $this->create_stripe_plan( $args ); | |
} | |
return $plan_id; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment