PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.1.9
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.1.9
1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 All 178 releases
disco / app / Utility / Settings.php

Settings.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.1.9, at app/Utility/Settings.php

144 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Search Utility
5 *
6 * @package Disco
7 * @subpackage App\Utility
8 * @since 1.0.0
9 * @category Utility
10 */
11
12 namespace Disco\App\Utility;
13
14 /**
15 * Class Settings
16 *
17 * @package Disco
18 * @subpackage App\Utility
19 * @author Ohidul Islam <wahid0003@gmail.com>
20 * @link https://webappick.com
21 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
22 * @category Utility
23 */
24 class Settings {
25
26 /**
27 * Get the product price according to settings.
28 *
29 * @param \WC_Product $product Product Object.
30 * @return float
31 */
32 public static function get_price( $product ) {
33 $price = self::get( 'product_price_type' );
34
35 if ( 'price' === $price ) {
36 return (float) $product->get_price();
37 }
38
39 if ( 'regular_price' === $price ) {
40 return (float) $product->get_regular_price();
41 }
42
43 if ( 'sale_price' === $price ) {
44 return (float) $product->get_sale_price();
45 }
46
47 return (float) $product->get_price();
48 }
49
50 /**
51 * Get settings.
52 * If the key is not found, return all settings.
53 * If the key is 'all', return all settings.
54 * If the key is 'default', return default settings.
55 * If the key is found, return the value.
56 * If the key is not found, return false.
57 *
58 * @param string $key Settings Key.
59 * @return array|string|\WP_Error Settings.
60 */
61 public static function get( $key = 'all' ) {
62 $default = array(
63 'product_price_type' => 'price', // price, regular_price, sale_price.
64 'min_max_discount_amount' => 'min', // min, max.
65 );
66
67 /**
68 * Add defaults without changing the core values.
69 *
70 * @param array $defaults
71 * @since 3.3.11
72 */
73 $default = wp_parse_args( apply_filters( 'disco_settings', $default ), $default );
74
75 if ( 'default' === $key ) {
76 return $default;
77 }
78
79 $get_settings = get_option( 'disco_settings', array() );
80
81 $settings = wp_parse_args( $get_settings, $default );
82
83 if ( 'all' === $key || empty( $key ) ) {
84 return $settings;
85 }
86
87 if ( array_key_exists( $key, $settings ) ) {
88 return $settings[ $key ];
89 }
90
91 return new \WP_Error(
92 'rest_not_found',
93 __( 'Sorry, Invalid Settings Key.', 'disco' ),
94 array( 'status' => 400 )
95 );
96 }
97
98 /**
99 * Set settings.
100 *
101 * @param string $key Settings Key.
102 * @param string $value Settings Value.
103 * @return bool|\WP_Error
104 * @throws \Exception Invalid Settings Key.
105 * @since 1.0.0
106 */
107 public static function set( $key, $value ) {
108 $setting = self::get( 'all' );
109
110 if ( is_wp_error( $setting ) ) {
111 return $setting;
112 }
113
114 if ( is_array( $setting ) && isset( $setting[ $key ] ) ) {
115 $setting[ $key ] = $value;
116
117 return self::save( $setting );
118 }
119
120 return new \WP_Error(
121 'rest_not_found',
122 __( 'Sorry, Invalid Settings Key.', 'disco' ),
123 array( 'status' => 400 )
124 );
125 }
126
127 /**
128 * Save settings.
129 *
130 * @param array $args Settings.
131 * @return bool
132 * @throws \Exception Invalid Settings Key.
133 * @since 1.0.0
134 */
135 public static function save( $args ) {
136 $settings = get_option( 'disco_settings', self::Get() );
137
138 $new_settings = wp_parse_args( $args, $settings );
139
140 return update_option( 'disco_settings', $new_settings );
141 }
142
143 }
144