PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.4.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.4.2
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 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Frontend / Product.php

Product.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.4.2, at includes/Frontend/Product.php

286 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Frontend;
4
5 use SpringDevs\Subscription\Illuminate\Helper;
6
7 // HPOS: This file is compatible with WooCommerce High-Performance Order Storage (HPOS).
8 // All WooCommerce order data is accessed via WooCommerce CRUD methods (wc_get_order, wc_get_order_item_meta, etc.).
9 // All direct post meta access is for subscription data only, not WooCommerce order data.
10 // If you add new order data access, use WooCommerce CRUD for HPOS compatibility.
11
12 /**
13 * Product class
14 * control single product page
15 */
16 class Product {
17
18 /**
19 * Initialize the class
20 */
21 public function __construct() {
22 add_filter(
23 'woocommerce_product_single_add_to_cart_text',
24 array(
25 $this,
26 'change_single_add_to_cart_text',
27 ),
28 10,
29 2
30 );
31 add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'change_single_add_to_cart_text' ), 10, 2 );
32 add_filter( 'woocommerce_get_price_html', array( $this, 'change_price_html' ), 10, 2 );
33 add_filter( 'woocommerce_quantity_input_args', array( $this, 'update_input_args' ), 10, 2 );
34 add_filter( 'woocommerce_add_to_cart', array( $this, 'update_cart_quantity' ), 10, 3 );
35 add_filter(
36 'woocommerce_store_api_product_quantity_minimum',
37 array(
38 $this,
39 'update_quantity_min_max',
40 ),
41 10,
42 2
43 );
44 add_filter(
45 'woocommerce_store_api_product_quantity_maximum',
46 array(
47 $this,
48 'update_quantity_min_max',
49 ),
50 10,
51 2
52 );
53 add_action(
54 'woocommerce_after_cart_item_quantity_update',
55 array(
56 $this,
57 'validate_quantity_on_manual_renewal',
58 ),
59 10,
60 2
61 );
62 add_filter( 'woocommerce_is_purchasable', array( $this, 'check_if_purchasable' ), 20, 2 );
63 add_action( 'woocommerce_single_product_summary', array( $this, 'text_if_active' ) );
64 add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'remove_button_active_products' ), 10, 2 );
65 }
66
67 /**
68 * Remove button if product already subscribed.
69 *
70 * @param mixed $button Button.
71 * @param \WC_Product $product Product.
72 *
73 * @return mixed
74 */
75 public function remove_button_active_products( $button, $product ) {
76 $product = sdevs_get_subscription_product( $product );
77 if ( ! $product->is_type( 'simple' ) ) {
78 return $button;
79 }
80
81 if ( $product->is_enabled() ) {
82 $limit = $product->get_limit();
83 if ( 'one' === $limit ) {
84 $unexpired = Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) );
85 if ( $unexpired ) {
86 return;
87 }
88 } elseif ( 'only_one' === $limit && ! Helper::check_trial( $product->get_id() ) ) {
89 return;
90 }
91 }
92
93 return $button;
94 }
95
96 /**
97 * Display notice if already purchased.
98 */
99 public function text_if_active() {
100 global $product;
101 $sdevs_product = sdevs_get_subscription_product( $product );
102 if ( ! $sdevs_product->is_type( 'simple' ) ) {
103 return;
104 }
105
106 if ( $sdevs_product->is_enabled() ) {
107 $limit = $sdevs_product->get_limit();
108 if ( 'unlimited' === $limit ) {
109 return;
110 }
111 if ( 'one' === $limit ) {
112 $unexpired = Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) );
113 if ( ! $unexpired ) {
114 return false;
115 } else {
116 echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'sdevs_subscrpt' ) . '</strong>';
117 }
118 }
119 if ( 'only_one' === $limit ) {
120 if ( ! Helper::check_trial( $product->get_id() ) ) {
121 echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'sdevs_subscrpt' ) . '</strong>';
122 }
123 }
124 }
125 }
126
127 /**
128 * Check if product purchasable.
129 *
130 * @param boolean $is_purchasable True\False.
131 * @param \WC_Product $product Product.
132 *
133 * @return boolean
134 */
135 public function check_if_purchasable( $is_purchasable, $product ) {
136 $product = sdevs_get_subscription_product( $product );
137 if ( $product->is_enabled() ) {
138 $limit = $product->get_limit();
139 if ( 'unlimited' === $limit ) {
140 return true;
141 } elseif ( 'only_one' === $limit ) {
142 return Helper::check_trial( $product->get_id() );
143 } elseif ( 'one' === $limit ) {
144 return ! Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) );
145 }
146 }
147
148 return $is_purchasable;
149 }
150
151 /**
152 * Validate quantity after add to cart cart on manual renewal process.
153 *
154 * @param string $cart_item_key Cart Item Key.
155 * @param int $quantity Quantity.
156 *
157 * @return void
158 */
159 public function validate_quantity_on_manual_renewal( $cart_item_key, $quantity ) {
160 $cart_item = WC()->cart->get_cart_item( $cart_item_key );
161 $expired = Helper::subscription_exists( $cart_item['data']->get_id(), 'expired' );
162 if ( $expired ) {
163 $order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true );
164 $item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true );
165 if ( $item_quantity !== $quantity ) {
166 WC()->cart->set_quantity( $cart_item_key, $item_quantity );
167 wc_add_notice( 'You can only add ' . $item_quantity . ' ' . ( $item_quantity > 1 ? 'items' : 'item' ) . ' on cart!', 'error' );
168 }
169 }
170 }
171
172 /**
173 * Update quantity min max for renewal process.
174 *
175 * @param int $value Value.
176 * @param \WC_Product $product Product Object.
177 *
178 * @return int
179 */
180 public function update_quantity_min_max( $value, $product ) {
181 $expired = Helper::subscription_exists( $product->get_id(), 'expired' );
182 if ( $expired ) {
183 $order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true );
184 $item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true );
185
186 return $item_quantity;
187 }
188
189 return $value;
190 }
191
192 /**
193 * Update cart quantity on manual renewal process.
194 *
195 * @param string $cart_item_key Cart item key.
196 * @param int $product_id Product Id.
197 * @param int $quantity Quantity.
198 *
199 * @return void
200 */
201 public function update_cart_quantity( $cart_item_key, $product_id, $quantity ) {
202 $expired = Helper::subscription_exists( $product_id, 'expired' );
203 if ( $expired ) {
204 $order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true );
205 $item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true );
206 if ( $item_quantity !== $quantity ) {
207 WC()->cart->set_quantity( $cart_item_key, $item_quantity );
208 wc_add_notice( 'You can only add ' . $item_quantity . ' ' . ( $item_quantity > 1 ? 'items' : 'item' ) . ' on cart!', 'error' );
209 }
210 }
211 }
212
213 /**
214 * Update quantity input args.
215 *
216 * @param array $args args.
217 * @param \WC_Product $product Product object.
218 *
219 * @return array
220 */
221 public function update_input_args( $args, $product ) {
222 $expired = Helper::subscription_exists( $product->get_id(), 'expired' );
223 if ( $expired ) {
224 $order_item_id = get_post_meta( $expired, '_subscrpt_order_item_id', true );
225 $item_quantity = (int) wc_get_order_item_meta( $order_item_id, '_qty', true );
226 $args['input_value'] = $item_quantity;
227 $args['min_value'] = $item_quantity;
228 $args['max_value'] = $item_quantity;
229 $args['step'] = 1;
230 }
231
232 return $args;
233 }
234
235 /**
236 * Change single product add-to-cart button text.
237 *
238 * @param string $text Add-to-cart button Text.
239 * @param \WC_Product $product Product Object.
240 */
241 public function change_single_add_to_cart_text( $text, $product ) {
242 $product = sdevs_get_subscription_product( $product );
243 if ( $product->is_type( 'variable' ) || '' === $product->get_price() ) {
244 return $text;
245 }
246 $cart_btn_label = $product->get_button_label();
247 $expired = Helper::subscription_exists( $product->get_id(), 'expired' );
248 if ( $expired ) {
249 $text = __( 'Renew', 'sdevs_subscrpt' );
250 } elseif ( $product->is_enabled() && $cart_btn_label ) {
251 $text = $cart_btn_label;
252 }
253
254 return $text;
255 }
256
257 /**
258 * Add trial, signup fee etc. with product price.
259 *
260 * @param mixed $price Price.
261 * @param \WC_Product $product Product.
262 *
263 * @return mixed
264 */
265 public function change_price_html( $price, $product ) {
266 $product = sdevs_get_subscription_product( $product );
267 if ( ! $product->is_type( 'simple' ) || '' === $price ) {
268 return $price;
269 }
270
271 if ( $product->is_enabled() ) :
272 $timing_option = $product->get_timing_option();
273 $type = Helper::get_typos( 1, $timing_option );
274 $trial = null;
275 if ( $product->has_trial() ) {
276 $meta_trial_time = $product->get_trial_timing_per();
277 $trial = '<br/><small> + Get ' . $meta_trial_time . ' ' . Helper::get_typos( $meta_trial_time, $product->get_trial_timing_option() ) . ' free trial!</small>';
278 }
279
280 return apply_filters( 'subscrpt_simple_price_html', ( $price . ' / ' . $type . $trial ), $product, $price, $trial );
281 else :
282 return $price;
283 endif;
284 }
285 }
286