| 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 |
* Volume discounts 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 |
* Constructor. |
| 42 |
* |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
// Module id. |
| 46 |
$this->module_id = self::MODULE_ID; |
| 47 |
|
| 48 |
// WooCommerce only. |
| 49 |
$this->wc_only = true; |
| 50 |
|
| 51 |
// Parent construct. |
| 52 |
parent::__construct(); |
| 53 |
|
| 54 |
// Module section. |
| 55 |
$this->module_section = 'boost-revenue'; |
| 56 |
|
| 57 |
// Module default settings. |
| 58 |
$this->module_default_settings = array( |
| 59 |
'title' => __( 'Discount', 'merchant' ), |
| 60 |
'single_product_placement' => 'before-cart-form', |
| 61 |
'table_title' => __( 'Buy more, save more!', 'merchant' ), |
| 62 |
'save_label' => esc_html__( 'Save {amount}', 'merchant' ), |
| 63 |
'buy_text' => esc_html__( 'Buy {amount}, get {discount} off each', 'merchant' ), |
| 64 |
'item_text' => esc_html__( 'Per item:', 'merchant' ), |
| 65 |
'total_text' => esc_html__( 'Total price:', 'merchant' ), |
| 66 |
'cart_title_text' => esc_html__( 'Discount', 'merchant' ), |
| 67 |
'cart_description_text' => esc_html__( 'A discount of {amount} has been applied.', 'merchant' ) |
| 68 |
); |
| 69 |
|
| 70 |
// Module data. |
| 71 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 72 |
|
| 73 |
// Module preview URL |
| 74 |
$this->module_data['preview_url'] = $this->set_module_preview_url( array( |
| 75 |
'type' => 'product', |
| 76 |
'query' => array( |
| 77 |
'meta_query' => array( |
| 78 |
'relation' => 'AND', |
| 79 |
array( |
| 80 |
'key' => '_merchant_volume_discounts', |
| 81 |
'value' => '', |
| 82 |
'compare' => '!=' |
| 83 |
), |
| 84 |
array( |
| 85 |
'key' => '_merchant_volume_discounts', |
| 86 |
'value' => 'a:0:{}', |
| 87 |
'compare' => '!=' |
| 88 |
) |
| 89 |
) |
| 90 |
) |
| 91 |
) ); |
| 92 |
|
| 93 |
// Module options path. |
| 94 |
$this->module_options_path = self::MODULE_DIR . '/admin/options.php'; |
| 95 |
|
| 96 |
// Is module preview page. |
| 97 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 98 |
self::$is_module_preview = true; |
| 99 |
|
| 100 |
// Enqueue admin scripts |
| 101 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 102 |
|
| 103 |
// Add preview box |
| 104 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 105 |
|
| 106 |
// Admin custom CSS. |
| 107 |
// The custom CSS should be added here as well due to ensure preview box works properly. |
| 108 |
add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) { |
| 112 |
// Init translations. |
| 113 |
$this->init_translations(); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Init translations. |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function init_translations() { |
| 123 |
$settings = $this->get_module_settings(); |
| 124 |
if ( ! empty( $settings['table_title'] ) ) { |
| 125 |
Merchant_Translator::register_string( $settings['table_title'], esc_html__( 'Bulk discount: title', 'merchant' ) ); |
| 126 |
} |
| 127 |
if ( ! empty( $settings['save_label'] ) ) { |
| 128 |
Merchant_Translator::register_string( $settings['save_label'], esc_html__( 'Bulk discount: save label', 'merchant' ) ); |
| 129 |
} |
| 130 |
if ( ! empty( $settings['buy_text'] ) ) { |
| 131 |
Merchant_Translator::register_string( $settings['buy_text'], esc_html__( 'Bulk discount: buy text', 'merchant' ) ); |
| 132 |
} |
| 133 |
if ( ! empty( $settings['item_text'] ) ) { |
| 134 |
Merchant_Translator::register_string( $settings['item_text'], esc_html__( 'Bulk discount: item text', 'merchant' ) ); |
| 135 |
} |
| 136 |
if ( ! empty( $settings['total_text'] ) ) { |
| 137 |
Merchant_Translator::register_string( $settings['total_text'], esc_html__( 'Bulk discount: total text', 'merchant' ) ); |
| 138 |
} |
| 139 |
if ( ! empty( $settings['cart_title_text'] ) ) { |
| 140 |
Merchant_Translator::register_string( $settings['cart_title_text'], esc_html__( 'Bulk discount: cart item discount title', 'merchant' ) ); |
| 141 |
} |
| 142 |
if ( ! empty( $settings['cart_description_text'] ) ) { |
| 143 |
Merchant_Translator::register_string( $settings['cart_description_text'], esc_html__( 'Bulk discount: Cart item discount description', 'merchant' ) ); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Enqueue admin page content scripts. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public function admin_enqueue_css() { |
| 153 |
if ( $this->is_module_settings_page() ) { |
| 154 |
wp_enqueue_style( "merchant-admin-{$this->module_id}", MERCHANT_URI . "assets/css/modules/volume-discounts/admin/preview.min.css", array(), MERCHANT_VERSION ); |
| 155 |
wp_enqueue_style( "merchant-{$this->module_id}", MERCHANT_URI . "assets/css/modules/{$this->module_id}/{$this->module_id}.min.css", array(), MERCHANT_VERSION ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Render admin preview |
| 161 |
* |
| 162 |
* @param Merchant_Admin_Preview $preview |
| 163 |
* @param string $module |
| 164 |
* |
| 165 |
* @return Merchant_Admin_Preview |
| 166 |
*/ |
| 167 |
public function render_admin_preview( $preview, $module ) { |
| 168 |
if ( $module === self::MODULE_ID ) { |
| 169 |
ob_start(); |
| 170 |
self::admin_preview_content(); |
| 171 |
$content = ob_get_clean(); |
| 172 |
|
| 173 |
// HTML |
| 174 |
$preview->set_html( $content ); |
| 175 |
|
| 176 |
// Table Title |
| 177 |
$preview->set_text( 'table_title', '.merchant-volume-discounts-title' ); |
| 178 |
|
| 179 |
// Save Label |
| 180 |
$preview->set_text( 'save_label', '.merchant-volume-discounts-save-label', array( |
| 181 |
array( |
| 182 |
'{amount}', |
| 183 |
), |
| 184 |
array( |
| 185 |
wc_price( 10 ), |
| 186 |
) |
| 187 |
) ); |
| 188 |
|
| 189 |
// Buy Text |
| 190 |
$preview->set_text( 'buy_text', '.merchant-volume-discounts-buy-label', array( |
| 191 |
array( |
| 192 |
'{amount}', |
| 193 |
'{discount}' |
| 194 |
), |
| 195 |
array( |
| 196 |
'<strong>10</strong>', |
| 197 |
'<strong>' . wc_price( 2 ) . '</strong>' |
| 198 |
) |
| 199 |
) ); |
| 200 |
|
| 201 |
// Item Text |
| 202 |
$preview->set_text( 'item_text', '.merchant-volume-discounts-item-text' ); |
| 203 |
|
| 204 |
// Total Text |
| 205 |
$preview->set_text( 'total_text', '.merchant-volume-discounts-total-text' ); |
| 206 |
|
| 207 |
// Title Font Size |
| 208 |
$preview->set_css( 'title_font_size', '.merchant-volume-discounts-title', '--merchant-font-size', 'px' ); |
| 209 |
|
| 210 |
// Title Font Weight |
| 211 |
$preview->set_css( 'title_font_weight', '.merchant-volume-discounts-title', '--merchant-font-weight' ); |
| 212 |
|
| 213 |
// Title Text Color |
| 214 |
$preview->set_css( 'title_text_color', '.merchant-volume-discounts-title', '--merchant-text-color' ); |
| 215 |
|
| 216 |
// Table Item Background Color |
| 217 |
$preview->set_css( 'table_item_bg_color', '.merchant-volume-discounts-item', '--merchant-table-item-bg-color' ); |
| 218 |
|
| 219 |
// Table Item Text Color |
| 220 |
$preview->set_css( 'table_item_text_color', '.merchant-volume-discounts-item', '--merchant-table-item-text-color' ); |
| 221 |
|
| 222 |
// Table Item Border Color |
| 223 |
$preview->set_css( 'table_item_border_color', '.merchant-volume-discounts-item', '--merchant-table-item-border-color' ); |
| 224 |
|
| 225 |
// Table Label Background Color |
| 226 |
$preview->set_css( 'table_label_bg_color', '.merchant-volume-discounts-item-label > span', '--merchant-table-label-bg-color' ); |
| 227 |
|
| 228 |
// Table label Text Color |
| 229 |
$preview->set_css( 'table_label_text_color', '.merchant-volume-discounts-item-label > span', '--merchant-table-label-text-color' ); |
| 230 |
} |
| 231 |
|
| 232 |
return $preview; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Admin preview content. |
| 237 |
* |
| 238 |
* @return void |
| 239 |
*/ |
| 240 |
public function admin_preview_content() { |
| 241 |
$settings = $this->get_module_settings(); |
| 242 |
?> |
| 243 |
|
| 244 |
<div class="mrc-preview-single-product-elements"> |
| 245 |
<div class="mrc-preview-left-column"> |
| 246 |
<div class="mrc-preview-product-image-wrapper"> |
| 247 |
<div class="mrc-preview-product-image"></div> |
| 248 |
<div class="mrc-preview-product-image-thumbs"> |
| 249 |
<div class="mrc-preview-product-image-thumb"></div> |
| 250 |
<div class="mrc-preview-product-image-thumb"></div> |
| 251 |
<div class="mrc-preview-product-image-thumb"></div> |
| 252 |
</div> |
| 253 |
</div> |
| 254 |
</div> |
| 255 |
<div class="mrc-preview-right-column"> |
| 256 |
<?php |
| 257 |
merchant_get_template_part( |
| 258 |
Merchant_Volume_Discounts::MODULE_TEMPLATES_PATH, |
| 259 |
'single-product', |
| 260 |
array( |
| 261 |
'settings' => $settings, |
| 262 |
'discount_tiers' => array( |
| 263 |
array( 'quantity' => 10, 'discount' => 5, 'layout' => 'percentage_discount' ), |
| 264 |
array( 'quantity' => 20, 'discount' => 10, 'layout' => 'percentage_discount' ) |
| 265 |
), |
| 266 |
'product_price' => 20 |
| 267 |
) |
| 268 |
); |
| 269 |
?> |
| 270 |
</div> |
| 271 |
</div> |
| 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 |
// Title Font Size |
| 285 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'title_font_size', 16, '.merchant-volume-discounts-title', '--merchant-font-size', 'px' ); |
| 286 |
|
| 287 |
// Title Font Weight |
| 288 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'title_font_weight', 'normal', '.merchant-volume-discounts-title', '--merchant-font-weight' ); |
| 289 |
|
| 290 |
// Title Text Color |
| 291 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'title_text_color', '#212121', '.merchant-volume-discounts-title', '--merchant-text-color' ); |
| 292 |
|
| 293 |
// Table Item Background Color |
| 294 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'table_item_bg_color', '#fcf0f1', '.merchant-volume-discounts-item', '--merchant-table-item-bg-color' ); |
| 295 |
|
| 296 |
// Table Item Border Color |
| 297 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'table_item_border_color', '#d83b3b', '.merchant-volume-discounts-item', '--merchant-table-item-border-color' ); |
| 298 |
|
| 299 |
// Table Item Text Color |
| 300 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'table_item_text_color', 'inherit', '.merchant-volume-discounts-item', '--merchant-table-item-text-color' ); |
| 301 |
|
| 302 |
// Table Label Background Color |
| 303 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'table_label_bg_color', '#d83b3b', '.merchant-volume-discounts-item-label > span', '--merchant-table-label-bg-color' ); |
| 304 |
|
| 305 |
// Table Label Text Color |
| 306 |
$css .= Merchant_Custom_CSS::get_variable_css( $this->module_id, 'table_label_text_color', '#ffffff', '.merchant-volume-discounts-item-label > span', '--merchant-table-label-text-color' ); |
| 307 |
|
| 308 |
return $css; |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Admin custom CSS. |
| 313 |
* |
| 314 |
* @param string $css The custom CSS. |
| 315 |
* |
| 316 |
* @return string $css The custom CSS. |
| 317 |
*/ |
| 318 |
public function admin_custom_css( $css ) { |
| 319 |
$css .= $this->get_module_custom_css(); |
| 320 |
|
| 321 |
return $css; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
// Initialize the module. |
| 327 |
add_action( 'init', function () { |
| 328 |
Merchant_Modules::create_module( new Merchant_Volume_Discounts() ); |
| 329 |
} ); |
| 330 |
|