blob: dded80f95f010decc0139129f079503a8e0f4187 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5}
6
7/**
8 * Trait for Subscriptions utility functions.
9 *
10 * @since 5.6.0
11 */
12trait WC_Stripe_Subscriptions_Utilities_Trait {
13
14 /**
15 * Checks if subscriptions are enabled on the site.
16 *
17 * @since 5.6.0
18 *
19 * @return bool Whether subscriptions is enabled or not.
20 */
21 public function is_subscriptions_enabled() {
22 return class_exists( 'WC_Subscriptions' ) && version_compare( WC_Subscriptions::$version, '2.2.0', '>=' );
23 }
24
25 /**
26 * Is $order_id a subscription?
27 *
28 * @since 5.6.0
29 *
30 * @param int $order_id
31 * @return boolean
32 */
33 public function has_subscription( $order_id ) {
34 return ( function_exists( 'wcs_order_contains_subscription' ) && ( wcs_order_contains_subscription( $order_id ) || wcs_is_subscription( $order_id ) || wcs_order_contains_renewal( $order_id ) ) );
35 }
36
37 /**
38 * Returns whether this user is changing the payment method for a subscription.
39 *
40 * @since 5.6.0
41 *
42 * @return bool
43 */
44 public function is_changing_payment_method_for_subscription() {
45 if ( isset( $_GET['change_payment_method'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
46 return wcs_is_subscription( wc_clean( wp_unslash( $_GET['change_payment_method'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
47 }
48 return false;
49 }
50
51 /**
52 * Returns boolean value indicating whether payment for an order will be recurring,
53 * as opposed to single.
54 *
55 * @since 5.6.0
56 *
57 * @param int $order_id ID for corresponding WC_Order in process.
58 *
59 * @return bool
60 */
61 public function is_payment_recurring( $order_id ) {
62 if ( ! $this->is_subscriptions_enabled() ) {
63 return false;
64 }
65 return $this->is_changing_payment_method_for_subscription() || $this->has_subscription( $order_id );
66 }
67
68 /**
69 * Returns a boolean value indicating whether the save payment checkbox should be
70 * displayed during checkout.
71 *
72 * Returns `false` if the cart currently has a subscriptions or if the request has a
73 * `change_payment_method` GET parameter. Returns the value in `$display` otherwise.
74 *
75 * @since 5.6.0
76 *
77 * @param bool $display Bool indicating whether to show the save payment checkbox in the absence of subscriptions.
78 *
79 * @return bool Indicates whether the save payment method checkbox should be displayed or not.
80 */
81 public function display_save_payment_method_checkbox( $display ) {
82 if ( WC_Subscriptions_Cart::cart_contains_subscription() || $this->is_changing_payment_method_for_subscription() ) {
83 return false;
84 }
85 // Only render the "Save payment method" checkbox if there are no subscription products in the cart.
86 return $display;
87 }
88
89 /**
90 * Returns boolean on whether current WC_Cart or WC_Subscriptions_Cart
91 * contains a subscription or subscription renewal item
92 *
93 * @since 5.6.0
94 *
95 * @return bool
96 */
97 public function is_subscription_item_in_cart() {
98 if ( $this->is_subscriptions_enabled() ) {
99 return WC_Subscriptions_Cart::cart_contains_subscription() || $this->cart_contains_renewal();
100 }
101 return false;
102 }
103
104 /**
105 * Checks the cart to see if it contains a subscription product renewal.
106 *
107 * @since 5.6.0
108 *
109 * @return mixed The cart item containing the renewal as an array, else false.
110 */
111 public function cart_contains_renewal() {
112 if ( ! function_exists( 'wcs_cart_contains_renewal' ) ) {
113 return false;
114 }
115 return wcs_cart_contains_renewal();
116 }
117
118}