Skip to content

Instantly share code, notes, and snippets.

@ajgagnon
Created June 24, 2021 19:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajgagnon/bed10fbae8c31f467a65f27fcf492f1e to your computer and use it in GitHub Desktop.
Save ajgagnon/bed10fbae8c31f467a65f27fcf492f1e to your computer and use it in GitHub Desktop.
<?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