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

183 lines 4.3 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 'discount_priority_type' => 'both', // both, woocommerce_coupon, disco_campaign
97 'on_sale_badge' => 'show_all', // show_all, show_sale.
98 'show_strike_through' => array(
99 'shop_page' => true,
100 'product_page' => true,
101 'category_page' => true,
102 'cart_page' => true,
103 ),
104 );
105
106 /**
107 * Add defaults without changing the core values.
108 *
109 * @param array $defaults
110 * @since 3.3.11
111 */
112 $default = wp_parse_args( apply_filters( 'disco_settings', $default ), $default );
113
114 if ( 'default' === $key ) {
115 return $default;
116 }
117
118 $get_settings = get_option( 'disco_settings', array() );
119
120 $settings = wp_parse_args( $get_settings, $default );
121
122 if ( 'all' === $key || empty( $key ) ) {
123 return $settings;
124 }
125
126 if ( array_key_exists( $key, $settings ) ) {
127 return $settings[ $key ];
128 }
129
130 return new \WP_Error(
131 'rest_not_found',
132 __( 'Sorry, Invalid Settings Key.', 'disco' ),
133 array( 'status' => 400 )
134 );
135 }
136
137 /**
138 * Set settings.
139 *
140 * @param string $key Settings Key.
141 * @param string $value Settings Value.
142 * @return bool|\WP_Error
143 * @throws \Exception Invalid Settings Key.
144 * @since 1.0.0
145 */
146 public static function set( $key, $value ) {
147 $setting = self::get( 'all' );
148
149 if ( is_wp_error( $setting ) ) {
150 return $setting;
151 }
152
153 if ( is_array( $setting ) && isset( $setting[ $key ] ) ) {
154 $setting[ $key ] = $value;
155
156 return self::save( $setting );
157 }
158
159 return new \WP_Error(
160 'rest_not_found',
161 __( 'Sorry, Invalid Settings Key.', 'disco' ),
162 array( 'status' => 400 )
163 );
164 }
165
166 /**
167 * Save settings.
168 *
169 * @param array $args Settings.
170 * @return bool
171 * @throws \Exception Invalid Settings Key.
172 * @since 1.0.0
173 */
174 public static function save( $args ) {
175 $settings = get_option( 'disco_settings', self::Get() );
176
177 $new_settings = wp_parse_args( $args, $settings );
178
179 return update_option( 'disco_settings', $new_settings );
180 }
181
182 }
183