| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Volume discounts |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Bulk discounts module class. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Volume_Discounts extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
*/ |
| 22 |
const MODULE_ID = 'volume-discounts'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Module path. |
| 26 |
*/ |
| 27 |
const MODULE_DIR = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID; |
| 28 |
|
| 29 |
/** |
| 30 |
* Module template path. |
| 31 |
*/ |
| 32 |
const MODULE_TEMPLATES_PATH = 'modules/' . self::MODULE_ID; |
| 33 |
|
| 34 |
/** |
| 35 |
* Is module preview. |
| 36 |
* |
| 37 |
*/ |
| 38 |
public static $is_module_preview = false; |
| 39 |
|
| 40 |
/** |
| 41 |
* Set the module as having analytics. |
| 42 |
* |
| 43 |
* @var bool |
| 44 |
*/ |
| 45 |
protected $has_analytics = true; |
| 46 |
|
| 47 |
/** |
| 48 |
* Initialize the module's option group definitions. |
| 49 |
* |
| 50 |
* @return array<int, array<string, mixed>> Array of option group arrays. |
| 51 |
*/ |
| 52 |
protected function init_option_groups() { |
| 53 |
return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor. |
| 58 |
* |
| 59 |
*/ |
| 60 |
public function __construct() { |
| 61 |
// Module id. |
| 62 |
$this->module_id = self::MODULE_ID; |
| 63 |
|
| 64 |
// WooCommerce only. |
| 65 |
$this->wc_only = true; |
| 66 |
|
| 67 |
// Parent construct. |
| 68 |
parent::__construct(); |
| 69 |
|
| 70 |
// Module section. |
| 71 |
$this->module_section = 'boost-revenue'; |
| 72 |
|
| 73 |
// Module default settings. |
| 74 |
$this->module_default_settings = array( |
| 75 |
'title' => __( 'Discount', 'merchant' ), |
| 76 |
'single_product_placement' => 'before-cart-form', |
| 77 |
'table_title' => __( 'Buy more, save more!', 'merchant' ), |
| 78 |
'save_label' => esc_html__( 'Save {amount}', 'merchant' ), |
| 79 |
'buy_text' => esc_html__( 'Buy {quantity}, get {discount} off each', 'merchant' ), |
| 80 |
'item_text' => esc_html__( 'Per item:', 'merchant' ), |
| 81 |
'total_text' => esc_html__( 'Total price:', 'merchant' ), |
| 82 |
'cart_title_text' => esc_html__( 'Discount', 'merchant' ), |
| 83 |
'cart_description_text' => esc_html__( 'A discount of {amount} has been applied.', 'merchant' ), |
| 84 |
); |
| 85 |
|
| 86 |
// Module data. |
| 87 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 88 |
|
| 89 |
// AI prompt examples. |
| 90 |
$this->ai_examples = array( |
| 91 |
'blurb' => esc_html__( 'See what AI can do for your bulk discount offers, from launching a new quantity discount to fine-tuning its tiers or the copy shoppers see.', 'merchant' ), |
| 92 |
'prompts' => array( |
| 93 |
esc_html__( 'Explore the abilities WPVibe gives you access to, then create a bulk discount offer that takes 10% off the Coffee Beans category when a customer buys 5 or more', 'merchant' ), |
| 94 |
esc_html__( 'Check what abilities you have available through WPVibe, then set up a bulk discount that takes a flat $15 off the regular price once the cart holds 10 or more items', 'merchant' ), |
| 95 |
esc_html__( "Look at your available WPVibe abilities, then change the tier format text on the Coffee Beans bulk discount to 'Buy {quantity}, save {discount} off each'", 'merchant' ), |
| 96 |
esc_html__( 'See what abilities WPVibe exposes, then exclude any products that are already on sale from the Coffee Beans bulk discount', 'merchant' ), |
| 97 |
), |
| 98 |
); |
| 99 |
|
| 100 |
// Module options path. |
| 101 |
$this->module_options_path = self::MODULE_DIR . '/admin/options.php'; |
| 102 |
|
| 103 |
// Is module preview page. |
| 104 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 105 |
self::$is_module_preview = true; |
| 106 |
|
| 107 |
// Enqueue admin scripts |
| 108 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 109 |
|
| 110 |
// Add preview box |
| 111 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 112 |
} |
| 113 |
|
| 114 |
if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) { |
| 115 |
// Init translations. |
| 116 |
$this->init_translations(); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Init translations. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function init_translations() { |
| 126 |
$settings = $this->get_module_settings(); |
| 127 |
$strings = array( |
| 128 |
'table_title' => 'Bulk discount: title', |
| 129 |
'save_label' => 'Bulk discount: save label', |
| 130 |
'buy_text' => 'Bulk discount: buy text', |
| 131 |
'item_text' => 'Bulk discount: item text', |
| 132 |
'total_text' => 'Bulk discount: total text', |
| 133 |
'cart_title_text' => 'Bulk discount: cart item discount title', |
| 134 |
'cart_description_text' => 'Bulk discount: Cart item discount description', |
| 135 |
); |
| 136 |
if ( isset( $settings['offers'] ) && ! empty( $settings['offers'] ) ) { |
| 137 |
foreach ( $settings['offers'] as $offer ) { |
| 138 |
foreach ( $strings as $key => $string ) { |
| 139 |
if ( ! empty( $offer['product_single_page'][ $key ] ) ) { |
| 140 |
Merchant_Translator::register_string( $offer['product_single_page'][ $key ], $string . ' - product single page' ); |
| 141 |
} |
| 142 |
if ( ! empty( $rule['cart_page'][ $key ] ) ) { |
| 143 |
Merchant_Translator::register_string( $rule['cart_page'][ $key ], $string . ' - cart page' ); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Enqueue admin page content scripts. |
| 152 |
* |
| 153 |
* @return void |
| 154 |
*/ |
| 155 |
public function admin_enqueue_css() { |
| 156 |
if ( $this->is_module_settings_page() ) { |
| 157 |
wp_enqueue_style( "merchant-admin-{$this->module_id}", MERCHANT_URI . "assets/css/modules/volume-discounts/admin/preview.min.css", array(), MERCHANT_VERSION ); |
| 158 |
wp_enqueue_style( "merchant-{$this->module_id}", MERCHANT_URI . "assets/css/modules/{$this->module_id}/{$this->module_id}.min.css", array(), MERCHANT_VERSION ); |
| 159 |
wp_enqueue_script( "merchant-{$this->module_id}", MERCHANT_URI . "assets/js/modules/{$this->module_id}/admin/preview.min.js", array( 'jquery' ), MERCHANT_VERSION, |
| 160 |
true ); |
| 161 |
wp_localize_script( "merchant-{$this->module_id}", 'merchant_volume_discounts', array( |
| 162 |
'mock_item_price' => wc_format_sale_price( 20, 16 ), |
| 163 |
) ); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Render admin preview |
| 169 |
* |
| 170 |
* @param Merchant_Admin_Preview $preview |
| 171 |
* @param string $module |
| 172 |
* |
| 173 |
* @return Merchant_Admin_Preview |
| 174 |
*/ |
| 175 |
public function render_admin_preview( $preview, $module ) { |
| 176 |
if ( $module === self::MODULE_ID ) { |
| 177 |
ob_start(); |
| 178 |
self::admin_preview_content(); |
| 179 |
$content = ob_get_clean(); |
| 180 |
|
| 181 |
// HTML |
| 182 |
$preview->set_html( $content ); |
| 183 |
|
| 184 |
// Table Title |
| 185 |
$preview->set_text( 'table_title', '.merchant-volume-discounts-title' ); |
| 186 |
|
| 187 |
// Save Label |
| 188 |
$preview->set_text( 'save_label', '.merchant-volume-discounts-save-label', array( |
| 189 |
array( |
| 190 |
'{amount}', |
| 191 |
), |
| 192 |
array( |
| 193 |
wc_price( 10 ), |
| 194 |
), |
| 195 |
) ); |
| 196 |
|
| 197 |
// Buy Text |
| 198 |
$preview->set_text( 'buy_text', '.merchant-volume-discounts-buy-label', array( |
| 199 |
array( |
| 200 |
'{amount}', |
| 201 |
'{discount}', |
| 202 |
), |
| 203 |
array( |
| 204 |
'<strong>10</strong>', |
| 205 |
'<strong>' . wc_price( 2 ) . '</strong>', |
| 206 |
), |
| 207 |
) ); |
| 208 |
} |
| 209 |
|
| 210 |
return $preview; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Admin preview content. |
| 215 |
* |
| 216 |
* @return void |
| 217 |
*/ |
| 218 |
public function admin_preview_content() { |
| 219 |
$settings = $this->get_module_settings(); |
| 220 |
?> |
| 221 |
<div class="merchant-single-product-preview"> |
| 222 |
<div class="mrc-preview-single-product-elements"> |
| 223 |
<div class="mrc-preview-left-column"> |
| 224 |
<div class="mrc-preview-product-image-wrapper"> |
| 225 |
<div class="mrc-preview-product-image"></div> |
| 226 |
<div class="mrc-preview-product-image-thumbs"> |
| 227 |
<div class="mrc-preview-product-image-thumb"></div> |
| 228 |
<div class="mrc-preview-product-image-thumb"></div> |
| 229 |
<div class="mrc-preview-product-image-thumb"></div> |
| 230 |
</div> |
| 231 |
</div> |
| 232 |
</div> |
| 233 |
<div class="mrc-preview-right-column" data-currency="<?php |
| 234 |
echo esc_attr( get_woocommerce_currency_symbol() ); ?>"> |
| 235 |
<?php |
| 236 |
merchant_get_template_part( |
| 237 |
Merchant_Volume_Discounts::MODULE_TEMPLATES_PATH, |
| 238 |
'admin-preview', |
| 239 |
array( |
| 240 |
'settings' => $settings, |
| 241 |
'discount_tiers' => array( |
| 242 |
array( |
| 243 |
'quantity' => 10, |
| 244 |
'discount' => 5, |
| 245 |
'discount_type' => 'percentage_discount', |
| 246 |
'product_single_page' => array( |
| 247 |
'save_label' => esc_html__( 'Save {amount}', 'merchant' ), |
| 248 |
'item_text' => esc_html__( 'Per item:', 'merchant' ), |
| 249 |
'total_text' => esc_html__( 'Total price:', 'merchant' ), |
| 250 |
'buy_text' => esc_html__( 'Buy {quantity}, get {discount} off each', 'merchant' ), |
| 251 |
'table_title' => esc_html__( 'Buy more, save more!', 'merchant' ), |
| 252 |
), |
| 253 |
), |
| 254 |
), |
| 255 |
'product_price' => 20, |
| 256 |
) |
| 257 |
); |
| 258 |
|
| 259 |
merchant_get_template_part( |
| 260 |
Merchant_Volume_Discounts::MODULE_TEMPLATES_PATH, |
| 261 |
'admin-preview-tiered-radio', |
| 262 |
array( |
| 263 |
'settings' => $settings, |
| 264 |
'discount_tiers' => array( |
| 265 |
array( |
| 266 |
'quantity' => 10, |
| 267 |
'discount' => 5, |
| 268 |
'discount_type' => 'percentage_discount', |
| 269 |
'product_single_page' => array( |
| 270 |
'save_label' => esc_html__( 'Save {amount}', 'merchant' ), |
| 271 |
'item_text' => esc_html__( 'Per item:', 'merchant' ), |
| 272 |
'total_text' => esc_html__( 'Total price:', 'merchant' ), |
| 273 |
'buy_text' => esc_html__( 'Buy {quantity}, get {discount} off each', 'merchant' ), |
| 274 |
'table_title' => esc_html__( 'Buy more, save more!', 'merchant' ), |
| 275 |
), |
| 276 |
), |
| 277 |
array( |
| 278 |
'quantity' => 10, |
| 279 |
'discount' => 5, |
| 280 |
'discount_type' => 'percentage_discount', |
| 281 |
'product_single_page' => array( |
| 282 |
'save_label' => esc_html__( 'Save {amount}', 'merchant' ), |
| 283 |
'item_text' => esc_html__( 'Per item:', 'merchant' ), |
| 284 |
'total_text' => esc_html__( 'Total price:', 'merchant' ), |
| 285 |
'buy_text' => esc_html__( 'Buy {quantity}, get {discount} off each', 'merchant' ), |
| 286 |
'table_title' => null, |
| 287 |
), |
| 288 |
), |
| 289 |
), |
| 290 |
'product_price' => 20, |
| 291 |
) |
| 292 |
); |
| 293 |
?> |
| 294 |
</div> |
| 295 |
</div> |
| 296 |
</div> |
| 297 |
<?php |
| 298 |
$this->cart_item_preview(); |
| 299 |
$this->checkout_page_preview(); |
| 300 |
$this->thank_you_page_preview(); |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Checkout page preview. |
| 305 |
* |
| 306 |
* @return string |
| 307 |
*/ |
| 308 |
public function checkout_page_preview() { |
| 309 |
?> |
| 310 |
<div class="merchant-checkout-preview"> |
| 311 |
<div class="order-received"> |
| 312 |
<div class="page-title"><?php esc_html_e('Checkout','merchant'); ?></div> |
| 313 |
<br> |
| 314 |
<div class="upsell-offer"> |
| 315 |
<div class="offer-title"><?php esc_html_e('Last chance to get {offer_quantity} x','merchant'); ?></div> |
| 316 |
<div class="product-details"> |
| 317 |
<div class="product-image"></div> |
| 318 |
<div class="product-info"> |
| 319 |
<div class="product-name"><?php esc_html_e('Your Product Name','merchant'); ?></div> |
| 320 |
<p><?php esc_html_e('with {discount} off','merchant'); ?></p> |
| 321 |
<button class="add-to-order"><?php esc_html_e('Add To My Order','merchant'); ?></button> |
| 322 |
</div> |
| 323 |
</div> |
| 324 |
</div> |
| 325 |
</div> |
| 326 |
</div> |
| 327 |
<?php |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Thank you page preview. |
| 332 |
* |
| 333 |
* @return string |
| 334 |
*/ |
| 335 |
public function thank_you_page_preview() { |
| 336 |
?> |
| 337 |
<div class="merchant-thank-you-preview"> |
| 338 |
<div class="order-received"> |
| 339 |
<div class="page-title"><?php esc_html_e('Order Received','merchant'); ?></div> |
| 340 |
<p><?php esc_html_e('Thank you. Your order has been received.','merchant'); ?></p> |
| 341 |
<div class="order-details"> |
| 342 |
<div class="order-info"> |
| 343 |
<div class="item-title"><?php esc_html_e('ORDER NUMBER:','merchant'); ?></div> |
| 344 |
<p>550</p> |
| 345 |
</div> |
| 346 |
<div class="order-info"> |
| 347 |
<div class="item-title"><?php esc_html_e('PAYMENT METHOD:','merchant'); ?></div> |
| 348 |
<p><?php echo esc_html( merchant_get_first_active_payment_gateway_label() ?? 'Apple Pay' ) ?></p> |
| 349 |
</div> |
| 350 |
</div> |
| 351 |
<div class="upsell-offer"> |
| 352 |
<div class="offer-title"><?php esc_html_e('Last chance to get {offer_quantity} x','merchant'); ?></div> |
| 353 |
<div class="product-details"> |
| 354 |
<div class="product-image"></div> |
| 355 |
<div class="product-info"> |
| 356 |
<div class="product-name"><?php esc_html_e('Your Product Name','merchant'); ?></div> |
| 357 |
<p><?php esc_html_e('with {discount} off','merchant'); ?></p> |
| 358 |
<button class="add-to-order"><?php esc_html_e('Add To My Order','merchant'); ?></button> |
| 359 |
</div> |
| 360 |
</div> |
| 361 |
</div> |
| 362 |
</div> |
| 363 |
</div> |
| 364 |
<?php |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Cart item admin preview. |
| 369 |
* |
| 370 |
* @return void |
| 371 |
*/ |
| 372 |
public function cart_item_preview() { |
| 373 |
?> |
| 374 |
<div class="merchant-cart-preview"> |
| 375 |
<div class="my-cart"> |
| 376 |
<div class="cart-title"><?php esc_html_e( 'My Cart', 'merchant' ); ?></div> |
| 377 |
<table class="cart-table"> |
| 378 |
<tr> |
| 379 |
<th class="product-col"><?php esc_html_e( 'PRODUCT', 'merchant' ); ?></th> |
| 380 |
<th class="price-col"><?php esc_html_e( 'PRICE', 'merchant' ); ?></th> |
| 381 |
<th class="quantity-col"><?php esc_html_e( 'QUANTITY', 'merchant' ); ?></th> |
| 382 |
<th class="total-col"><?php esc_html_e( 'TOTAL', 'merchant' ); ?></th> |
| 383 |
</tr> |
| 384 |
<tr class="cart-item"> |
| 385 |
<td class="product-column"> |
| 386 |
<div class="product"> |
| 387 |
<div class="product-image"></div> |
| 388 |
<div class="product-info"> |
| 389 |
<div class="product-name"><?php esc_html_e( 'Your Product Name', 'merchant' ); ?></div> |
| 390 |
<p class="upsell-offer"><?php esc_html_e( 'Buy 3 Get 3 with 20% off', 'merchant' ); ?></p> |
| 391 |
<div class="upsell-product"> |
| 392 |
<div class="upsell-info"> |
| 393 |
<button class="add-to-cart"><?php esc_html_e( 'Add To Cart', 'merchant' ); ?></button> |
| 394 |
</div> |
| 395 |
</div> |
| 396 |
</div> |
| 397 |
</div> |
| 398 |
</td> |
| 399 |
<td class="price-col"> |
| 400 |
<span class="original-price"><?php echo wp_kses(wc_price(16), merchant_kses_allowed_tags(array( 'bdi' )))?></span> |
| 401 |
<span class="discounted-price"><?php echo wp_kses(wc_price(12), merchant_kses_allowed_tags(array( 'bdi' )))?></span> |
| 402 |
</td> |
| 403 |
<td class="quantity-col"> |
| 404 |
<div class="quantity-control"> |
| 405 |
<button class="decrease">-</button> |
| 406 |
<input type="text" value="1" min="1"> |
| 407 |
<button class="increase">+</button> |
| 408 |
</div> |
| 409 |
</td> |
| 410 |
<td class="total-col"><?php echo wp_kses(wc_price(300), merchant_kses_allowed_tags(array( 'bdi' )))?></td> |
| 411 |
</tr> |
| 412 |
</table> |
| 413 |
</div> |
| 414 |
</div> |
| 415 |
<?php |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* Add this function for backward compatibility purpose. |
| 420 |
* |
| 421 |
* @return void |
| 422 |
*/ |
| 423 |
public function get_module_custom_css() { |
| 424 |
// No implementation needed. |
| 425 |
return; |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
// Initialize the module. |
| 431 |
add_action( 'init', function () { |
| 432 |
Merchant_Modules::create_module( new Merchant_Volume_Discounts() ); |
| 433 |
} ); |
| 434 |
|