PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.0.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.0.0
2.4.0 2.3.0 2.2.0 2.1.0 2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 All 65 releases
← All changes | includes/Illuminate/Helper.php +443 -24 1.9.6 → 2.0.0 View file →
@@ -1,5 +1,10 @@
1 1 <?php
2 +/**
3 + * Subscription helper utilities.
4 + *
5 + * @package SpringDevs\Subscription\Illuminate
6 + */
2 7
3 8 namespace SpringDevs\Subscription\Illuminate;
4 9
5 10 use SpringDevs\Subscription\Illuminate\Gateways\Stripe\Stripe;
@@ -59,15 +64,18 @@
59 64 * Get verbose status from status slug.
60 65 *
61 66 * @param string $status Status.
62 67 * @param bool $return_all Whether to return all statuses or a single status.
68 + *
69 + * @return string|array Verbose status label, or the full status map when $return_all is true.
63 70 */
64 - public static function get_verbose_status( $status, $return_all = false ): string|array {
71 + public static function get_verbose_status( $status, $return_all = false ) {
65 72 $statuses = array(
66 73 'pending' => __( 'Pending', 'subscription' ),
67 74 'active' => __( 'Active', 'subscription' ),
68 75 'on-hold' => __( 'On Hold', 'subscription' ),
69 76 'expired' => __( 'Expired', 'subscription' ),
77 + 'completed' => __( 'Completed', 'subscription' ),
70 78 'pe_cancelled' => __( 'Pending Cancellation', 'subscription' ),
71 79 'cancelled' => __( 'Cancelled', 'subscription' ),
72 80 'draft' => __( 'Draft', 'subscription' ),
73 81 'trash' => __( 'Trash', 'subscription' ),
@@ -495,12 +503,16 @@
495 503
496 504 // Allow modification of split payment arguments
497 505 $split_payment_args = apply_filters( 'subscrpt_split_payment_args', $split_payment_args, $order_item, $product );
498 506
507 + // Own the subscription from the order's customer, not the current user.
508 + $parent_order = wc_get_order( $order_item->get_order_id() );
509 +
499 510 $args = array(
500 511 'post_title' => 'Subscription',
501 512 'post_type' => 'subscrpt_order',
502 513 'post_status' => $split_payment_args['post_status'],
514 + 'post_author' => $parent_order ? (int) $parent_order->get_customer_id() : get_current_user_id(),
503 515 );
504 516 $subscription_id = wp_insert_post( $args );
505 517 wp_update_post(
506 518 array(
@@ -508,9 +520,10 @@
508 520 'post_title' => "Subscription #{$subscription_id}",
509 521 )
510 522 );
511 523 // Check if this is a split payment subscription
512 - $payment_type = $product->get_meta( '_subscrpt_payment_type' ) ?: 'recurring';
524 + $payment_type = $product->get_meta( '_subscrpt_payment_type' );
525 + $payment_type = $payment_type ? $payment_type : 'recurring';
513 526 $max_payments = $product->get_meta( '_subscrpt_max_no_payment' );
514 527
515 528 $comment_content = '';
516 529 $activity_type = '';
@@ -561,8 +574,408 @@
561 574 return $subscription_id;
562 575 }
563 576
564 577 /**
578 + * Resolve applied coupon discounts for a single cart item, split by whether they recur.
579 + *
580 + * WooCommerce computes a per-coupon, per-item discount breakdown while calculating cart
581 + * totals but never persists it — only the aggregated per-coupon and per-item sums survive.
582 + * Replaying the coupons through a fresh WC_Discounts instance recovers that breakdown,
583 + * which is what lets recurring and one-time discounts be told apart for one cart line.
584 + *
585 + * Amounts are returned in the same space WC_Discounts works in, i.e. `get_price() * qty`,
586 + * so they are tax-inclusive only when the store's prices include tax.
587 + *
588 + * @param string $cart_item_key Cart item key.
589 + *
590 + * @return array{recurring:float,non_recurring:float,total:float,recurring_limit:int}
591 + */
592 + public static function get_cart_item_coupon_discounts( $cart_item_key ) {
593 + $empty = array(
594 + 'recurring' => 0.0,
595 + 'non_recurring' => 0.0,
596 + 'total' => 0.0,
597 + 'recurring_limit' => 0,
598 + );
599 +
600 + if ( ! function_exists( 'WC' ) || ! WC()->cart ) {
601 + return $empty;
602 + }
603 +
604 + $coupons = WC()->cart->get_coupons();
605 + if ( empty( $coupons ) ) {
606 + return $empty;
607 + }
608 +
609 + // Memoized per cart state: recurring totals are read several times per request, and
610 + // replaying coupons re-runs coupon validation, which hits the database.
611 + static $cache = array();
612 +
613 + $cache_key = md5( WC()->cart->get_cart_hash() . '|' . implode( ',', array_keys( $coupons ) ) );
614 +
615 + if ( ! isset( $cache[ $cache_key ] ) ) {
616 + // Replay in cart order so stacked coupons resolve exactly as WC_Cart_Totals resolved them.
617 + $discounts = new \WC_Discounts( WC()->cart );
618 + foreach ( $coupons as $coupon ) {
619 + $discounts->apply_coupon( $coupon );
620 + }
621 + $cache[ $cache_key ] = $discounts->get_discounts();
622 + }
623 +
624 + $result = $empty;
625 + $limits = array();
626 +
627 + foreach ( $cache[ $cache_key ] as $coupon_code => $item_discounts ) {
628 + $amount = (float) ( $item_discounts[ $cart_item_key ] ?? 0 );
629 + if ( 0 >= $amount ) {
630 + continue;
631 + }
632 +
633 + $coupon = $coupons[ $coupon_code ] ?? new \WC_Coupon( $coupon_code );
634 +
635 + /**
636 + * Filters whether a coupon's discount also applies to subscription renewals.
637 + *
638 + * The free plugin has no recurring-coupon concept, so discounts apply to the
639 + * first payment only unless an extension — the pro plugin — says otherwise.
640 + *
641 + * @param bool $is_recurring Whether the discount recurs. Default false.
642 + * @param \WC_Coupon $coupon Coupon object.
643 + * @param string $cart_item_key Cart item key the discount applies to.
644 + */
645 + $is_recurring = (bool) apply_filters( 'subscrpt_coupon_is_recurring', false, $coupon, $cart_item_key );
646 +
647 + if ( ! $is_recurring ) {
648 + $result['non_recurring'] += $amount;
649 + continue;
650 + }
651 +
652 + /**
653 + * Filters how many payments a recurring coupon's discount covers.
654 + *
655 + * @param int $limit Number of payments, including the initial one. 0 means unlimited.
656 + * @param \WC_Coupon $coupon Coupon object.
657 + * @param string $cart_item_key Cart item key the discount applies to.
658 + */
659 + $limit = (int) apply_filters( 'subscrpt_coupon_recurring_limit', 0, $coupon, $cart_item_key );
660 +
661 + // A limit of one covers the initial payment only, so it never reaches a renewal —
662 + // whatever the coupon is flagged as, its effect here is a one-time discount.
663 + if ( 1 === $limit ) {
664 + $result['non_recurring'] += $amount;
665 + continue;
666 + }
667 +
668 + $result['recurring'] += $amount;
669 +
670 + if ( $limit > 0 ) {
671 + $limits[] = $limit;
672 + }
673 + }
674 +
675 + $result['total'] = $result['recurring'] + $result['non_recurring'];
676 +
677 + // The earliest limit to expire is when the discounted figure stops being true.
678 + $result['recurring_limit'] = empty( $limits ) ? 0 : min( $limits );
679 +
680 + return $result;
681 + }
682 +
683 + /**
684 + * Build the discount-aware recurring price figures and markup for one cart item.
685 + *
686 + * A recurring coupon lowers what every renewal costs, so it is folded into the recurring
687 + * figures. A one-time coupon lowers only what is paid today, so the recurring figures keep
688 + * the full price and the caller discloses the first-payment amount separately.
689 + *
690 + * Discounts come back from WC_Discounts in `get_price() * qty` space, so they are converted
691 + * to the tax-inclusive display space by ratio rather than by re-deriving tax.
692 + *
693 + * @param array $cart_item Cart item.
694 + * @param string $cart_item_key Cart item key.
695 + * @param string $type_label Human readable timing label, e.g. "Month".
696 + *
697 + * @return array
698 + */
699 + public static function build_cart_recurring_price_data( $cart_item, $cart_item_key, $type_label ) {
700 + $product = $cart_item['data'];
701 + $quantity = (int) $cart_item['quantity'];
702 + $per_cost = (float) ( $cart_item['subscription']['per_cost'] ?? 0 );
703 +
704 + $full_total = (float) wc_get_price_including_tax( $product, [ 'qty' => $quantity ] );
705 + $discounts = self::get_cart_item_coupon_discounts( $cart_item_key );
706 +
707 + // Same basis WC_Discounts used, so the discount and the basis are directly comparable.
708 + $basis = (float) $product->get_price() * $quantity;
709 + $recurring_ratio = $basis > 0 ? ( $basis - $discounts['recurring'] ) / $basis : 1.0;
710 + $first_ratio = $basis > 0 ? ( $basis - $discounts['total'] ) / $basis : 1.0;
711 +
712 + $total = $full_total * $recurring_ratio;
713 + $timing_html = "<span class='wpsubs-subscription-timing'>&nbsp;/&nbsp;{$type_label}</span>";
714 +
715 + $has_recurring_discount = $discounts['recurring'] > 0;
716 + $full_price_html = wc_price( $full_total ) . $timing_html;
717 + $price_html = $has_recurring_discount
718 + ? '<del aria-hidden="true">' . wc_price( $full_total ) . '</del> <ins>' . wc_price( $total ) . '</ins>' . $timing_html
719 + : $full_price_html;
720 +
721 + return array(
722 + 'price_html' => $price_html,
723 + 'full_price_html' => $full_price_html,
724 + 'price' => $per_cost * $recurring_ratio,
725 + 'full_price' => $per_cost,
726 + 'total' => $total,
727 + 'full_total' => $full_total,
728 + 'first_total' => $full_total * $first_ratio,
729 + 'has_recurring_discount' => $has_recurring_discount,
730 + 'has_one_time_discount' => $discounts['non_recurring'] > 0,
731 + 'recurring_limit' => $discounts['recurring_limit'],
732 + );
733 + }
734 +
735 + /**
736 + * Resolve the discount that still applies to a subscription's future renewals.
737 + *
738 + * Only coupons flagged as recurring survive into renewal orders, and only while their
739 + * recurring limit holds — this mirrors the skip conditions in the pro plugin's
740 + * `Coupon::maybe_add_coupon_to_renewal_order()`, so what is displayed matches what the
741 + * next renewal order will actually be charged.
742 + *
743 + * A subscription order always holds exactly one line item (enforced by
744 + * `Frontend\Cart::validate_cart_items()`), so each coupon line's whole discount belongs
745 + * to that item.
746 + *
747 + * @param int $subscription_id Subscription ID.
748 + * @param \WC_Order|null $order Source order. Resolved from the subscription when omitted.
749 + * @param \WC_Order_Item|null $order_item Source order item. Used to rebase the discount when the
750 + * recurring price has since drifted, e.g. after a switch.
751 + *
752 + * @return array{amount:float,limit:int,exhausted:bool}
753 + */
754 + public static function get_subscription_recurring_discount( $subscription_id, $order = null, $order_item = null ) {
755 + $result = array(
756 + 'amount' => 0.0,
757 + 'limit' => 0,
758 + 'exhausted' => false,
759 + );
760 +
761 + // Memoized per request: list views and the single view each resolve the same
762 + // subscription two or three times, and a coupon'd subscription costs a query.
763 + static $cache = array();
764 +
765 + $cache_key = $subscription_id . '|' . ( $order_item ? $order_item->get_id() : 0 );
766 +
767 + if ( isset( $cache[ $cache_key ] ) ) {
768 + return $cache[ $cache_key ];
769 + }
770 +
771 + if ( ! $order ) {
772 + $order_item_id = get_post_meta( $subscription_id, '_subscrpt_order_item_id', true );
773 + $order = $order_item_id ? wc_get_order( wc_get_order_id_by_order_item_id( $order_item_id ) ) : null;
774 + }
775 +
776 + if ( ! $order ) {
777 + $cache[ $cache_key ] = $result;
778 + return $result;
779 + }
780 +
781 + $coupon_lines = $order->get_items( 'coupon' );
782 + if ( empty( $coupon_lines ) ) {
783 + $cache[ $cache_key ] = $result;
784 + return $result;
785 + }
786 +
787 + /*
788 + * Position the next renewal will take in this subscription's order sequence.
789 + *
790 + * Note this is deliberately one more than the current order count, and so is NOT the
791 + * same expression pro's Coupon::maybe_add_coupon_to_renewal_order() evaluates: that
792 + * runs after the new order's relation row is already inserted, so its count includes
793 + * the order being created. Both mean "is this order still within the limit".
794 + */
795 + $next_order_position = count( self::get_related_orders( (int) $subscription_id ) ) + 1;
796 + $limits = array();
797 +
798 + foreach ( $coupon_lines as $coupon_line ) {
799 + $coupon = new \WC_Coupon( $coupon_line->get_code() );
800 +
801 + /** This filter is documented in includes/Illuminate/Helper.php */
802 + if ( ! apply_filters( 'subscrpt_coupon_is_recurring', false, $coupon, '' ) ) {
803 + continue;
804 + }
805 +
806 + /** This filter is documented in includes/Illuminate/Helper.php */
807 + $limit = (int) apply_filters( 'subscrpt_coupon_recurring_limit', 0, $coupon, '' );
808 +
809 + if ( $limit > 0 ) {
810 + $limits[] = $limit;
811 +
812 + // The next renewal is past the limit, so it will be charged full price.
813 + if ( $next_order_position > $limit ) {
814 + $result['exhausted'] = true;
815 + continue;
816 + }
817 + }
818 +
819 + $result['amount'] += (float) $coupon_line->get_discount();
820 + }
821 +
822 + $result['limit'] = empty( $limits ) ? 0 : min( $limits );
823 +
824 + // Rebase onto the current recurring price when it no longer matches what was discounted.
825 + $discounted_subtotal = $order_item ? (float) $order_item->get_subtotal() : 0.0;
826 + $recurring_subtotal = $order_item ? (float) self::get_subscription_total( $subscription_id ) * max( 1, (int) $order_item->get_quantity() ) : 0.0;
827 +
828 + if ( $result['amount'] > 0 && $discounted_subtotal > 0 && abs( $discounted_subtotal - $recurring_subtotal ) > 0.01 ) {
829 + $result['amount'] = $result['amount'] * ( $recurring_subtotal / $discounted_subtotal );
830 + }
831 +
832 + $cache[ $cache_key ] = $result;
833 +
834 + return $result;
835 + }
836 +
837 + /**
838 + * Build the figures the My Account subscription views display.
839 + *
840 + * The recurring price in `_subscrpt_price` is always the undiscounted product price, so the
841 + * renewal figure has to be derived: full price, minus whatever discount recurs, plus tax on
842 + * the discounted amount. Tax is scaled from the order item's own tax ratio rather than
843 + * recalculated, which keeps the figures consistent with the order they came from.
844 + *
845 + * @param int $subscription_id Subscription ID.
846 + * @param \WC_Order_Item|null $order_item Source order item.
847 + *
848 + * @return array{full_excl:float,discount:float,discount_tax:float,tax:float,total:float,has_discount:bool}
849 + */
850 + public static function get_subscription_display_totals( $subscription_id, $order_item = null ) {
851 + $quantity = $order_item ? max( 1, (int) $order_item->get_quantity() ) : 1;
852 + $full_excl = (float) self::get_subscription_total( $subscription_id ) * $quantity;
853 +
854 + $item_subtotal = $order_item ? (float) $order_item->get_subtotal() : 0.0;
855 + $item_tax = $order_item ? (float) $order_item->get_subtotal_tax() : 0.0;
856 + $tax_ratio = $item_subtotal > 0 ? $item_tax / $item_subtotal : 0.0;
857 +
858 + $order = $order_item ? wc_get_order( $order_item->get_order_id() ) : null;
859 + $discount = self::get_subscription_recurring_discount( $subscription_id, $order, $order_item );
860 + $discount = min( (float) $discount['amount'], $full_excl );
861 +
862 + $discount_tax = $discount * $tax_ratio;
863 + $tax = ( $full_excl * $tax_ratio ) - $discount_tax;
864 +
865 + return array(
866 + 'full_excl' => $full_excl,
867 + 'discount' => $discount,
868 + 'discount_tax' => $discount_tax,
869 + 'tax' => $tax,
870 + 'total' => $full_excl - $discount + $tax,
871 + 'has_discount' => $discount > 0,
872 + );
873 + }
874 +
875 + /**
876 + * Resolve the pieces every recurring-amount display needs.
877 + *
878 + * @param int $subscription_id Subscription ID.
879 + * @param \WC_Order_Item|null $order_item Source order item.
880 + *
881 + * @return array|false {discounted:string,full:string,has_discount:bool}, or false when the
882 + * order item is missing or carries no subscription meta.
883 + */
884 + protected static function get_subscription_recurring_price_parts( $subscription_id, $order_item = null ) {
885 + if ( ! $order_item ) {
886 + return false;
887 + }
888 +
889 + $totals = self::get_subscription_display_totals( $subscription_id, $order_item );
890 + $discounted = self::format_price_with_order_item( $totals['total'], $order_item->get_id() );
891 +
892 + if ( ! $discounted ) {
893 + return false;
894 + }
895 +
896 + // Undiscounted amount including its own tax.
897 + $full = $totals['full_excl'] + $totals['tax'] + $totals['discount_tax'];
898 + $order = wc_get_order( $order_item->get_order_id() );
899 +
900 + return array(
901 + 'discounted' => $discounted,
902 + 'full' => wc_price(
903 + $full,
904 + array(
905 + 'currency' => $order ? $order->get_currency() : '',
906 + )
907 + ),
908 + 'has_discount' => $totals['has_discount'],
909 + );
910 + }
911 +
912 + /**
913 + * Formatted recurring amount for a subscription, striking the original when a discount recurs.
914 + *
915 + * Produces the same `<del>` / `<ins>` shape the cart's recurring totals use, so a customer
916 + * sees one consistent treatment of a recurring discount from cart through to order details.
917 + * Use `get_subscription_recurring_price_text()` anywhere the output may reach a plain-text
918 + * context, such as an email that renders in both HTML and plain.
919 + *
920 + * @param int $subscription_id Subscription ID.
921 + * @param \WC_Order_Item|null $order_item Source order item.
922 + * @param array $args Optional. 'del_style' is an inline style for the struck-through
923 + * amount — email clients strip stylesheets, so email callers
924 + * must pass one.
925 + *
926 + * @return string|false Formatted price, or false when the order item has no subscription meta.
927 + */
928 + public static function get_subscription_recurring_price_html( $subscription_id, $order_item = null, $args = array() ) {
929 + $parts = self::get_subscription_recurring_price_parts( $subscription_id, $order_item );
930 +
931 + if ( ! $parts ) {
932 + return false;
933 + }
934 +
935 + if ( ! $parts['has_discount'] ) {
936 + return $parts['discounted'];
937 + }
938 +
939 + $del_style = $args['del_style'] ?? '';
940 + $del_attributes = $del_style ? ' style="' . esc_attr( $del_style ) . '"' : '';
941 +
942 + return '<del aria-hidden="true"' . $del_attributes . '>' . $parts['full'] . '</del> <ins>' . $parts['discounted'] . '</ins>';
943 + }
944 +
945 + /**
946 + * Formatted recurring amount for a subscription, as markup-free text.
947 + *
948 + * For contexts that cannot render `<del>` — plain-text emails above all, where stripping the
949 + * tags would leave two bare amounts side by side and no way to tell which is charged.
950 + *
951 + * @param int $subscription_id Subscription ID.
952 + * @param \WC_Order_Item|null $order_item Source order item.
953 + *
954 + * @return string|false Formatted price, or false when the order item has no subscription meta.
955 + */
956 + public static function get_subscription_recurring_price_text( $subscription_id, $order_item = null ) {
957 + $parts = self::get_subscription_recurring_price_parts( $subscription_id, $order_item );
958 +
959 + if ( ! $parts ) {
960 + return false;
961 + }
962 +
963 + $discounted = wp_strip_all_tags( $parts['discounted'] );
964 +
965 + if ( ! $parts['has_discount'] ) {
966 + return $discounted;
967 + }
968 +
969 + return sprintf(
970 + // translators: 1: discounted recurring amount, 2: original amount before the discount.
971 + __( '%1$s (discounted from %2$s)', 'subscription' ),
972 + $discounted,
973 + wp_strip_all_tags( $parts['full'] )
974 + );
975 + }
976 +
977 + /**
565 978 * Get recurrings items from cart items.
566 979 *
567 980 * @param array $cart_items Cart items.
568 981 *
@@ -573,25 +986,29 @@
573 986 foreach ( $cart_items as $key => $cart_item ) {
574 987 $product = $cart_item['data'];
575 988 if ( $product->is_type( 'simple' ) && isset( $cart_item['subscription'] ) ) {
576 989 $cart_subscription = $cart_item['subscription'];
577 - $type = ucfirst( $cart_subscription['type'] );
990 + // Cadence word must respect the frequency (plan items store the raw
991 + // plural interval, e.g. "months"): singular for 1, plural + count above.
992 + $sub_time = max( 1, (int) ( $cart_subscription['time'] ?? 1 ) );
993 + $type = ( 1 === $sub_time ? '' : $sub_time . ' ' ) . ucfirst( self::get_typos( $sub_time, $cart_subscription['type'] ) );
994 + $price_data = self::build_cart_recurring_price_data( $cart_item, $key, $type );
578 995
579 - // Total amount with tax
580 - $quantity = (int) $cart_item['quantity'];
581 - $total_amount = wc_get_price_including_tax( $product, [ 'qty' => $quantity ] );
582 - $timing_html = "<span class='wpsubs-subscription-timing'>&nbsp;/&nbsp;{$type}</span>";
583 - $price_html = wc_price( (float) $total_amount ) . $timing_html;
584 -
585 - $recurrs[ $key ] = array(
586 - 'trial_status' => ! is_null( $cart_subscription['trial'] ),
587 - 'price_html' => $price_html,
588 - 'start_date' => self::start_date( $cart_subscription['trial'] ),
589 - 'next_date' => self::next_date( ( $cart_subscription['time'] ?? 1 ) . ' ' . $cart_subscription['type'], $cart_subscription['trial'] ),
590 - 'can_user_cancel' => $cart_item['data']->get_meta( '_subscrpt_user_cancel' ),
591 - 'max_no_payment' => $cart_item['data']->get_meta( '_subscrpt_max_no_payment' ),
592 - 'price' => (float) $cart_subscription['per_cost'],
593 - 'quantity' => (int) $cart_item['quantity'],
996 + $recurrs[ $key ] = array_merge(
997 + $price_data,
998 + array(
999 + 'trial_status' => ! is_null( $cart_subscription['trial'] ),
1000 + 'start_date' => self::start_date( $cart_subscription['trial'] ),
1001 + 'next_date' => self::next_date( ( $cart_subscription['time'] ?? 1 ) . ' ' . $cart_subscription['type'], $cart_subscription['trial'] ),
1002 + 'can_user_cancel' => $cart_item['data']->get_meta( '_subscrpt_user_cancel' ),
1003 + 'max_no_payment' => ! empty( $cart_item['subscrpt_max_no_payment'] )
1004 + ? (int) $cart_item['subscrpt_max_no_payment']
1005 + : $cart_item['data']->get_meta( '_subscrpt_max_no_payment' ),
1006 + // Exact plan total for split items (the entered price the split is
1007 + // divided from); null for classic split items which have no plan total.
1008 + 'split_total' => isset( $cart_item['subscrpt_split_total'] ) ? (float) $cart_item['subscrpt_split_total'] : null,
1009 + 'quantity' => (int) $cart_item['quantity'],
1010 + )
594 1011 );
595 1012 }
596 1013 }
597 1014
@@ -628,8 +1045,9 @@
628 1045 /**
629 1046 * Create renewal order when subscription expired. [wip]
630 1047 *
631 1048 * @param int $subscription_id Subscription ID.
1049 + * @return false|\WC_Order Renewal order object or false on failure.
632 1050 * @throws \WC_Data_Exception Exception.
633 1051 * @throws \Exception Exception.
634 1052 */
635 1053 public static function create_renewal_order( $subscription_id ) {
@@ -659,9 +1077,9 @@
659 1077 }
660 1078
661 1079 if ( ! $old_order ) {
662 1080 subscrpt_write_log( "Old order not found for renewal. Skipping creating renewal order. [ Subscription ID: {$subscription_id} ]" );
663 - return;
1081 + return false;
664 1082 }
665 1083
666 1084 $order_item = $old_order->get_item( $order_item_id );
667 1085 $subscription_price = (float) get_post_meta( $subscription_id, '_subscrpt_price', true );
@@ -686,9 +1104,9 @@
686 1104 // creating new order.
687 1105 $new_order_data = self::create_new_order_for_renewal( $old_order, $order_item, $product_args );
688 1106 if ( ! $new_order_data ) {
689 1107 subscrpt_write_log( "Failed to create renewal order. [ Subscription ID: {$subscription_id} ]" );
690 - return;
1108 + return false;
691 1109 }
692 1110 $new_order = $new_order_data['order'];
693 1111 $new_order_item_id = $new_order_data['order_item_id'];
694 1112
@@ -961,9 +1379,9 @@
961 1379
962 1380 $comment_id = wp_insert_comment(
963 1381 array(
964 1382 'comment_author' => 'Subscription for WooCommerce',
965 - 'comment_content' => sprintf( 'Subscription Renewal order successfully created. order is %s', $new_order_id ),
1383 + 'comment_content' => sprintf( 'Subscription Renewal order successfully created. Order #%s', $new_order_id ),
966 1384 'comment_post_ID' => $subscription_id,
967 1385 'comment_type' => 'order_note',
968 1386 )
969 1387 );
@@ -982,9 +1400,9 @@
982 1400 return null;
983 1401 }
984 1402
985 1403 $subs_post = get_post( $subscription_id );
986 - $user_id = (int) $subs_post->post_author ?? 0;
1404 + $user_id = ! empty( $subs_post ) ? (int) $subs_post->post_author : 0;
987 1405
988 1406 $product_id = get_post_meta( $subscription_id, '_subscrpt_product_id', true );
989 1407 $product_id = ! empty( $product_id ) ? (int) $product_id : 0;
990 1408
@@ -992,9 +1410,9 @@
992 1410 $variation_id = ! empty( $variation_id ) ? (int) $variation_id : 0;
993 1411
994 1412 $chk_product_id = $variation_id ? $variation_id : $product_id;
995 1413
996 - $status = get_post_status( $subscription_id );
1414 + $status = get_post_status( $subscription_id ); // pending, active, cancelled, pe_cancelled, expired
997 1415 $price = get_post_meta( $subscription_id, '_subscrpt_price', true );
998 1416
999 1417 $signup_fee = get_post_meta( $subscription_id, '_subscrpt_signup_fee', true );
1000 1418 $signup_fee = ! empty( $signup_fee ) ? $signup_fee : 0;
@@ -1062,9 +1480,10 @@
1062 1480 );
1063 1481 }
1064 1482
1065 1483 if (
1066 - ! in_array( strtolower( $status ), array( 'cancelled', 'pending' ), true )
1484 + ! in_array( strtolower( $status ), array( 'cancelled', 'pending', 'completed' ), true )
1485 + && ! empty( $next_date )
1067 1486 && $next_datetime - time() <= 0
1068 1487 && (int) $default_grace_period > 0
1069 1488 ) {
1070 1489 $subscription_data['grace_period'] = array(