PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.2.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.2.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/Frontend/Product.php +100 -15 1.3.2 → 2.2.0 View file →
@@ -1,10 +1,21 @@
1 1 <?php
2 +/**
3 + * Storefront product handling for subscriptions.
4 + *
5 + * @package SpringDevs\Subscription
6 + */
2 7
3 8 namespace SpringDevs\Subscription\Frontend;
4 9
5 10 use SpringDevs\Subscription\Illuminate\Helper;
11 +use SpringDevs\Subscription\Illuminate\Subscription\Subscription;
6 12
13 +// HPOS: This file is compatible with WooCommerce High-Performance Order Storage (HPOS).
14 +// All WooCommerce order data is accessed via WooCommerce CRUD methods (wc_get_order, wc_get_order_item_meta, etc.).
15 +// All direct post meta access is for subscription data only, not WooCommerce order data.
16 +// If you add new order data access, use WooCommerce CRUD for HPOS compatibility.
17 +
7 18 /**
8 19 * Product class
9 20 * control single product page
10 21 */
@@ -22,9 +33,15 @@
22 33 ),
23 34 10,
24 35 2
25 36 );
26 - add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'change_single_add_to_cart_text' ), 10, 2 );
37 + add_filter( 'woocommerce_product_add_to_cart_text', array( $this, 'change_loop_add_to_cart_text' ), 10, 2 );
38 + // Plan-tied simple products behave like a variable product on the shop /
39 + // Product Collection loops: a "Select options" link to the product page (so
40 + // the customer picks a plan), never a direct/ajax add-to-cart. These native
41 + // product-method filters drive both the classic loop and the block button.
42 + add_filter( 'woocommerce_product_add_to_cart_url', array( $this, 'plan_loop_add_to_cart_url' ), 10, 2 );
43 + add_filter( 'woocommerce_product_supports', array( $this, 'plan_loop_disable_ajax' ), 10, 3 );
27 44 add_filter( 'woocommerce_get_price_html', array( $this, 'change_price_html' ), 10, 2 );
28 45 add_filter( 'woocommerce_quantity_input_args', array( $this, 'update_input_args' ), 10, 2 );
29 46 add_filter( 'woocommerce_add_to_cart', array( $this, 'update_cart_quantity' ), 10, 3 );
30 47 add_filter(
@@ -67,9 +84,9 @@
67 84 *
68 85 * @return mixed
69 86 */
70 87 public function remove_button_active_products( $button, $product ) {
71 - $product = sdevs_get_subscription_product( $product );
88 + $product = Subscription::get_subs_product( $product );
72 89 if ( ! $product->is_type( 'simple' ) ) {
73 90 return $button;
74 91 }
75 92
@@ -92,9 +109,9 @@
92 109 * Display notice if already purchased.
93 110 */
94 111 public function text_if_active() {
95 112 global $product;
96 - $sdevs_product = sdevs_get_subscription_product( $product );
113 + $sdevs_product = Subscription::get_subs_product( $product );
97 114 if ( ! $sdevs_product->is_type( 'simple' ) ) {
98 115 return;
99 116 }
100 117
@@ -103,18 +120,18 @@
103 120 if ( 'unlimited' === $limit ) {
104 121 return;
105 122 }
106 123 if ( 'one' === $limit ) {
107 - $unexpired = Helper::subscription_exists( $product->get_id(), array( 'active', 'pending' ) );
124 + $unexpired = Helper::subscription_exists( $sdevs_product->get_id(), array( 'active', 'pending' ) );
108 125 if ( ! $unexpired ) {
109 126 return false;
110 127 } else {
111 - echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'sdevs_subscrpt' ) . '</strong>';
128 + echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'subscription' ) . '</strong>';
112 129 }
113 130 }
114 131 if ( 'only_one' === $limit ) {
115 - if ( ! Helper::check_trial( $product->get_id() ) ) {
116 - echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'sdevs_subscrpt' ) . '</strong>';
132 + if ( ! Helper::check_trial( $sdevs_product->get_id() ) ) {
133 + echo '<strong>' . esc_html_e( 'You Already Subscribed These Product!', 'subscription' ) . '</strong>';
117 134 }
118 135 }
119 136 }
120 137 }
@@ -127,9 +144,9 @@
127 144 *
128 145 * @return boolean
129 146 */
130 147 public function check_if_purchasable( $is_purchasable, $product ) {
131 - $product = sdevs_get_subscription_product( $product );
148 + $product = Subscription::get_subs_product( $product );
132 149 if ( $product->is_enabled() ) {
133 150 $limit = $product->get_limit();
134 151 if ( 'unlimited' === $limit ) {
135 152 return true;
@@ -233,9 +250,9 @@
233 250 * @param string $text Add-to-cart button Text.
234 251 * @param \WC_Product $product Product Object.
235 252 */
236 253 public function change_single_add_to_cart_text( $text, $product ) {
237 - $product = sdevs_get_subscription_product( $product );
254 + $product = Subscription::get_subs_product( $product );
238 255 if ( $product->is_type( 'variable' ) || '' === $product->get_price() ) {
239 256 return $text;
240 257 }
241 258 $cart_btn_label = $product->get_button_label();
@@ -240,9 +257,9 @@
240 257 }
241 258 $cart_btn_label = $product->get_button_label();
242 259 $expired = Helper::subscription_exists( $product->get_id(), 'expired' );
243 260 if ( $expired ) {
244 - $text = __( 'Renew', 'sdevs_subscrpt' );
261 + $text = __( 'Renew', 'subscription' );
245 262 } elseif ( $product->is_enabled() && $cart_btn_label ) {
246 263 $text = $cart_btn_label;
247 264 }
248 265
@@ -249,8 +266,71 @@
249 266 return $text;
250 267 }
251 268
252 269 /**
270 + * Whether this is a plan-tied simple product that should behave like a
271 + * variable product on the shop / Product Collection loops.
272 + *
273 + * @param mixed $product Product object.
274 + *
275 + * @return bool
276 + */
277 + private function is_plan_loop_product( $product ) {
278 + return $product instanceof \WC_Product
279 + && $product->is_type( 'simple' )
280 + && subscrpt_plan_offered( $product->get_id() );
281 + }
282 +
283 + /**
284 + * Loop / block add-to-cart button text: "Select options" for plan products,
285 + * otherwise the classic label logic.
286 + *
287 + * @param string $text Add-to-cart button text.
288 + * @param \WC_Product $product Product object.
289 + *
290 + * @return string
291 + */
292 + public function change_loop_add_to_cart_text( $text, $product ) {
293 + if ( $this->is_plan_loop_product( $product ) ) {
294 + return __( 'Select options', 'subscription' );
295 + }
296 +
297 + return $this->change_single_add_to_cart_text( $text, $product );
298 + }
299 +
300 + /**
301 + * Loop / block add-to-cart URL: the product page for plan products, so the
302 + * customer lands on the plan selector instead of a bare add-to-cart.
303 + *
304 + * @param string $url Add-to-cart URL.
305 + * @param \WC_Product $product Product object.
306 + *
307 + * @return string
308 + */
309 + public function plan_loop_add_to_cart_url( $url, $product ) {
310 + return $this->is_plan_loop_product( $product ) ? $product->get_permalink() : $url;
311 + }
312 +
313 + /**
314 + * Disable ajax add-to-cart for plan products so their loop / block button
315 + * renders as a "Select options" link (navigates to the product page) rather
316 + * than an ajax add.
317 + *
318 + * @param bool $supports Whether the feature is supported.
319 + * @param string $feature Feature name.
320 + * @param \WC_Product $product Product object.
321 + *
322 + * @return bool
323 + */
324 + public function plan_loop_disable_ajax( $supports, $feature, $product ) {
325 + if ( 'ajax_add_to_cart' === $feature && $this->is_plan_loop_product( $product ) ) {
326 + return false;
327 + }
328 +
329 + return $supports;
330 + }
331 +
332 + /**
253 333 * Add trial, signup fee etc. with product price.
254 334 *
255 335 * @param mixed $price Price.
256 336 * @param \WC_Product $product Product.
@@ -257,9 +337,9 @@
257 337 *
258 338 * @return mixed
259 339 */
260 340 public function change_price_html( $price, $product ) {
261 - $product = sdevs_get_subscription_product( $product );
341 + $product = Subscription::get_subs_product( $product );
262 342 if ( ! $product->is_type( 'simple' ) || '' === $price ) {
263 343 return $price;
264 344 }
265 345
@@ -264,16 +344,21 @@
264 344 }
265 345
266 346 if ( $product->is_enabled() ) :
267 347 $timing_option = $product->get_timing_option();
268 - $type = Helper::get_typos( 1, $timing_option );
269 - $trial = null;
348 + $type = ucfirst( Helper::get_typos( 1, $timing_option, true ) );
349 +
350 + $trial = null;
270 351 if ( $product->has_trial() ) {
271 352 $meta_trial_time = $product->get_trial_timing_per();
272 - $trial = '<br/><small> + Get ' . $meta_trial_time . ' ' . Helper::get_typos( $meta_trial_time, $product->get_trial_timing_option() ) . ' free trial!</small>';
353 + $trial_type = Helper::get_typos( $meta_trial_time, $product->get_trial_timing_option(), true );
354 + $trial = '<br/><small> + Get ' . $meta_trial_time . ' ' . ucfirst( $trial_type ) . ' free trial!</small>';
273 355 }
274 356
275 - return apply_filters( 'subscrpt_simple_price_html', ( $price . ' / ' . $type . $trial ), $product, $price, $trial );
357 + $timing_html = "<span class='wpsubs-subscription-timing'>&nbsp;/&nbsp;{$type}</span>";
358 + $price_html = $price . $timing_html . $trial;
359 +
360 + return apply_filters( 'subscrpt_simple_price_html', $price_html, $product, $price, $trial );
276 361 else :
277 362 return $price;
278 363 endif;
279 364 }