PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.3.2
Subscriptions for WooCommerce with Stripe Recurring Payments v1.3.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.3.2, at includes/Frontend/Product.php

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