PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.3.1
Subscriptions for WooCommerce with Stripe Recurring Payments v1.3.1
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 / Admin / Product.php

Product.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.3.1, at includes/Admin/Product.php

188 lines 6.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\Admin;
4
5 use SpringDevs\Subscription\Illuminate\Helper;
6
7 /**
8 * Product class
9 *
10 * @package SpringDevs\Subscription\Admin
11 */
12 class Product {
13
14 /**
15 * Initialize the class
16 */
17 public function __construct() {
18 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
19 add_filter( 'woocommerce_product_data_tabs', array( $this, 'register_tab' ) );
20 add_action( 'woocommerce_product_data_panels', array( $this, 'subscription_forms' ) );
21 add_filter( 'product_type_options', array( $this, 'add_product_type_options' ) );
22 add_action( 'save_post_product', array( $this, 'save_subscrpt_data' ) );
23 add_filter( 'woocommerce_get_price_html', array( $this, 'change_price_html' ), 10, 2 );
24 }
25
26 /**
27 * Add trial, signup fee etc. with product price.
28 *
29 * @param string $price Price.
30 * @param \WC_Product $product Product.
31 *
32 * @return string
33 */
34 public function change_price_html( $price, $product ) {
35 if ( $product->is_type( 'variable' ) || '' === $price || subscrpt_pro_activated() ) {
36 return $price;
37 }
38
39 $enabled = $product->get_meta( '_subscrpt_enabled' );
40 if ( $enabled ) :
41 $type = Helper::get_typos( 1, $product->get_meta( '_subscrpt_timing_option' ) );
42 $meta_trial_time = $product->get_meta( '_subscrpt_trial_timing_per' );
43 $trial = null;
44 if ( ! empty( $meta_trial_time ) && $meta_trial_time > 0 ) {
45 $trial = '<br/> + Get ' . $meta_trial_time . ' ' . Helper::get_typos( $meta_trial_time, $product->get_meta( '_subscrpt_trial_timing_option' ) ) . ' free trial!';
46 }
47
48 $price_html = $price . ' / ' . $type . $trial;
49 return $price_html;
50 else :
51 return $price;
52 endif;
53 }
54
55 /**
56 * Enqueue Assets.
57 */
58 public function enqueue_assets() {
59 wp_enqueue_script( 'sdevs_subscription_admin' );
60 }
61
62 /**
63 * Register "Subscription" option tab.
64 *
65 * @param array $tabs Tabs.
66 *
67 * @return array
68 */
69 public function register_tab( $tabs ) {
70 $tabs['sdevs_subscription'] = array(
71 'label' => __( 'Subscription', 'sdevs_subscrpt' ),
72 'class' => array( 'show_if_simple', 'show_if_subscription' ),
73 'target' => 'sdevs_subscription_options',
74 'priority' => 11,
75 );
76 return $tabs;
77 }
78
79 /**
80 * Display Enable Subscription Checkbox on product data tab.
81 *
82 * @param array $product_type_options Product Type options on Data tab.
83 *
84 * @return array
85 */
86 public function add_product_type_options( $product_type_options ) {
87 $screen = get_current_screen();
88 $value = 'no';
89 if ( 'edit' === $screen->parent_base ) {
90 $product = wc_get_product( get_the_ID() );
91 if ( $product ) {
92 $value = $product->get_meta( '_subscrpt_enabled' ) ? 'yes' : 'no';
93 }
94 }
95
96 $wrapper_class = apply_filters( 'subscrpt_simple_enable_checkbox_classes', 'show_if_simple' );
97 $product_type_options['subscrpt_enable'] = array(
98 'id' => 'subscrpt_enable',
99 'wrapper_class' => $wrapper_class,
100 'label' => __( 'Subscription', 'sdevs_subscrpt' ),
101 'description' => __( 'Enable Subscriptions', 'sdevs_subscrpt' ),
102 'default' => $value,
103 );
104
105 return $product_type_options;
106 }
107
108 /**
109 * Display forms on product create/edit.
110 */
111 public function subscription_forms() {
112 if ( function_exists( 'subscrpt_pro_activated' ) ) {
113 if ( subscrpt_pro_activated() ) {
114 do_action( 'subscrpt_simple_pro_fields', get_the_ID() );
115 } else {
116 $timing_types = array(
117 'days' => __( 'Daily', 'sdevs_subscrpt' ),
118 'weeks' => __( 'Weekly', 'sdevs_subscrpt' ),
119 'months' => __( 'Monthly', 'sdevs_subscrpt' ),
120 'years' => __( 'Yearly', 'sdevs_subscrpt' ),
121 );
122 $trial_timing_types = get_timing_types();
123 $subscrpt_timing = null;
124 $subscrpt_trial_time = null;
125 $subscrpt_trial_timing = null;
126 $subscrpt_cart_txt = 'subscribe';
127 $subscrpt_user_cancell = 'yes';
128 $subscrpt_limit = 'one';
129
130 $screen = get_current_screen();
131 if ( 'edit' === $screen->parent_base ) {
132 $product = wc_get_product( get_the_ID() );
133 if ( $product ) {
134 $subscrpt_timing = $product->get_meta( '_subscrpt_timing_option' );
135 $subscrpt_trial_time = $product->get_meta( '_subscrpt_trial_timing_per' );
136 $subscrpt_trial_timing = $product->get_meta( '_subscrpt_trial_timing_option' );
137 $subscrpt_cart_txt = $product->get_meta( '_subscrpt_cart_btn_label' );
138 $subscrpt_user_cancell = $product->get_meta( '_subscrpt_user_cancel' );
139 $subscrpt_limit = $product->get_meta( '_subscrpt_limit' );
140 }
141 }
142 include 'views/product-form.php';
143 }
144 }
145 }
146
147 /**
148 * Save subscription settings.
149 *
150 * @param int $product_id Product Id.
151 *
152 * @return void
153 */
154 public function save_subscrpt_data( $product_id ) {
155 if ( function_exists( 'subscrpt_pro_activated' ) ) {
156 if ( subscrpt_pro_activated() ) {
157 return;
158 }
159 }
160
161 if ( ! isset( $_POST['_subscript_nonce'], $_POST['subscrpt_timing'], $_POST['subscrpt_cart_txt'], $_POST['subscrpt_user_cancel'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_subscript_nonce'] ) ), '_subscript_edit_product_nonce' ) ) {
162 return;
163 }
164
165 remove_action( 'save_post_product', array( $this, 'save_subscrpt_data' ) );
166
167 $subscrpt_enable = isset( $_POST['subscrpt_enable'] );
168 $subscrpt_timing = sanitize_text_field( wp_unslash( $_POST['subscrpt_timing'] ) );
169 $subscrpt_trial_time = sanitize_text_field( wp_unslash( $_POST['subscrpt_trial_time'] ) );
170 $subscrpt_trial_timing = sanitize_text_field( wp_unslash( $_POST['subscrpt_trial_timing'] ) );
171 $subscrpt_cart_txt = sanitize_text_field( wp_unslash( $_POST['subscrpt_cart_txt'] ) );
172 $subscrpt_user_cancel = sanitize_text_field( wp_unslash( $_POST['subscrpt_user_cancel'] ) );
173 $subscrpt_limit = isset( $_POST['subscrpt_limit'] ) ? sanitize_text_field( wp_unslash( $_POST['subscrpt_limit'] ) ) : null;
174
175 $product = wc_get_product( $product_id );
176 $product->update_meta_data( '_subscrpt_enabled', $subscrpt_enable );
177 $product->update_meta_data( '_subscrpt_timing_option', $subscrpt_timing );
178 $product->update_meta_data( '_subscrpt_trial_timing_per', $subscrpt_trial_time );
179 $product->update_meta_data( '_subscrpt_trial_timing_option', $subscrpt_trial_timing );
180 $product->update_meta_data( '_subscrpt_cart_btn_label', $subscrpt_cart_txt );
181 $product->update_meta_data( '_subscrpt_user_cancel', $subscrpt_user_cancel );
182 $product->update_meta_data( '_subscrpt_limit', $subscrpt_limit );
183 $product->save();
184
185 add_action( 'save_post_product', array( $this, 'save_subscrpt_data' ) );
186 }
187 }
188