Change Plan
$action = $_GET['action'];
$currentPrice = 0.00;
if (isset($action) && ($action === 'downgrade' || $action === 'upgrade')){
$userSubscriptions = WC_Subscriptions_Manager::get_users_subscriptions();
foreach ($userSubscriptions as $subscription){
if ($subscription['status'] === 'active'){
$currentPrice = (float)get_post_meta($subscription['order_id'], '_order_recurring_total', true);
// Case where user had a groupon code (Free 60 days)
if ($currentPrice === 0.00 || $currentPrice === 0.01){
$recurringDiscount = (float)get_post_meta($subscription['product_id'], '_price', true);
if (getInSetValueFromPrice($recurringDiscount) > 0){
$currentPrice = $recurringDiscount;
}
}
//$product = new WC_Product($subscription['product_id']);
//$currentPrice = $product->get_price();
}
}
if (isset($currentPrice) && $currentPrice > 0.01){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'product_cat' => $_GET['action'],
'orderby' => 'price'
);
$posts = query_posts( $args );
$productIds = array();
foreach($posts as $product){
$pricePerMonth = (float)get_post_meta($product->ID, 'price', true);
$skip = false;
if ($action === 'downgrade'){
if ($pricePerMonth < $currentPrice){
$productIds[] = $product->ID;
break;
}
}
else{
if ($pricePerMonth > $currentPrice)
$productIds[] = $product->ID;
}
}
if (count($productIds) > 0){
echo do_shortcode('[products ids="'.implode(',',$productIds).'"]');
}
}
}
else{ ?>
Downgrading or upgrading your plan is not available at the moment.