PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.1
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.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 / stock-scarcity / class-stock-scarcity.php

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

240 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Stock Scarcity
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Stock Scarcity Class.
15 *
16 */
17 class Merchant_Stock_Scarcity extends Merchant_Add_Module {
18
19 /**
20 * Module ID.
21 */
22 const MODULE_ID = 'stock-scarcity';
23
24 /**
25 * Module path.
26 */
27 const MODULE_DIR = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID;
28
29
30 /**
31 * Module template path.
32 */
33 const MODULE_TEMPLATES_PATH = 'modules/' . self::MODULE_ID;
34
35 /**
36 * Is module preview.
37 *
38 */
39 public static $is_module_preview = false;
40
41 /**
42 * Initialize the module's option group definitions.
43 *
44 * @return array<int, array<string, mixed>> Array of option group arrays.
45 */
46 protected function init_option_groups() {
47 return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
48 }
49
50 /**
51 * Constructor.
52 *
53 */
54 public function __construct() {
55 // Module id.
56 $this->module_id = self::MODULE_ID;
57
58 // WooCommerce only.
59 $this->wc_only = true;
60
61 // Parent construct.
62 parent::__construct();
63
64 // Module section.
65 $this->module_section = 'convert-more';
66
67 // Module default settings.
68 $this->module_default_settings = array(
69 'min_inventory' => 50,
70 'display-pages' => array( 'product' ),
71 'low_inventory_text' => esc_html__( 'Hurry! Only {stock} units left in stock!', 'merchant' ),
72 );
73
74 // Module data.
75 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
76
77 // AI prompt examples.
78 $this->ai_examples = array(
79 'blurb' => esc_html__( 'See what AI can do for your low-stock urgency box, from setting the stock threshold that triggers it to rewording its message and restyling how it looks.', 'merchant' ),
80 'prompts' => array(
81 esc_html__( 'Explore the abilities WPVibe gives you access to, then show the low-stock urgency box only once a product drops below 5 units in stock', 'merchant' ),
82 esc_html__( "Check what abilities you have available through WPVibe, then reword the low-stock message to 'Almost gone, only {stock} left in stock'", 'merchant' ),
83 esc_html__( 'Look at your available WPVibe abilities, then show the urgency box on shop and category archive pages too, not just the single product page', 'merchant' ),
84 esc_html__( 'See what abilities WPVibe exposes, then restyle the low-stock urgency box\'s progress bar with a green-to-red gradient and bump the message font size up to 18px', 'merchant' ),
85 ),
86 );
87
88 // Module options path.
89 $this->module_options_path = self::MODULE_DIR . '/admin/options.php';
90
91 // Is module preview page.
92 if ( is_admin() && parent::is_module_settings_page() ) {
93 self::$is_module_preview = true;
94
95 // Enqueue admin styles.
96 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
97
98 // Admin preview box.
99 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
100
101 // Admin custom CSS.
102 // The custom CSS should be added here as well due to ensure preview box works properly.
103 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
104 }
105
106 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
107 // Init translations.
108 $this->init_translations();
109 }
110 }
111
112 /**
113 * Init translations.
114 *
115 * @return void
116 */
117 public function init_translations() {
118 $settings = $this->get_module_settings();
119 if ( ! empty( $settings['low_inventory_text'] ) ) {
120 Merchant_Translator::register_string( $settings['low_inventory_text'], esc_html__( 'Stock Scarcity: Text when inventory is low (when only 1 item left in stock)', 'merchant' ) );
121 }
122 if ( ! empty( $settings['low_inventory_text_plural'] ) ) {
123 Merchant_Translator::register_string( $settings['low_inventory_text_plural'], esc_html__( 'Stock Scarcity: Text when inventory is low (when more than 1 item is left in stock)', 'merchant' ) );
124 }
125 if ( ! empty( $settings['low_inventory_text_simple'] ) ) {
126 Merchant_Translator::register_string( $settings['low_inventory_text_simple'], esc_html__( 'Stock Scarcity: Text when inventory is low (variable - used for product variation)', 'merchant' ) );
127 }
128 }
129
130 /**
131 * Admin enqueue CSS.
132 *
133 * @return void
134 */
135 public function admin_enqueue_css() {
136 if ( parent::is_module_settings_page() ) {
137 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/stock-scarcity.min.css', array(), MERCHANT_VERSION );
138 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
139 }
140 }
141
142 /**
143 * Render admin preview
144 *
145 * @param Merchant_Admin_Preview $preview
146 * @param string $module
147 *
148 * @return Merchant_Admin_Preview
149 */
150 public function render_admin_preview( $preview, $module ) {
151 if ( $module === self::MODULE_ID ) {
152 ob_start();
153 self::admin_preview_content();
154 $content = ob_get_clean();
155
156 $preview->set_html( $content );
157
158 $preview->set_text( 'low_inventory_text', '.merchant-stock-scarcity-message', array(
159 array(
160 '{stock}',
161 ),
162 array(
163 '20',
164 ),
165 ) );
166 $preview->set_css( 'text_font_size', '.merchant-stock-scarcity-message', '--merchant-font-size', 'px' );
167 $preview->set_css( 'text_font_weight', '.merchant-stock-scarcity-message', '--merchant-font-weight' );
168 $preview->set_css( 'text_text_color', '.merchant-stock-scarcity-message', '--merchant-text-color' );
169 $preview->set_css( 'gradient_start', '.merchant-stock-scarcity-progress-bar ', '--merchant-gradient-start' );
170 $preview->set_css( 'gradient_end', '.merchant-stock-scarcity-progress-bar ', '--merchant-gradient-end' );
171 $preview->set_css( 'progress_bar_bg', '.merchant-stock-scarcity-content', '--merchant-bg-color' );
172 }
173
174 return $preview;
175 }
176
177 /**
178 * Admin preview content.
179 *
180 * @return void
181 */
182 public function admin_preview_content() {
183 $settings = $this->get_module_settings();
184 ?>
185
186 <div class="mrc-preview-single-product-elements">
187 <div class="mrc-preview-content">
188 <?php
189 merchant_get_template_part(
190 self::MODULE_TEMPLATES_PATH,
191 'single-product',
192 array(
193 'settings' => $settings,
194 'percentage' => 30,
195 'stock' => 20,
196 )
197 ); ?>
198 </div>
199 </div>
200
201 <?php
202 }
203
204 /**
205 * Custom CSS.
206 *
207 * @return string
208 */
209 public function get_module_custom_css() {
210 $css = '';
211
212 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'gradient_start', '#ffc108', '.merchant-stock-scarcity-progress-bar ', '--merchant-gradient-start' );
213 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'gradient_end', '#d61313', '.merchant-stock-scarcity-progress-bar ', '--merchant-gradient-end' );
214 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'progress_bar_bg', '#e1e1e1', '.merchant-stock-scarcity-content', '--merchant-bg-color' );
215 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'text_font_size', 16, '.merchant-stock-scarcity-message', '--merchant-font-size', 'px' );
216 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'text_font_weight', 'normal', '.merchant-stock-scarcity-message', '--merchant-font-weight' );
217 $css .= Merchant_Custom_CSS::get_variable_css( self::MODULE_ID, 'text_text_color', '#212121', '.merchant-stock-scarcity-message', '--merchant-text-color' );
218
219 return $css;
220 }
221
222 /**
223 * Admin custom CSS.
224 *
225 * @param string $css The custom CSS.
226 *
227 * @return string $css The custom CSS.
228 */
229 public function admin_custom_css( $css ) {
230 $css .= $this->get_module_custom_css();
231
232 return $css;
233 }
234 }
235
236 // Initialize the module.
237 add_action( 'init', function () {
238 Merchant_Modules::create_module( new Merchant_Stock_Scarcity() );
239 } );
240