PluginProbe ʕ •ᴥ•ʔ
Bopo – WooCommerce Product Bundle Builder / trunk
Bopo – WooCommerce Product Bundle Builder vtrunk
bopo-woo-product-bundle-builder / includes / functions.php
bopo-woo-product-bundle-builder / includes Last commit date
elementor 1 year ago data.php 4 years ago functions.php 1 year ago helper.php 1 year ago includes.php 4 years ago support.php 2 months ago
functions.php
258 lines
1 <?php
2
3 /**
4 * Function include all files in folder
5 *
6 * @param $path Directory address
7 * @param $ext array file extension what will include
8 * @param $prefix string Class prefix
9 */
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13 if ( ! function_exists( 'vi_include_folder' ) ) {
14 function vi_include_folder( $path, $prefix = '', $ext = array( 'php' ) ) {
15
16 /*Include all files in payment folder*/
17 if ( ! is_array( $ext ) ) {
18 $ext = explode( ',', $ext );
19 $ext = array_map( 'trim', $ext );
20 }
21 $sfiles = scandir( $path );
22 foreach ( $sfiles as $sfile ) {
23 if ( $sfile != '.' && $sfile != '..' ) {
24 if ( is_file( $path . "/" . $sfile ) ) {
25 $ext_file = pathinfo( $path . "/" . $sfile );
26 $file_name = $ext_file['filename'];
27 if ( $ext_file['extension'] ) {
28 if ( in_array( $ext_file['extension'], $ext ) ) {
29 $class = preg_replace( '/\W/i', '_', $prefix . ucfirst( $file_name ) );
30
31 if ( ! class_exists( $class ) ) {
32 require_once $path . $sfile;
33 if ( class_exists( $class ) ) {
34 new $class;
35 }
36 }
37 }
38 }
39 }
40 }
41 }
42 }
43 }
44
45 if ( ! function_exists( 'bopobb_woocommerce_version_check' ) ) {
46 function bopobb_woocommerce_version_check( $version = '3.0' ) {
47 global $woocommerce;
48
49 if ( version_compare( $woocommerce->version, $version, ">=" ) ) {
50 return true;
51 }
52
53 return false;
54 }
55 }
56
57 if ( ! function_exists( 'bopobb_sanitize_block' ) ) {
58 function bopobb_sanitize_block( $var ) {
59 return stripslashes( $var );
60 }
61 }
62
63 if ( ! function_exists( 'bopobb_get_template' ) ) {
64 function bopobb_get_template( $template_name, $args = array(), $template_path = '', $default_path = '' ) {
65 if ( ! empty( $args ) && is_array( $args ) ) {
66 extract( $args );
67 }
68
69 $located = $default_path . $template_name;
70
71 if ( ! file_exists( $located ) ) {
72 wc_doing_it_wrong( __FUNCTION__, sprintf( '%1s %2s',
73 '<code>' . $located . '</code>', esc_html__( 'does not exist.', 'woo-bopo-bundle' ) ), '2.1' );
74
75 return;
76 }
77
78 // Allow 3rd party plugin filter template file from their plugin.
79 $located = $located;
80
81
82 include( $located );
83
84 }
85 }
86
87 if ( ! function_exists( 'bopobb_register_product_type' ) ):
88 function bopobb_register_product_type() {
89 class VI_WOO_BOPO_Type extends WC_Product {
90 protected $items = null;
91
92 public function __construct( $product = 0 ) {
93 $this->supports[] = 'ajax_add_to_cart';
94
95 parent::__construct( $product );
96
97 $this->build_data();
98 }
99
100 public function get_type() {
101 return 'bopobb';
102 }
103
104 public function is_manage_stock() {
105
106 return $this->get_meta( 'bopobb_manage_stock', true ) === 'on';
107 }
108
109 public function is_fixed_price() {
110 $product_id = $this->id;
111 $fixed_price = get_post_meta( $product_id, '_regular_price', true );
112 if ( ! empty( $fixed_price ) ) {
113 return true;
114 } else {
115 return false;
116 }
117 }
118
119 public function get_discount_amount() {
120 $discount_amount = array();
121 $product_id = $this->id;
122
123 // discount amount
124 if ( ! $this->is_fixed_price() ) {
125 $discount_amount = $this->get_data( 'discount' );
126 } else {
127 $_price_meta = get_post_meta( $product_id, '_regular_price', true );
128 $_sale_meta = get_post_meta( $product_id, '_sale_price', true );
129 $_price = empty( $_price_meta ) ? 0 : floatval( $_price_meta );
130 $_salePrice = empty( $_sale_meta ) ? $_price : floatval( $_sale_meta );
131 $discount_amount = ( ( $_price - $_salePrice ) * 100 ) / $_price;
132 }
133
134 return $discount_amount;
135 }
136
137 public function get_discount() {
138 $discount_percentage = 0;
139
140 // discount percentage
141 if ( ! $this->is_fixed_price() && ! $this->get_discount_amount() && ( $discount_percentage = $this->get_meta( 'bopobb_discount', true ) ) && is_numeric( $discount_percentage ) && ( (float) $discount_percentage < 100 ) && ( (float) $discount_percentage > 0 ) ) {
142 $discount_percentage = (float) $discount_percentage;
143 }
144
145 return $discount_percentage;
146 }
147
148 public function get_ids() {
149 $product_id = $this->id;
150 $items_data = $this->items['items'];
151 $ids = '';
152 $isDefault = true;
153 foreach ( $items_data as $item ) {
154 if ( $item['bopobb_bpi_set_default'] != 1 || ! isset( $item['bopobb_bpi_default_product'] ) ) {
155 $isDefault = false;
156 }
157 }
158 if ( $isDefault ) {
159 foreach ( $items_data as $item ) {
160 $id_array = explode( '/', $item['bopobb_bpi_default_product'] );
161 $variation_string = '';
162 if ( count( $id_array ) > 1 ) {
163 $variation_string = '/' . $id_array[1];
164 }
165 if ( $ids == '' ) {
166 $ids .= $id_array[0] . '/' . $item['bopobb_bpi_quantity'] . $variation_string;
167 } else {
168 $ids .= ',' . $item['bopobb_bpi_default_product'] . '/' . $item['bopobb_bpi_quantity'] . $variation_string;
169 }
170 }
171 }
172
173 return $ids;
174 }
175
176 public function build_items( $ids = null ) {
177 $items = array();
178
179 if ( ! $ids ) {
180 $ids = $this->get_ids();
181 }
182
183 if ( $ids ) {
184 $ids_arr = explode( ',', $ids );
185
186 if ( is_array( $ids_arr ) && count( $ids_arr ) > 0 ) {
187 foreach ( $ids_arr as $ids_item ) {
188 $data = explode( '/', $ids_item );
189
190 if ( $pid = absint( isset( $data[0] ) ? $data[0] : 0 ) ) {
191 $item = [];
192 $item['id'] = $pid;
193 $item['qty'] = (float) ( isset( $data[1] ) ? $data[1] : 1 );
194 if ( isset( $data[2] ) ) {
195 $item['variations'] = $data[2];
196 }
197 $items[] = $item;
198 }
199 }
200 }
201 }
202
203 return $items;
204 }
205
206 public function build_data( $items = null ) {
207 if ( ! $items ) {
208 $items = $this->get_data();
209 }
210
211 $this->items = $items;
212 }
213
214 public function get_data( $target = '' ) {
215 $product_id = $this->id;
216 $bundle_data = array();
217 $items_data = array();
218 $items_discount = array();
219 $item_discount = array();
220 $item_count = $this->get_meta( 'bopobb_count', true );
221 $item_f_price = get_post_meta( $product_id, '_regular_price', true ) ? get_post_meta( $product_id, '_regular_price', true ) : 0;
222 $item_s_price = get_post_meta( $product_id, '_sale_price', true ) ? get_post_meta( $product_id, '_sale_price', true ) : 0;
223 $item_title = $this->get_meta( 'bopobb_title', true );
224 for ( $i = 0; $i < $item_count; $i ++ ) {
225 $meta_item = $this->get_meta( 'bopobb_item_' . $i, true );
226 if ( $meta_item ) {
227 if ( $target == 'discount' ) {
228 if ( isset( $meta_item['bopobb_bpi_discount'] ) ) {
229 $item_discount['by'] = $meta_item['bopobb_bpi_discount'];
230 }
231 if ( isset( $meta_item['bopobb_bpi_discount_number'] ) ) {
232 $item_discount['number'] = $meta_item['bopobb_bpi_discount_number'];
233 } else {
234 $item_discount['number'] = 0;
235 }
236 array_push( $items_discount, $item_discount );
237 }
238 array_push( $items_data, $meta_item );
239 }
240 }
241 if ( $target == 'discount' ) {
242 return $items_discount;
243 }
244 $bundle_data['count'] = $item_count;
245 $bundle_data['fixed'] = $item_f_price;
246 $bundle_data['sale'] = $item_s_price;
247 $bundle_data['title'] = $item_title;
248 $bundle_data['items'] = $items_data;
249
250 return $bundle_data;
251 }
252
253 public function get_items() {
254 return $this->items;
255 }
256 }
257 }
258 endif;