PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.18
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.18
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.2.18, at app/Utility/Settings.php

175 lines 4.0 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 from meta according to settings.
28 *
29 * @param int $product_id Product id.
30 * @return float Product Price.
31 */
32 public static function get_price_from_meta( $product_id ): float {
33 $price = self::get( 'product_price_type' );
34
35 if ( 'price' === $price ) {
36 $meta_value = get_post_meta( $product_id, '_price', true );
37
38 return is_numeric( $meta_value ) ? (float) $meta_value : 0.0; //phpcs:ignore
39 }
40
41 if ( 'regular_price' === $price ) {
42 $meta_value = get_post_meta( $product_id, '_regular_price', true );
43
44 return is_numeric( $meta_value ) ? (float) $meta_value : 0.0; //phpcs:ignore
45 }
46
47 if ( 'sale_price' === $price ) {
48 $meta_value = get_post_meta( $product_id, '_sale_price', true );
49
50 return is_numeric( $meta_value ) ? (float) $meta_value : 0.0; //phpcs:ignore
51 }
52
53 // @phpstan-ignore-next-line
54 return (float) get_post_meta( $product_id, '_price', true );
55 }
56
57 /**
58 * Get the product price according to settings.
59 *
60 * @param \WC_Product $product Product Object.
61 * @return float Product Price.
62 */
63 public static function get_price( \WC_Product $product ): float {
64 $price = self::get( 'product_price_type' );
65
66 if ( 'price' === $price ) {
67 return (float) $product->get_price();
68 }
69
70 if ( 'regular_price' === $price ) {
71 return (float) $product->get_regular_price();
72 }
73
74 if ( 'sale_price' === $price ) {
75 return (float) $product->get_sale_price();
76 }
77
78 return (float) $product->get_price();
79 }
80
81 /**
82 * Get settings.
83 * If the key is not found, return all settings.
84 * If the key is 'all', return all settings.
85 * If the key is 'default', return default settings.
86 * If the key is found, return the value.
87 * If the key is not found, return false.
88 *
89 * @param string $key Settings Key.
90 * @return array|string|\WP_Error Settings.
91 */
92 public static function get( $key = 'all' ) {
93 $default = array(
94 'product_price_type' => 'price', // price, regular_price, sale_price.
95 'min_max_discount_amount' => 'min', // min, max.
96 );
97
98 /**
99 * Add defaults without changing the core values.
100 *
101 * @param array $defaults
102 * @since 3.3.11
103 */
104 $default = wp_parse_args( apply_filters( 'disco_settings', $default ), $default );
105
106 if ( 'default' === $key ) {
107 return $default;
108 }
109
110 $get_settings = get_option( 'disco_settings', array() );
111
112 $settings = wp_parse_args( $get_settings, $default );
113
114 if ( 'all' === $key || empty( $key ) ) {
115 return $settings;
116 }
117
118 if ( array_key_exists( $key, $settings ) ) {
119 return $settings[ $key ];
120 }
121
122 return new \WP_Error(
123 'rest_not_found',
124 __( 'Sorry, Invalid Settings Key.', 'disco' ),
125 array( 'status' => 400 )
126 );
127 }
128
129 /**
130 * Set settings.
131 *
132 * @param string $key Settings Key.
133 * @param string $value Settings Value.
134 * @return bool|\WP_Error
135 * @throws \Exception Invalid Settings Key.
136 * @since 1.0.0
137 */
138 public static function set( $key, $value ) {
139 $setting = self::get( 'all' );
140
141 if ( is_wp_error( $setting ) ) {
142 return $setting;
143 }
144
145 if ( is_array( $setting ) && isset( $setting[ $key ] ) ) {
146 $setting[ $key ] = $value;
147
148 return self::save( $setting );
149 }
150
151 return new \WP_Error(
152 'rest_not_found',
153 __( 'Sorry, Invalid Settings Key.', 'disco' ),
154 array( 'status' => 400 )
155 );
156 }
157
158 /**
159 * Save settings.
160 *
161 * @param array $args Settings.
162 * @return bool
163 * @throws \Exception Invalid Settings Key.
164 * @since 1.0.0
165 */
166 public static function save( $args ) {
167 $settings = get_option( 'disco_settings', self::Get() );
168
169 $new_settings = wp_parse_args( $args, $settings );
170
171 return update_option( 'disco_settings', $new_settings );
172 }
173
174 }
175