PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.2.8
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.2.8
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / modules / spending-goal / class-spending-goal.php

class-spending-goal.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.2.8, at inc/modules/spending-goal/class-spending-goal.php

293 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Spending Goal
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Spending Goal class.
15 */
16 class Merchant_Spending_Goal extends Merchant_Add_Module {
17
18 /**
19 * Module ID.
20 *
21 */
22 const MODULE_ID = 'spending-goal';
23
24 /**
25 * Module template path.
26 *
27 */
28 const MODULE_TEMPLATES_PATH = 'modules/' . self::MODULE_ID;
29
30 /**
31 * Is module preview.
32 *
33 */
34 public static $is_module_preview = false;
35
36 /**
37 * Constructor.
38 *
39 */
40 public function __construct() {
41 // Module id.
42 $this->module_id = self::MODULE_ID;
43
44 // WooCommerce only.
45 $this->wc_only = true;
46
47 // Parent construct.
48 parent::__construct();
49
50 // Module section.
51 $this->module_section = 'boost-revenue';
52
53 // Module default settings.
54 $this->module_default_settings = array(
55 'spending_goal' => 150,
56 'total_type' => 'subtotal',
57 'discount_type' => 'percent',
58 'discount_amount' => 10,
59 'discount_name' => esc_html__( 'Spending goal', 'merchant' ),
60 'inclusion' => false,
61 'exclusion' => false,
62 'user_condition' => 'all',
63 'text_goal_zero' => esc_html__( 'Spend {spending_goal} to get a {discount_amount} discount!', 'merchant' ),
64 'text_goal_started' => esc_html__( 'Spend {spending_goal} more to get a {discount_amount} discount!', 'merchant' ),
65 'text_goal_reached' => esc_html__( 'Congratulations! You got a discount of {discount_amount} on this order!', 'merchant' ),
66 );
67
68 // Module data.
69 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
70
71 // Module options path.
72 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
73
74 // Is module preview page.
75 if ( is_admin() && parent::is_module_settings_page() ) {
76 self::$is_module_preview = true;
77
78 // Enqueue admin styles.
79 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
80
81 // Enqueue admin scripts.
82 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
83
84 // Admin preview box.
85 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
86
87 // Custom CSS.
88 // The custom CSS should be added here as well due to ensure preview box works properly.
89 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
90 }
91
92 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
93 // Init translations.
94 $this->init_translations();
95 }
96 }
97
98 /**
99 * Init translations.
100 *
101 * @return void
102 */
103 public function init_translations() {
104 $settings = $this->get_module_settings();
105 if ( ! empty( $settings['discount_name'] ) ) {
106 Merchant_Translator::register_string( $settings['discount_name'], esc_html__( 'Spending Discount Goal: Discount name', 'merchant' ) );
107 }
108 if ( ! empty( $settings['text_goal_zero'] ) ) {
109 Merchant_Translator::register_string( $settings['text_goal_zero'], esc_html__( 'Spending Discount Goal: When the goal target is at 0%', 'merchant' ) );
110 }
111 if ( ! empty( $settings['text_goal_started'] ) ) {
112 Merchant_Translator::register_string( $settings['text_goal_started'], esc_html__( 'Spending Discount Goal: When the goal target is between 1-99%', 'merchant' ) );
113 }
114 if ( ! empty( $settings['text_goal_reached'] ) ) {
115 Merchant_Translator::register_string( $settings['text_goal_reached'], esc_html__( 'Spending Discount Goal: When the goal target is at 100%', 'merchant' ) );
116 }
117 }
118
119 /**
120 * Admin enqueue CSS.
121 *
122 * @return void
123 */
124 public function admin_enqueue_css() {
125 if ( parent::is_module_settings_page() ) {
126 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/spending-goal.min.css', array(), MERCHANT_VERSION );
127 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
128 }
129 }
130
131 /**
132 * Admin Enqueue scripts.
133 *
134 * @return void
135 */
136 public function admin_enqueue_scripts() {
137 // Register and enqueue the main module script.
138 wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array(), MERCHANT_VERSION, true );
139 wp_localize_script( 'merchant-admin-' . self::MODULE_ID, 'merchantSpendingGoal', array(
140 'currencySymbol' => function_exists( 'get_woocommerce_currency_symbol' ) ? get_woocommerce_currency_symbol() : '$',
141 ) );
142 }
143
144 /**
145 * Render admin preview
146 *
147 * @param Merchant_Admin_Preview $preview
148 * @param string $module
149 *
150 * @return Merchant_Admin_Preview
151 */
152 public function render_admin_preview( $preview, $module ) {
153 if ( $module === self::MODULE_ID ) {
154 ob_start();
155 self::admin_preview_content();
156 $content = ob_get_clean();
157
158 // Get the template with dummy preview data.
159 $preview->set_html( $content );
160
161 $preview->set_css( 'gradient_start', '.merchant-spending-goal-widget-label', '--merchant-gradient-start' );
162 $preview->set_css( 'gradient_end', '.merchant-spending-goal-widget-label', '--merchant-gradient-end' );
163 $preview->set_css( 'progress_bar', '.merchant-spending-goal-widget-progress-bar-filled', '--merchant-progress-bar' );
164 $preview->set_css( 'content_bg_color', '.merchant-spending-goal-widget', '--merchant-content-bg-color' );
165 $preview->set_css( 'content_text_color', '.merchant-spending-goal-widget-text', '--merchant-content-text-color' );
166 $preview->set_css( 'content_width', '.merchant-spending-goal-widget-content', '--merchant-content-width', 'px' );
167 $preview->set_css( 'content_width', '.merchant-spending-goal-widget', '--merchant-content-width', 'px' );
168 $preview->set_text( 'text_goal_zero', '.merchant-spending-goal-widget-text', array(
169 array(
170 '{spending_goal}',
171 '{discount_amount}',
172 ),
173 array(
174 array(
175 'setting' => 'spending_goal',
176 'format' => $preview->get_price_format(),
177 ),
178 array(
179 'setting' => 'discount_type',
180 'conditions' => array(
181 'percent' => array(
182 'setting' => 'discount_amount',
183 'format' => '<strong>{string}%</strong>',
184 ),
185 'fixed' => array(
186 'setting' => 'discount_amount',
187 'format' => '<strong>' . $preview->get_price_format() . '</strong>',
188 ),
189 ),
190 ),
191 ),
192 ) );
193 $preview->trigger_update( 'spending_goal' );
194 $preview->trigger_update( 'discount_type' );
195 $preview->trigger_update( 'discount_amount' );
196 }
197
198 return $preview;
199 }
200
201 /**
202 * Admin preview content.
203 *
204 * @return void
205 */
206 public function admin_preview_content() {
207 $settings = $this->get_module_settings();
208 // Discount amount formatted with currency symbol or percentage based on discount type
209 $discount_amount_formatted = $settings['discount_type'] === 'percent'
210 ? $settings['discount_amount'] . '%'
211 : wc_price( $settings['discount_amount'] );
212 ?>
213
214 <div class="mrc-preview-single-product-elements">
215 <div class="mrc-preview-left-column">
216 <div class="mrc-preview-product-image-wrapper">
217 <div class="mrc-preview-product-image"></div>
218 <div class="mrc-preview-product-image-thumbs">
219 <div class="mrc-preview-product-image-thumb"></div>
220 <div class="mrc-preview-product-image-thumb"></div>
221 <div class="mrc-preview-product-image-thumb"></div>
222 </div>
223 </div>
224 </div>
225 <div class="mrc-preview-right-column">
226 <div class="mrc-preview-text-placeholder"></div>
227 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
228 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
229 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
230 <div class="mrc-preview-addtocart-placeholder"></div>
231 </div>
232 </div>
233
234 <?php merchant_get_template_part(
235 self::MODULE_TEMPLATES_PATH,
236 'widget',
237 array(
238 'spending' => 50,
239 'content' => str_replace(
240 array(
241 '{spending_goal}',
242 '{discount_amount}',
243 ),
244 array(
245 wc_price( $settings['spending_goal'] ),
246 '<strong>' . $discount_amount_formatted . '</strong>',
247 ),
248 sanitize_text_field( $settings['text_goal_started'] )
249 ),
250 )
251 ); ?>
252
253 <?php
254 }
255
256 /**
257 * Custom CSS.
258 *
259 * @return string
260 */
261 public function get_module_custom_css() {
262 $css = '';
263
264 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'gradient_start', '#5e5e5e', '.merchant-spending-goal-widget-label', '--merchant-gradient-start' );
265 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'gradient_end', '#212121', '.merchant-spending-goal-widget-label', '--merchant-gradient-end' );
266 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'progress_bar', '#d83a3b', '.merchant-spending-goal-widget-progress-bar-filled', '--merchant-progress-bar' );
267 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_bg_color', '#f9f9f9', '.merchant-spending-goal-widget', '--merchant-content-bg-color' );
268 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_text_color', 'inherit', '.merchant-spending-goal-widget-text', '--merchant-content-text-color' );
269 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_width', 300, '.merchant-spending-goal-widget-content', '--merchant-content-width', 'px' );
270 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_width', 300, '.merchant-spending-goal-widget', '--merchant-content-width', 'px' );
271
272 return $css;
273 }
274
275 /**
276 * Admin custom CSS.
277 *
278 * @param string $css The custom CSS.
279 *
280 * @return string $css The custom CSS.
281 */
282 public function admin_custom_css( $css ) {
283 $css .= $this->get_module_custom_css();
284
285 return $css;
286 }
287 }
288
289 // Initialize the module.
290 add_action( 'init', function () {
291 Merchant_Modules::create_module( new Merchant_Spending_Goal() );
292 } );
293