PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.11.1
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.11.1
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 / pre-orders / class-pre-orders.php

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

454 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Pre Orders.
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Pre Orders Class.
15 *
16 */
17 class Merchant_Pre_Orders extends Merchant_Add_Module {
18
19 /**
20 * Module ID.
21 *
22 */
23 const MODULE_ID = 'pre-orders';
24
25 /**
26 * Is module preview.
27 *
28 */
29 public static $is_module_preview = false;
30
31 /**
32 * Main functionality dependency.
33 *
34 */
35 public $main_func;
36
37 /**
38 * Constructor.
39 *
40 */
41 public function __construct( Merchant_Pre_Orders_Main_Functionality $main_func ) {
42 // Module id.
43 $this->module_id = self::MODULE_ID;
44
45 // WooCommerce only.
46 $this->wc_only = true;
47
48 // Parent construct.
49 parent::__construct();
50
51 $this->main_func = $main_func;
52
53 // Module section.
54 $this->module_section = 'boost-revenue';
55
56 // Module default settings.
57 $this->module_default_settings = array(
58 'button_text' => __( 'Pre Order Now!', 'merchant' ),
59 'additional_text' => __( 'Ships on {date}.', 'merchant' ),
60 );
61
62 // Mount preview url.
63 $preview_url = site_url( '/' );
64
65 if ( function_exists( 'wc_get_page_id' ) ) {
66 $preview_url = get_permalink( wc_get_page_id( 'shop' ) );
67 }
68
69 // Module data.
70 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
71 $this->module_data['preview_url'] = $preview_url;
72
73 // Module options path.
74 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
75
76 // Is module preview page.
77 if ( is_admin() && parent::is_module_settings_page() ) {
78 self::$is_module_preview = true;
79
80 // Enqueue admin styles.
81 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
82 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_js' ) );
83
84 // Localize Script.
85 add_filter( 'merchant_admin_localize_script', array( $this, 'localize_script' ) );
86
87 // Admin preview box.
88 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
89
90 // Custom CSS.
91 // The custom CSS should be added here as well due to ensure preview box works properly.
92 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
93 }
94
95 add_action( 'merchant_admin_before_include_modules_options', array( $this, 'help_banner' ) );
96
97 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
98 // Init translations.
99 $this->init_translations();
100 }
101
102 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
103 return;
104 }
105
106 // TODO: Refactor the 'Merchant_Pre_Orders_Main_Functionality' class to load admin things separated from frontend things.
107 $main_func->init();
108
109 // Return early if it's on admin but not in the respective module settings page.
110 if ( is_admin() && ! wp_doing_ajax() && ! parent::is_module_settings_page() ) {
111 return;
112 }
113
114 // Enqueue styles.
115 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
116
117 // Enqueue scripts.
118 add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) );
119
120 // Localize script.
121 add_filter( 'merchant_localize_script', array( $this, 'localize_script' ) );
122
123 // Custom CSS.
124 add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
125 }
126
127 /**
128 * Init translations.
129 *
130 * @return void
131 */
132 public function init_translations() {
133 $settings = $this->get_module_settings();
134 if ( ! empty( $settings['rules'] ) ) {
135 foreach ( $settings['rules'] as $rule ) {
136 if ( ! empty( $rule['button_text'] ) ) {
137 Merchant_Translator::register_string( $rule['button_text'], 'Pre order button text' );
138 }
139 if ( ! empty( $rule['additional_text'] ) ) {
140 Merchant_Translator::register_string( $rule['additional_text'], 'Pre order additional information' );
141 }
142 if ( ! empty( $rule['cart_label_text'] ) ) {
143 Merchant_Translator::register_string( $rule['cart_label_text'], 'Label text on cart' );
144 }
145 }
146 }
147 }
148
149 /**
150 * Admin enqueue CSS.
151 *
152 * @return void
153 */
154 public function admin_enqueue_css() {
155 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
156 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
157
158 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
159 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION );
160 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
161 }
162 }
163
164 /**
165 * Admin JS
166 *
167 * @return void
168 */
169 public function admin_enqueue_js() {
170 if ( parent::is_module_settings_page() ) {
171 wp_enqueue_script(
172 'merchant-admin-' . self::MODULE_ID,
173 MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js',
174 array( 'jquery' ),
175 MERCHANT_VERSION,
176 true
177 );
178 }
179 }
180
181 /**
182 * Enqueue CSS.
183 *
184 * @return void
185 */
186 public function enqueue_css() {
187 // Specific module styles.
188 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION );
189
190 // add inline style
191 if ( is_singular( 'product' ) ) {
192 wp_add_inline_style( 'merchant-' . self::MODULE_ID, $this->add_inline_style() );
193 }
194 }
195
196 public function add_inline_style() {
197 ob_start();
198 $rule = $this->current_rule();
199 if ( ! empty( $rule ) ) {
200 ?>
201 .woocommerce .merchant-pre-ordered-product{
202 --mrc-po-text-color: <?php
203 echo esc_attr( $rule['text-color'] ); ?>;
204 --mrc-po-text-hover-color: <?php
205 echo esc_attr( $rule['text-hover-color'] ); ?>;
206 --mrc-po-border-color: <?php
207 echo esc_attr( $rule['border-color'] ); ?>;
208 --mrc-po-border-hover-color: <?php
209 echo esc_attr( $rule['border-hover-color'] ); ?>;
210 --mrc-po-background-color: <?php
211 echo esc_attr( $rule['background-color'] ); ?>;
212 --mrc-po-background-hover-color: <?php
213 echo esc_attr( $rule['background-hover-color'] ); ?>;
214 }
215 <?php
216 }
217
218 return ob_get_clean();
219 }
220
221 /**
222 * Enqueue scripts.
223 *
224 * @return void
225 */
226 public function enqueue_scripts() {
227 // Register and enqueue the main module script.
228 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/pre-orders.min.js', array(), MERCHANT_VERSION, true );
229 }
230
231 /**
232 * Localize script with module settings.
233 *
234 * @param array $setting The merchant global object setting parameter.
235 *
236 * @return array $setting The merchant global object setting parameter.
237 */
238 public function localize_script( $setting ) {
239 //$module_settings = $this->get_module_settings();
240
241 $setting['pre_orders'] = true;
242 $rule = $this->current_rule();
243 if ( ! empty( $rule ) && $rule['button_text'] ) {
244 $setting['pre_orders_add_button_title'] = Merchant_Translator::translate( $rule['button_text'] );
245 } else {
246 $setting['pre_orders_add_button_title'] = esc_html__( 'Pre Order Now!', 'merchant' );
247 }
248
249 $setting['shipping_date_missing_text'] = esc_html__( 'Please set a shipping date first', 'merchant' );
250
251 return $setting;
252 }
253
254 /**
255 * Render admin preview
256 *
257 * @param Merchant_Admin_Preview $preview
258 * @param string $module
259 *
260 * @return Merchant_Admin_Preview
261 */
262 public function render_admin_preview( $preview, $module ) {
263 if ( self::MODULE_ID === $module ) {
264 $settings = $this->get_module_settings();
265
266 // Additional text.
267 $additional_text = $settings['additional_text'];
268 $time_format = date_i18n( get_option( 'date_format' ), strtotime( gmdate( 'Y-m-d', strtotime( '+2 days' ) ) ) );
269 $text = $this->main_func->replace_date_text( $additional_text, $time_format );
270
271 ob_start();
272 self::admin_preview_content( $settings, $text );
273 $content = ob_get_clean();
274
275 // HTML.
276 $preview->set_html( $content );
277
278 // Button Text.
279 $preview->set_text( 'button_text', '.add_to_cart_button' );
280
281 // Additional Text.
282 $preview->set_text( 'additional_text', '.merchant-pre-orders-date', array(
283 array(
284 '{date}',
285 ),
286 array(
287 $time_format,
288 ),
289 ) );
290 }
291
292 return $preview;
293 }
294
295 /**
296 * Admin preview content.
297 *
298 * @return void
299 */
300 public function admin_preview_content( $settings, $text ) {
301 ?>
302
303 <div class="mrc-preview-single-product-elements">
304 <div class="mrc-preview-left-column">
305 <div class="mrc-preview-product-image-wrapper">
306 <div class="mrc-preview-product-image"></div>
307 <div class="mrc-preview-product-image-thumbs">
308 <div class="mrc-preview-product-image-thumb"></div>
309 <div class="mrc-preview-product-image-thumb"></div>
310 <div class="mrc-preview-product-image-thumb"></div>
311 </div>
312 </div>
313 </div>
314 <div class="mrc-preview-right-column">
315 <div class="mrc-preview-text-placeholder"></div>
316 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
317 <div class="mrc-preview-text-placeholder mrc-mw-40 mrc-hide-on-smaller-screens"></div>
318 <div class="mrc-preview-text-placeholder mrc-mw-30 mrc-hide-on-smaller-screens"></div>
319 <div class="merchant-pre-ordered-product">
320 <div class="merchant-pre-orders-date"><?php
321 printf( '<div class="merchant-pre-orders-date">%s</div>', esc_html( $text ) ); ?></div>
322 <a href="#" class="add_to_cart_button"><?php
323 echo esc_html( $settings['button_text'] ); ?></a>
324 </div>
325 </div>
326 </div>
327
328 <?php
329 }
330
331 /**
332 * Custom CSS.
333 *
334 * @return string
335 */
336 public function get_module_custom_css() {
337 $css = '';
338
339 if ( is_admin() || is_singular( 'product' ) ) {
340 // Text Color.
341 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-color' );
342
343 // Text Color (hover).
344 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-hover-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-hover-color' );
345
346 // Border Color.
347 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-border-color' );
348
349 // Border Color (hover).
350 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-border-hover-color' );
351
352 // Background Color.
353 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-background-color' );
354
355 // Background Color (hover).
356 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-background-hover-color' );
357 }
358
359 return $css;
360 }
361
362 /**
363 * Admin custom CSS.
364 *
365 * @param string $css The custom CSS.
366 *
367 * @return string $css The custom CSS.
368 */
369 public function admin_custom_css( $css ) {
370 $css .= $this->get_module_custom_css();
371
372 return $css;
373 }
374
375 /**
376 * Frontend custom CSS.
377 *
378 * @param string $css The custom CSS.
379 *
380 * @return string $css The custom CSS.
381 */
382 public function frontend_custom_css( $css ) {
383 $css .= $this->get_module_custom_css();
384
385 return $css;
386 }
387
388 /**
389 * Get current product rule.
390 *
391 * @return array
392 */
393 private function current_rule() {
394 if ( is_singular( 'product' ) ) {
395 $product = wc_get_product( get_queried_object_id() );
396 $rule = Merchant_Pre_Orders_Main_Functionality::available_product_rule( $product->get_id() );
397 if ( empty( $rule ) && $product->is_type( 'variable' ) ) {
398 $available_variations = $product->get_available_variations();
399 foreach ( $available_variations as $variation ) {
400 $rule = Merchant_Pre_Orders_Main_Functionality::available_product_rule( $variation['variation_id'] );
401 if ( ! empty( $rule ) ) {
402 break;
403 }
404 }
405 }
406
407 return $rule;
408 }
409
410 return array();
411 }
412
413 /**
414 * Help banner.
415 *
416 * @return void
417 */
418 public function help_banner( $module_id ) {
419 if ( $module_id === 'pre-orders' ) {
420 ?>
421 <div class="merchant-module-page-setting-fields">
422 <div class="merchant-module-page-setting-field merchant-module-page-setting-field-content">
423 <div class="merchant-module-page-setting-field-inner">
424 <div class="merchant-tag-pre-orders">
425 <i class="dashicons dashicons-info"></i>
426 <p>
427 <?php
428 echo esc_html__(
429 'Pre-orders captured by Merchant are tagged with "MerchantPreOrder" and can be found in your WooCommerce Order Section.',
430 'merchant'
431 );
432 printf(
433 '<a href="%1s" target="_blank">%2s</a>',
434 esc_url( admin_url( 'edit.php?post_type=shop_order' ) ),
435 esc_html__( 'View Pre-Orders', 'merchant' )
436 );
437 ?></p>
438 </div>
439 </div>
440 </div>
441 </div>
442 <?php
443 }
444 }
445 }
446
447 // Main functionality.
448 require MERCHANT_DIR . 'inc/modules/pre-orders/class-pre-orders-main-functionality.php';
449
450 // Initialize the module.
451 add_action( 'init', function () {
452 new Merchant_Pre_Orders( new Merchant_Pre_Orders_Main_Functionality() );
453 } );
454