PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / trunk
Subscriptions for WooCommerce with Stripe Recurring Payments vtrunk
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 1.5.2 All 60 releases
subscription / includes / Frontend / Product.php

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

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