/** * Check whether the cart needs payment even if the order total is $0 * * @param bool $needs_payment The existing flag for whether the cart needs payment or not. * @param WC_Cart $cart The WooCommerce cart object. * @return bool */ public static function cart_needs_payment($needs_payment, $cart) { if (false === $needs_payment && self::cart_contains_subscription() && $cart->total == 0 && $cart->recurring_total > 0 && 'yes' !== get_option(WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no')) { $is_one_period = self::get_cart_subscription_length() > 0 && self::get_cart_subscription_length() == self::get_cart_subscription_interval() ? true : false; $has_trial = self::get_cart_subscription_trial_length() > 0 ? true : false; $is_syncd = WC_Subscriptions_Synchroniser::cart_contains_synced_subscription(); if (false === $is_one_period || true === $has_trial || false !== $is_syncd && false == WC_Subscriptions_Synchroniser::is_today(WC_Subscriptions_Synchroniser::calculate_first_payment_date($is_syncd['data'], 'timestamp'))) { $needs_payment = true; } } return $needs_payment; }
/** * Check whether the cart needs payment even if the order total is $0 * * @param bool $needs_payment The existing flag for whether the cart needs payment or not. * @param WC_Cart $cart The WooCommerce cart object. * @return bool */ public static function cart_needs_payment($needs_payment, $cart) { if (false === $needs_payment && self::cart_contains_subscription() && $cart->total == 0 && 'yes' !== get_option(WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no')) { $recurring_total = 0; $is_one_period = true; $is_synced = false; foreach (WC()->cart->recurring_carts as $cart) { $recurring_total += $cart->total; $cart_length = wcs_cart_pluck($cart, 'subscription_length'); if (0 == $cart_length || wcs_cart_pluck($cart, 'subscription_period_interval') != $cart_length) { $is_one_period = false; } $is_synced = $is_synced || false != WC_Subscriptions_Synchroniser::cart_contains_synced_subscription($cart) ? true : false; } $has_trial = self::cart_contains_free_trial(); if ($recurring_total > 0 && (false === $is_one_period || true === $has_trial || false !== $is_synced && false == WC_Subscriptions_Synchroniser::is_today(WC_Subscriptions_Synchroniser::calculate_first_payment_date($is_synced['data'], 'timestamp')))) { $needs_payment = true; } } return $needs_payment; }
/** * Get prorated sub price. * * @param double $recurring_price * @param WC_Product $product * @return double */ public function get_prorated_price_for_subscription($recurring_price, $product = false) { if (!$product) { $product = $this->product; } $price = 0; if (WC_Subscriptions_Product::is_subscription($product)) { if (0 == WC_Subscriptions_Product::get_trial_length($product)) { if (WC_Subscriptions_Synchroniser::is_product_prorated($product)) { $next_payment_date = WC_Subscriptions_Synchroniser::calculate_first_payment_date($product, 'timestamp'); if (WC_Subscriptions_Synchroniser::is_today($next_payment_date)) { return $recurring_price; } switch ($product->subscription_period) { case 'week': $days_in_cycle = 7 * $product->subscription_period_interval; break; case 'month': $days_in_cycle = date('t') * $product->subscription_period_interval; break; case 'year': $days_in_cycle = (365 + date('L')) * $product->subscription_period_interval; break; } $days_until_next_payment = ceil(($next_payment_date - gmdate('U')) / (60 * 60 * 24)); $price = $days_until_next_payment * ($recurring_price / $days_in_cycle); } else { $price = $recurring_price; } } } return $price; }