PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.16
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.16
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / Abstracts / Store.php

Store.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.16, at includes/Abstracts/Store.php

642 lines 19.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Abstract Store class.
4 *
5 * @package WooCommerce\POS\Abstracts
6 */
7
8 namespace WCPOS\WooCommercePOS\Abstracts;
9
10 use WCPOS\WooCommercePOS\Interfaces\StoreInterface;
11 use WCPOS\WooCommercePOS\Services\Store_Defaults;
12 use WC_Countries;
13 use function wc_format_country_state_string;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Abstract Store class.
21 *
22 * Handles the store data, and provides CRUD methods.
23 * Implements StoreInterface to ensure API consistency with Pro version.
24 */
25 class Store extends \WC_Data implements StoreInterface {
26 /**
27 * This is the name of this object type.
28 *
29 * @var string
30 */
31 protected $object_type = 'store';
32
33 /**
34 * Stores product data.
35 *
36 * @var array
37 */
38 protected $data = array(
39 'name' => '',
40 'locale' => '',
41 'timezone' => '',
42 'store_address' => '',
43 'store_address_2' => '',
44 'store_city' => '',
45 'store_postcode' => '',
46 'store_state' => '',
47 'store_country' => '',
48 'default_customer_address' => '',
49 'calc_taxes' => '',
50 'enable_coupons' => '',
51 'calc_discounts_sequentially' => '',
52 'currency' => '',
53 'currency_pos' => '',
54 'price_thousand_sep' => '',
55 'price_decimal_sep' => '',
56 'price_num_decimals' => '',
57 'prices_include_tax' => '',
58 'tax_based_on' => '',
59 'tax_address' => array(
60 'country' => '',
61 'state' => '',
62 'postcode' => '',
63 'city' => '',
64 ),
65 'shipping_tax_class' => '',
66 'tax_round_at_subtotal' => '',
67 'tax_display_shop' => '',
68 'tax_display_cart' => '',
69 'price_display_suffix' => '',
70 'tax_total_display' => '',
71 'default_customer' => 0,
72 'default_customer_is_cashier' => false,
73 // Pro properties (with WooCommerce defaults in free version).
74 'url' => '',
75 'phone' => '',
76 'email' => '',
77 'opening_hours' => array(),
78 'opening_hours_notes' => '',
79 'personal_notes' => '',
80 'policies_and_conditions' => '',
81 'footer_imprint' => '',
82 'tax_ids' => array(),
83 );
84
85 /**
86 * Construct the default POS store.
87 */
88 public function __construct() {
89 parent::__construct();
90 $this->set_wordpress_settings();
91 $this->set_woocommerce_general_settings();
92 $this->set_woocommerce_tax_settings();
93 $this->set_woocommerce_pos_settings();
94 $this->set_pro_property_defaults();
95 }
96
97 /**
98 * Set WordPress settings.
99 */
100 public function set_wordpress_settings() {
101 $this->set_prop( 'locale', \get_locale() );
102 $this->set_prop( 'timezone', \wp_timezone_string() );
103 }
104
105 /**
106 * Set WooCommerce general settings.
107 */
108 public function set_woocommerce_general_settings() {
109 $default_country = \WC_Admin_Settings::get_option( 'woocommerce_default_country' );
110 $country_state_array = wc_format_country_state_string( $default_country );
111
112 $this->set_prop( 'store_address', \WC_Admin_Settings::get_option( 'woocommerce_store_address' ) );
113 $this->set_prop( 'store_address_2', \WC_Admin_Settings::get_option( 'woocommerce_store_address_2' ) );
114 $this->set_prop( 'store_city', \WC_Admin_Settings::get_option( 'woocommerce_store_city' ) );
115 $this->set_prop( 'store_state', $country_state_array['state'] );
116 $this->set_prop( 'store_country', $country_state_array['country'] );
117 // $this->set_prop( 'default_country', $default_country );
118 $this->set_prop( 'store_postcode', \WC_Admin_Settings::get_option( 'woocommerce_store_postcode' ) );
119 $this->set_prop( 'default_customer_address', \WC_Admin_Settings::get_option( 'woocommerce_default_customer_address' ) );
120 $this->set_prop( 'calc_taxes', \WC_Admin_Settings::get_option( 'woocommerce_calc_taxes' ) );
121 $this->set_prop( 'enable_coupons', \WC_Admin_Settings::get_option( 'woocommerce_enable_coupons' ) );
122 $this->set_prop( 'calc_discounts_sequentially', \WC_Admin_Settings::get_option( 'woocommerce_calc_discounts_sequentially' ) );
123 $this->set_prop( 'currency', \WC_Admin_Settings::get_option( 'woocommerce_currency' ) );
124 $this->set_prop( 'currency_pos', \WC_Admin_Settings::get_option( 'woocommerce_currency_pos' ) );
125 $this->set_prop( 'price_thousand_sep', \WC_Admin_Settings::get_option( 'woocommerce_price_thousand_sep' ) );
126 $this->set_prop( 'price_decimal_sep', \WC_Admin_Settings::get_option( 'woocommerce_price_decimal_sep' ) );
127 $this->set_prop( 'price_num_decimals', (int) \WC_Admin_Settings::get_option( 'woocommerce_price_num_decimals', 2 ) );
128 }
129
130 /**
131 * Set WooCommerce tax settings.
132 */
133 public function set_woocommerce_tax_settings() {
134 $this->set_prop( 'prices_include_tax', \WC_Admin_Settings::get_option( 'woocommerce_prices_include_tax' ) );
135 $this->set_prop( 'tax_based_on', 'base' ); // default should be base, perhaps have a setting for this?
136 $this->set_prop( 'shipping_tax_class', \WC_Admin_Settings::get_option( 'woocommerce_shipping_tax_class' ) );
137 $this->set_prop( 'tax_round_at_subtotal', \WC_Admin_Settings::get_option( 'woocommerce_tax_round_at_subtotal' ) );
138 $this->set_prop( 'tax_display_shop', \WC_Admin_Settings::get_option( 'woocommerce_tax_display_shop' ) );
139 $this->set_prop( 'tax_display_cart', \WC_Admin_Settings::get_option( 'woocommerce_tax_display_cart' ) );
140 $this->set_prop( 'price_display_suffix', \WC_Admin_Settings::get_option( 'woocommerce_price_display_suffix' ) );
141 $this->set_prop( 'tax_total_display', \WC_Admin_Settings::get_option( 'woocommerce_tax_total_display' ) );
142
143 // tax address is same as WooCommerce address by default.
144 $country_state_array = wc_format_country_state_string( \WC_Admin_Settings::get_option( 'woocommerce_default_country' ) );
145
146 $this->set_prop(
147 'tax_address',
148 array(
149 'country' => $country_state_array['country'],
150 'state' => $country_state_array['state'],
151 'postcode' => \WC_Admin_Settings::get_option( 'woocommerce_store_postcode' ),
152 'city' => \WC_Admin_Settings::get_option( 'woocommerce_store_city' ),
153 )
154 );
155 }
156
157 /**
158 * Set WooCommerce POS settings.
159 */
160 public function set_woocommerce_pos_settings() {
161 $this->set_prop( 'default_customer', woocommerce_pos_get_settings( 'general', 'default_customer' ) );
162 $this->set_prop( 'default_customer_is_cashier', woocommerce_pos_get_settings( 'general', 'default_customer_is_cashier' ) );
163 }
164
165 /**
166 * Get Store name.
167 *
168 * @param string $context What the value is for. Valid values are view and edit.
169 * @return string
170 */
171 public function get_name( $context = 'view' ) {
172 return $this->get_prop( 'name', $context );
173 }
174
175 /**
176 * Get Store locale.
177 *
178 * @param string $context What the value is for. Valid values are view and edit.
179 * @return string
180 */
181 public function get_locale( $context = 'view' ) {
182 return $this->get_prop( 'locale', $context );
183 }
184
185
186 /**
187 * Get Store timezone.
188 *
189 * Empty string means inherit the WordPress site timezone.
190 *
191 * @param string $context What the value is for. Valid values are view and edit.
192 * @return string
193 */
194 public function get_timezone( $context = 'view' ) {
195 $value = $this->get_prop( 'timezone', $context );
196 return is_string( $value ) ? $value : '';
197 }
198
199 /**
200 * Get Store address.
201 *
202 * @param string $context What the value is for. Valid values are view and edit.
203 * @return string
204 */
205 public function get_store_address( $context = 'view' ) {
206 return $this->get_prop( 'store_address', $context );
207 }
208
209 /**
210 * Get Store address 2.
211 *
212 * @param string $context What the value is for. Valid values are view and edit.
213 * @return string
214 */
215 public function get_store_address_2( $context = 'view' ) {
216 return $this->get_prop( 'store_address_2', $context );
217 }
218
219 /**
220 * Get Store city.
221 *
222 * @param string $context What the value is for. Valid values are view and edit.
223 * @return string
224 */
225 public function get_store_city( $context = 'view' ) {
226 return $this->get_prop( 'store_city', $context );
227 }
228
229 /**
230 * Get Store postcode.
231 *
232 * @param string $context What the value is for. Valid values are view and edit.
233 * @return string
234 */
235 public function get_store_postcode( $context = 'view' ) {
236 return $this->get_prop( 'store_postcode', $context );
237 }
238
239 /**
240 * Get Store country, eg: US:AL.
241 * This is of the form COUNTRYCODE:STATECODE to be consistent with the WooCommerce settings.
242 *
243 * @param string $context What the value is for. Valid values are view and edit.
244 * @return string
245 */
246 // phpcs:disable Squiz.Commenting.InlineComment.InvalidEndChar -- Commented-out code.
247 // public function get_default_country( $context = 'view' ) {
248 // return $this->get_prop( 'default_country', $context );
249 // }
250 // phpcs:enable Squiz.Commenting.InlineComment.InvalidEndChar
251
252 /**
253 * Get Store state code.
254 *
255 * @param string $context What the value is for. Valid values are view and edit.
256 * @return string
257 */
258 public function get_store_state( $context = 'view' ) {
259 return $this->get_prop( 'store_state', $context );
260 }
261
262 /**
263 * Get Store country code.
264 *
265 * @param string $context What the value is for. Valid values are view and edit.
266 * @return string
267 */
268 public function get_store_country( $context = 'view' ) {
269 return $this->get_prop( 'store_country', $context );
270 }
271
272 /**
273 * Get Store customer address.
274 *
275 * @param string $context What the value is for. Valid values are view and edit.
276 * @return string
277 */
278 public function get_default_customer_address( $context = 'view' ) {
279 return $this->get_prop( 'default_customer_address', $context );
280 }
281
282 /**
283 * Get Store calculate taxes.
284 *
285 * @param string $context What the value is for. Valid values are view and edit.
286 * @return string
287 */
288 public function get_calc_taxes( $context = 'view' ) {
289 return $this->get_prop( 'calc_taxes', $context );
290 }
291
292 /**
293 * Get Store enable coupons.
294 *
295 * @param string $context What the value is for. Valid values are view and edit.
296 * @return string
297 */
298 public function get_enable_coupons( $context = 'view' ) {
299 return $this->get_prop( 'enable_coupons', $context );
300 }
301
302 /**
303 * Get Store calculate discounts sequentially.
304 *
305 * @param string $context What the value is for. Valid values are view and edit.
306 * @return string
307 */
308 public function get_calc_discounts_sequentially( $context = 'view' ) {
309 return $this->get_prop( 'calc_discounts_sequentially', $context );
310 }
311
312 /**
313 * Get Store currency.
314 *
315 * @param string $context What the value is for. Valid values are view and edit.
316 * @return string
317 */
318 public function get_currency( $context = 'view' ) {
319 return $this->get_prop( 'currency', $context );
320 }
321
322 /**
323 * Get Store currency position.
324 *
325 * @param string $context What the value is for. Valid values are view and edit.
326 * @return string
327 */
328 public function get_currency_position( $context = 'view' ) {
329 return $this->get_prop( 'currency_pos', $context );
330 }
331
332 /**
333 * Get Store price thousand separator.
334 *
335 * @param string $context What the value is for. Valid values are view and edit.
336 * @return string
337 */
338 public function get_price_thousand_separator( $context = 'view' ) {
339 return $this->get_prop( 'price_thousand_sep', $context );
340 }
341
342 /**
343 * Get Store price decimal separator.
344 *
345 * @param string $context What the value is for. Valid values are view and edit.
346 * @return string
347 */
348 public function get_price_decimal_separator( $context = 'view' ) {
349 return $this->get_prop( 'price_decimal_sep', $context );
350 }
351
352 /**
353 * Get Store price number of decimals.
354 *
355 * @param string $context What the value is for. Valid values are view and edit.
356 * @return int
357 */
358 public function get_price_number_of_decimals( $context = 'view' ) {
359 return $this->get_prop( 'price_num_decimals', $context );
360 }
361
362 /**
363 * Get Store prices include tax.
364 *
365 * @param string $context What the value is for. Valid values are view and edit.
366 * @return string
367 */
368 public function get_prices_include_tax( $context = 'view' ) {
369 return $this->get_prop( 'prices_include_tax', $context );
370 }
371
372 /**
373 * Get Store tax based on.
374 *
375 * @param string $context What the value is for. Valid values are view and edit.
376 * @return string
377 */
378 public function get_tax_based_on( $context = 'view' ) {
379 return $this->get_prop( 'tax_based_on', $context );
380 }
381
382 /**
383 * Get Store tax address.
384 *
385 * @param string $context What the value is for. Valid values are view and edit.
386 * @return array
387 */
388 public function get_tax_address( $context = 'view' ) {
389 return $this->get_prop( 'tax_address', $context );
390 }
391
392 /**
393 * Get Store shipping tax class.
394 *
395 * @param string $context What the value is for. Valid values are view and edit.
396 * @return string
397 */
398 public function get_shipping_tax_class( $context = 'view' ) {
399 return $this->get_prop( 'shipping_tax_class', $context );
400 }
401
402 /**
403 * Get Store tax round at subtotal.
404 *
405 * @param string $context What the value is for. Valid values are view and edit.
406 * @return string
407 */
408 public function get_tax_round_at_subtotal( $context = 'view' ) {
409 return $this->get_prop( 'tax_round_at_subtotal', $context );
410 }
411
412 /**
413 * Get Store tax display shop.
414 *
415 * @param string $context What the value is for. Valid values are view and edit.
416 * @return string
417 */
418 public function get_tax_display_shop( $context = 'view' ) {
419 return $this->get_prop( 'tax_display_shop', $context );
420 }
421
422 /**
423 * Get Store tax display cart.
424 *
425 * @param string $context What the value is for. Valid values are view and edit.
426 * @return string
427 */
428 public function get_tax_display_cart( $context = 'view' ) {
429 return $this->get_prop( 'tax_display_cart', $context );
430 }
431
432 /**
433 * Get Store price display suffix.
434 *
435 * @param string $context What the value is for. Valid values are view and edit.
436 * @return string
437 */
438 public function get_price_display_suffix( $context = 'view' ) {
439 return $this->get_prop( 'price_display_suffix', $context );
440 }
441
442 /**
443 * Get Store tax total display.
444 *
445 * @param string $context What the value is for. Valid values are view and edit.
446 * @return string
447 */
448 public function get_tax_total_display( $context = 'view' ) {
449 return $this->get_prop( 'tax_total_display', $context );
450 }
451
452
453 /**
454 * Get Store default customer.
455 *
456 * @param string $context What the value is for. Valid values are view and edit.
457 * @return int
458 */
459 public function get_default_customer( $context = 'view' ) {
460 return $this->get_prop( 'default_customer', $context );
461 }
462
463 /**
464 * Get Store default customer is cashier.
465 *
466 * @param string $context What the value is for. Valid values are view and edit.
467 * @return bool
468 */
469 public function get_default_customer_is_cashier( $context = 'view' ) {
470 return $this->get_prop( 'default_customer_is_cashier', $context );
471 }
472
473 /**
474 * Set Pro property defaults from WooCommerce settings.
475 *
476 * These properties are fully customizable in the Pro version,
477 * but in the free version we derive sensible defaults via
478 * Store_Defaults: WCPOS general settings → WC POS core options
479 * → WordPress / WooCommerce fallbacks.
480 */
481 protected function set_pro_property_defaults() {
482 $this->set_prop( 'name', Store_Defaults::name() );
483 $this->set_prop( 'url', \home_url() );
484 $this->set_prop( 'phone', Store_Defaults::phone() );
485 $this->set_prop( 'email', Store_Defaults::email() );
486 $this->set_prop( 'opening_hours', array() );
487 $this->set_prop( 'opening_hours_notes', '' );
488 $this->set_prop( 'personal_notes', '' );
489 $this->set_prop( 'policies_and_conditions', Store_Defaults::policies_and_conditions() );
490 $this->set_prop( 'footer_imprint', '' );
491 $this->set_prop( 'tax_ids', Store_Defaults::tax_ids() );
492 }
493
494 /**
495 * Get Store URL.
496 *
497 * @param string $context What the value is for. Valid values are view and edit.
498 * @return string
499 */
500 public function get_url( $context = 'view' ) {
501 return $this->get_prop( 'url', $context );
502 }
503
504 /**
505 * Get Store phone number.
506 *
507 * @param string $context What the value is for. Valid values are view and edit.
508 * @return string
509 */
510 public function get_phone( $context = 'view' ) {
511 return $this->get_prop( 'phone', $context );
512 }
513
514 /**
515 * Get Store email address.
516 *
517 * @param string $context What the value is for. Valid values are view and edit.
518 * @return string
519 */
520 public function get_email( $context = 'view' ) {
521 return $this->get_prop( 'email', $context );
522 }
523
524 /**
525 * Get Store opening hours.
526 *
527 * @param string $context What the value is for. Valid values are view and edit.
528 * @return array|string
529 */
530 public function get_opening_hours( $context = 'view' ) {
531 return $this->get_prop( 'opening_hours', $context );
532 }
533
534 /**
535 * Get Store opening hours notes.
536 *
537 * @param string $context What the value is for. Valid values are view and edit.
538 * @return string
539 */
540 public function get_opening_hours_notes( $context = 'view' ) {
541 return $this->get_prop( 'opening_hours_notes', $context );
542 }
543
544 /**
545 * Get Store personal notes.
546 *
547 * @param string $context What the value is for. Valid values are view and edit.
548 * @return string
549 */
550 public function get_personal_notes( $context = 'view' ) {
551 return $this->get_prop( 'personal_notes', $context );
552 }
553
554 /**
555 * Get Store policies and conditions.
556 *
557 * @param string $context What the value is for. Valid values are view and edit.
558 * @return string
559 */
560 public function get_policies_and_conditions( $context = 'view' ) {
561 return $this->get_prop( 'policies_and_conditions', $context );
562 }
563
564 /**
565 * Get Store footer imprint.
566 *
567 * @param string $context What the value is for. Valid values are view and edit.
568 * @return string
569 */
570 public function get_footer_imprint( $context = 'view' ) {
571 return $this->get_prop( 'footer_imprint', $context );
572 }
573
574 /**
575 * Get Store formatted address.
576 *
577 * @return string
578 */
579 public function get_formatted_address() {
580 $countries = new WC_Countries();
581
582 return $countries->get_formatted_address(
583 array(
584 'address_1' => $this->get_store_address(),
585 'address_2' => $this->get_store_address_2(),
586 'city' => $this->get_store_city(),
587 'state' => $this->get_store_state(),
588 'postcode' => $this->get_store_postcode(),
589 'country' => $this->get_store_country(),
590 )
591 );
592 }
593
594 /**
595 * Get Store tax IDs as a structured array.
596 *
597 * @param string $context What the value is for. Valid values are view and edit.
598 * @return array<int,array<string,string>>
599 */
600 public function get_tax_ids( $context = 'view' ) {
601 $value = $this->get_prop( 'tax_ids', $context );
602 return is_array( $value ) ? $value : array();
603 }
604
605 /**
606 * Get the primary tax ID as a formatted scalar.
607 *
608 * Back-compat helper for templates using {{store.tax_id}}.
609 * Returns the first entry's value (verbatim — country prefix is preserved
610 * exactly as stored), or empty string when no entries exist.
611 *
612 * @param string $context What the value is for. Valid values are view and edit.
613 * @return string
614 */
615 public function get_tax_id( $context = 'view' ) {
616 $tax_ids = $this->get_tax_ids( $context );
617 if ( empty( $tax_ids ) ) {
618 return '';
619 }
620 $primary = reset( $tax_ids );
621 return isset( $primary['value'] ) ? (string) $primary['value'] : '';
622 }
623
624 /**
625 * Get Store logo image source.
626 *
627 * In the free version, this returns the site's custom logo if set.
628 *
629 * @param string $size Image size. Default 'full'.
630 * @return array|false Array of image data, or false if no image.
631 */
632 public function get_logo_image_src( $size = 'full' ) {
633 $custom_logo_id = \get_theme_mod( 'custom_logo' );
634
635 if ( $custom_logo_id ) {
636 return \wp_get_attachment_image_src( $custom_logo_id, $size );
637 }
638
639 return false;
640 }
641 }
642