PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.0
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.0
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.3.0, at inc/modules/spending-goal/class-spending-goal.php

313 lines 11.1 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 * Initialize the module's option group definitions.
38 *
39 * @return array<int, array<string, mixed>> Array of option group arrays.
40 */
41 protected function init_option_groups() {
42 return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
43 }
44
45 /**
46 * Constructor.
47 *
48 */
49 public function __construct() {
50 // Module id.
51 $this->module_id = self::MODULE_ID;
52
53 // WooCommerce only.
54 $this->wc_only = true;
55
56 // Parent construct.
57 parent::__construct();
58
59 // Module section.
60 $this->module_section = 'boost-revenue';
61
62 // Module default settings.
63 $this->module_default_settings = array(
64 'spending_goal' => 150,
65 'total_type' => 'subtotal',
66 'discount_type' => 'percent',
67 'discount_amount' => 10,
68 'discount_name' => esc_html__( 'Spending goal', 'merchant' ),
69 'inclusion' => false,
70 'exclusion' => false,
71 'user_condition' => 'all',
72 'text_goal_zero' => esc_html__( 'Spend {spending_goal} to get a {discount_amount} discount!', 'merchant' ),
73 'text_goal_started' => esc_html__( 'Spend {spending_goal} more to get a {discount_amount} discount!', 'merchant' ),
74 'text_goal_reached' => esc_html__( 'Congratulations! You got a discount of {discount_amount} on this order!', 'merchant' ),
75 );
76
77 // Module data.
78 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
79
80 // AI prompt examples.
81 $this->ai_examples = array(
82 'blurb' => esc_html__( 'See what AI can do for your spending goal, from setting its target and discount to tailoring the progress message and who qualifies.', 'merchant' ),
83 'prompts' => array(
84 esc_html__( "Explore the abilities WPVibe gives you access to, then set the spending goal to $150 based on the cart subtotal, giving shoppers a 10% discount called 'Loyalty Bonus' when they reach it", 'merchant' ),
85 esc_html__( 'Check what abilities you have available through WPVibe, then switch the Loyalty Bonus spending goal reward to a fixed $20 off based on the cart total instead of a percentage', 'merchant' ),
86 esc_html__( "Look at your available WPVibe abilities, then change the Loyalty Bonus in-progress message to 'Only {spending_goal} more to unlock your bonus!'", 'merchant' ),
87 esc_html__( 'See what abilities WPVibe exposes, then limit the Loyalty Bonus spending goal to customers with the Wholesale role', 'merchant' ),
88 ),
89 );
90
91 // Module options path.
92 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
93
94 // Is module preview page.
95 if ( is_admin() && parent::is_module_settings_page() ) {
96 self::$is_module_preview = true;
97
98 // Enqueue admin styles.
99 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
100
101 // Enqueue admin scripts.
102 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
103
104 // Admin preview box.
105 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
106
107 // Custom CSS.
108 // The custom CSS should be added here as well due to ensure preview box works properly.
109 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
110 }
111
112 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
113 // Init translations.
114 $this->init_translations();
115 }
116 }
117
118 /**
119 * Init translations.
120 *
121 * @return void
122 */
123 public function init_translations() {
124 $settings = $this->get_module_settings();
125 if ( ! empty( $settings['discount_name'] ) ) {
126 Merchant_Translator::register_string( $settings['discount_name'], esc_html__( 'Spending Discount Goal: Discount name', 'merchant' ) );
127 }
128 if ( ! empty( $settings['text_goal_zero'] ) ) {
129 Merchant_Translator::register_string( $settings['text_goal_zero'], esc_html__( 'Spending Discount Goal: When the goal target is at 0%', 'merchant' ) );
130 }
131 if ( ! empty( $settings['text_goal_started'] ) ) {
132 Merchant_Translator::register_string( $settings['text_goal_started'], esc_html__( 'Spending Discount Goal: When the goal target is between 1-99%', 'merchant' ) );
133 }
134 if ( ! empty( $settings['text_goal_reached'] ) ) {
135 Merchant_Translator::register_string( $settings['text_goal_reached'], esc_html__( 'Spending Discount Goal: When the goal target is at 100%', 'merchant' ) );
136 }
137 }
138
139 /**
140 * Admin enqueue CSS.
141 *
142 * @return void
143 */
144 public function admin_enqueue_css() {
145 if ( parent::is_module_settings_page() ) {
146 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/spending-goal.min.css', array(), MERCHANT_VERSION );
147 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
148 }
149 }
150
151 /**
152 * Admin Enqueue scripts.
153 *
154 * @return void
155 */
156 public function admin_enqueue_scripts() {
157 // Register and enqueue the main module script.
158 wp_enqueue_script( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', array(), MERCHANT_VERSION, true );
159 wp_localize_script( 'merchant-admin-' . self::MODULE_ID, 'merchantSpendingGoal', array(
160 'currencySymbol' => function_exists( 'get_woocommerce_currency_symbol' ) ? get_woocommerce_currency_symbol() : '$',
161 ) );
162 }
163
164 /**
165 * Render admin preview
166 *
167 * @param Merchant_Admin_Preview $preview
168 * @param string $module
169 *
170 * @return Merchant_Admin_Preview
171 */
172 public function render_admin_preview( $preview, $module ) {
173 if ( $module === self::MODULE_ID ) {
174 ob_start();
175 self::admin_preview_content();
176 $content = ob_get_clean();
177
178 // Get the template with dummy preview data.
179 $preview->set_html( $content );
180
181 $preview->set_css( 'gradient_start', '.merchant-spending-goal-widget-label', '--merchant-gradient-start' );
182 $preview->set_css( 'gradient_end', '.merchant-spending-goal-widget-label', '--merchant-gradient-end' );
183 $preview->set_css( 'progress_bar', '.merchant-spending-goal-widget-progress-bar-filled', '--merchant-progress-bar' );
184 $preview->set_css( 'content_bg_color', '.merchant-spending-goal-widget', '--merchant-content-bg-color' );
185 $preview->set_css( 'content_text_color', '.merchant-spending-goal-widget-text', '--merchant-content-text-color' );
186 $preview->set_css( 'content_width', '.merchant-spending-goal-widget-content', '--merchant-content-width', 'px' );
187 $preview->set_css( 'content_width', '.merchant-spending-goal-widget', '--merchant-content-width', 'px' );
188 $preview->set_text( 'text_goal_zero', '.merchant-spending-goal-widget-text', array(
189 array(
190 '{spending_goal}',
191 '{discount_amount}',
192 ),
193 array(
194 array(
195 'setting' => 'spending_goal',
196 'format' => $preview->get_price_format(),
197 ),
198 array(
199 'setting' => 'discount_type',
200 'conditions' => array(
201 'percent' => array(
202 'setting' => 'discount_amount',
203 'format' => '<strong>{string}%</strong>',
204 ),
205 'fixed' => array(
206 'setting' => 'discount_amount',
207 'format' => '<strong>' . $preview->get_price_format() . '</strong>',
208 ),
209 ),
210 ),
211 ),
212 ) );
213 $preview->trigger_update( 'spending_goal' );
214 $preview->trigger_update( 'discount_type' );
215 $preview->trigger_update( 'discount_amount' );
216 }
217
218 return $preview;
219 }
220
221 /**
222 * Admin preview content.
223 *
224 * @return void
225 */
226 public function admin_preview_content() {
227 $settings = $this->get_module_settings();
228 // Discount amount formatted with currency symbol or percentage based on discount type
229 $discount_amount_formatted = $settings['discount_type'] === 'percent'
230 ? $settings['discount_amount'] . '%'
231 : wc_price( $settings['discount_amount'] );
232 ?>
233
234 <div class="mrc-preview-single-product-elements">
235 <div class="mrc-preview-left-column">
236 <div class="mrc-preview-product-image-wrapper">
237 <div class="mrc-preview-product-image"></div>
238 <div class="mrc-preview-product-image-thumbs">
239 <div class="mrc-preview-product-image-thumb"></div>
240 <div class="mrc-preview-product-image-thumb"></div>
241 <div class="mrc-preview-product-image-thumb"></div>
242 </div>
243 </div>
244 </div>
245 <div class="mrc-preview-right-column">
246 <div class="mrc-preview-text-placeholder"></div>
247 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
248 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
249 <div class="mrc-preview-text-placeholder mrc-mw-40"></div>
250 <div class="mrc-preview-addtocart-placeholder"></div>
251 </div>
252 </div>
253
254 <?php merchant_get_template_part(
255 self::MODULE_TEMPLATES_PATH,
256 'widget',
257 array(
258 'spending' => 50,
259 'content' => str_replace(
260 array(
261 '{spending_goal}',
262 '{discount_amount}',
263 ),
264 array(
265 wc_price( $settings['spending_goal'] ),
266 '<strong>' . $discount_amount_formatted . '</strong>',
267 ),
268 sanitize_text_field( $settings['text_goal_started'] )
269 ),
270 )
271 ); ?>
272
273 <?php
274 }
275
276 /**
277 * Custom CSS.
278 *
279 * @return string
280 */
281 public function get_module_custom_css() {
282 $css = '';
283
284 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'gradient_start', '#5e5e5e', '.merchant-spending-goal-widget-label', '--merchant-gradient-start' );
285 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'gradient_end', '#212121', '.merchant-spending-goal-widget-label', '--merchant-gradient-end' );
286 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'progress_bar', '#d83a3b', '.merchant-spending-goal-widget-progress-bar-filled', '--merchant-progress-bar' );
287 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_bg_color', '#f9f9f9', '.merchant-spending-goal-widget', '--merchant-content-bg-color' );
288 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_text_color', 'inherit', '.merchant-spending-goal-widget-text', '--merchant-content-text-color' );
289 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_width', 300, '.merchant-spending-goal-widget-content', '--merchant-content-width', 'px' );
290 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'content_width', 300, '.merchant-spending-goal-widget', '--merchant-content-width', 'px' );
291
292 return $css;
293 }
294
295 /**
296 * Admin custom CSS.
297 *
298 * @param string $css The custom CSS.
299 *
300 * @return string $css The custom CSS.
301 */
302 public function admin_custom_css( $css ) {
303 $css .= $this->get_module_custom_css();
304
305 return $css;
306 }
307 }
308
309 // Initialize the module.
310 add_action( 'init', function () {
311 Merchant_Modules::create_module( new Merchant_Spending_Goal() );
312 } );
313