| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Pre-Orders Product Metabox |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Pre-Orders Metabox Class |
| 15 |
* |
| 16 |
* @since 1.0.0 |
| 17 |
*/ |
| 18 |
class Merchant_Pre_Orders_Metabox extends Merchant_Metabox { |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
add_filter( 'merchant_metabox_options', array( $this, 'add_metabox_options' ), 10, 1 ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Add metabox options. |
| 31 |
* |
| 32 |
* @param array $options Existing metabox options. |
| 33 |
* |
| 34 |
* @return array Modified metabox options. |
| 35 |
*/ |
| 36 |
public function add_metabox_options( $options ) { |
| 37 |
// Add Pre-Orders section |
| 38 |
$this->add_section( 'merchant_pre_orders', array( |
| 39 |
'title' => esc_html__( 'Pre-Orders', 'merchant' ), |
| 40 |
'post_type' => array( 'product' ), |
| 41 |
'priority' => 20, |
| 42 |
) ); |
| 43 |
|
| 44 |
// General Tab Fields |
| 45 |
$this->add_field( '_merchant_pre_order_mode', array( |
| 46 |
'section' => 'merchant_pre_orders', |
| 47 |
'type' => 'select', |
| 48 |
'title' => esc_html__( 'Pre-Order Mode', 'merchant' ), |
| 49 |
'desc' => esc_html__( 'Choose how pre-orders should be handled for this product.', 'merchant' ), |
| 50 |
'options' => array( |
| 51 |
'global' => esc_html__( 'Global Settings (Default)', 'merchant' ), |
| 52 |
'product' => esc_html__( 'Product Specific Settings', 'merchant' ), |
| 53 |
'disabled' => esc_html__( 'Disable Pre-Orders', 'merchant' ), |
| 54 |
), |
| 55 |
'default' => 'global', |
| 56 |
) ); |
| 57 |
|
| 58 |
$this->add_field( '_merchant_pre_order_start', array( |
| 59 |
'section' => 'merchant_pre_orders', |
| 60 |
'type' => 'date_time', |
| 61 |
'title' => esc_html__( 'Pre-Order Starts At', 'merchant' ), |
| 62 |
'desc' => esc_html__( 'If you want your pre-order settings to take effect immediately, leave the pre-order start empty.', 'merchant' ), |
| 63 |
'placeholder' => esc_html__( 'Select date and time', 'merchant' ), |
| 64 |
'options' => array( |
| 65 |
'timepicker' => true, |
| 66 |
'minDate' => 'today', |
| 67 |
), |
| 68 |
'conditions' => array( |
| 69 |
'relation' => 'AND', |
| 70 |
'terms' => array( |
| 71 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 72 |
), |
| 73 |
), |
| 74 |
) ); |
| 75 |
|
| 76 |
$this->add_field( '_merchant_pre_order_end', array( |
| 77 |
'section' => 'merchant_pre_orders', |
| 78 |
'type' => 'date_time', |
| 79 |
'title' => esc_html__( 'Pre-Order Ends At', 'merchant' ), |
| 80 |
'desc' => sprintf( |
| 81 |
/* Translators: %1$s: Time zone, %2$s WordPress setting link */ |
| 82 |
esc_html__( 'Leave it empty if you don’t want to have an end date. The times set above are in the %1$s timezone, according to your settings from %2$s.', 'merchant' ), |
| 83 |
'<strong>' . wp_timezone_string() . '</strong>', |
| 84 |
'<a href="' . esc_url( admin_url( 'options-general.php' ) ) . '" target="_blank">' . esc_html__( 'WordPress Settings', 'merchant' ) . '</a>' |
| 85 |
), |
| 86 |
'placeholder' => esc_html__( 'Select date and time', 'merchant' ), |
| 87 |
'options' => array( |
| 88 |
'timepicker' => true, |
| 89 |
'minDate' => 'today', |
| 90 |
), |
| 91 |
'conditions' => array( |
| 92 |
'relation' => 'AND', |
| 93 |
'terms' => array( |
| 94 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 95 |
), |
| 96 |
), |
| 97 |
) ); |
| 98 |
|
| 99 |
$this->add_field( '_merchant_pre_order_shipping_date', array( |
| 100 |
'section' => 'merchant_pre_orders', |
| 101 |
'type' => 'date_time', |
| 102 |
'title' => esc_html__( 'Expected Shipping Date', 'merchant' ), |
| 103 |
'desc' => sprintf( |
| 104 |
/* Translators: %1$s: Time zone, %2$s WordPress setting link */ |
| 105 |
esc_html__( 'The times set above are in the %1$s timezone, according to your settings from %2$s.', 'merchant' ), |
| 106 |
'<strong>' . wp_timezone_string() . '</strong>', |
| 107 |
'<a href="' . esc_url( admin_url( 'options-general.php' ) ) . '" target="_blank">' . esc_html__( 'WordPress Settings', 'merchant' ) . '</a>' |
| 108 |
), |
| 109 |
'placeholder' => esc_html__( 'Select date and time', 'merchant' ), |
| 110 |
'options' => array( |
| 111 |
'timepicker' => true, |
| 112 |
'minDate' => 'today', |
| 113 |
), |
| 114 |
'conditions' => array( |
| 115 |
'relation' => 'AND', |
| 116 |
'terms' => array( |
| 117 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 118 |
), |
| 119 |
), |
| 120 |
) ); |
| 121 |
|
| 122 |
// Discount Tab Fields |
| 123 |
$this->add_field( '_merchant_pre_order_discount_toggle', array( |
| 124 |
'section' => 'merchant_pre_orders', |
| 125 |
'type' => 'switcher', |
| 126 |
'title' => esc_html__( 'Offer Discount', 'merchant' ), |
| 127 |
'desc' => esc_html__( 'Enable to offer a discount for pre-ordering this product.', 'merchant' ), |
| 128 |
'default' => 0, |
| 129 |
'conditions' => array( |
| 130 |
'relation' => 'AND', |
| 131 |
'terms' => array( |
| 132 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 133 |
), |
| 134 |
), |
| 135 |
) ); |
| 136 |
|
| 137 |
$this->add_field( '_merchant_pre_order_discount_type', array( |
| 138 |
'section' => 'merchant_pre_orders', |
| 139 |
'type' => 'radio', |
| 140 |
'title' => esc_html__( 'Discount Type', 'merchant' ), |
| 141 |
'options' => array( |
| 142 |
'percentage' => esc_html__( 'Percentage', 'merchant' ), |
| 143 |
'fixed' => esc_html__( 'Fixed Amount', 'merchant' ), |
| 144 |
), |
| 145 |
'default' => 'percentage', |
| 146 |
'conditions' => array( |
| 147 |
'relation' => 'AND', |
| 148 |
'terms' => array( |
| 149 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 150 |
array( '_merchant_pre_order_discount_toggle', '==', '1' ), |
| 151 |
), |
| 152 |
), |
| 153 |
) ); |
| 154 |
|
| 155 |
$this->add_field( '_merchant_pre_order_discount_amount', array( |
| 156 |
'section' => 'merchant_pre_orders', |
| 157 |
'type' => 'number', |
| 158 |
'title' => esc_html__( 'Discount Amount', 'merchant' ), |
| 159 |
'desc' => esc_html__( 'Enter the discount amount (percentage or fixed).', 'merchant' ), |
| 160 |
'default' => 0, |
| 161 |
'conditions' => array( |
| 162 |
'relation' => 'AND', |
| 163 |
'terms' => array( |
| 164 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 165 |
array( '_merchant_pre_order_discount_toggle', '==', '1' ), |
| 166 |
), |
| 167 |
), |
| 168 |
) ); |
| 169 |
|
| 170 |
// User Conditions Tab Fields |
| 171 |
$this->add_field( '_merchant_pre_order_user_condition', array( |
| 172 |
'section' => 'merchant_pre_orders', |
| 173 |
'type' => 'select', |
| 174 |
'title' => esc_html__( 'User Condition', 'merchant' ), |
| 175 |
'desc' => esc_html__( 'Who can pre-order this product?', 'merchant' ), |
| 176 |
'options' => array( |
| 177 |
'all' => esc_html__( 'All Users', 'merchant' ), |
| 178 |
'roles' => esc_html__( 'Specific Roles', 'merchant' ), |
| 179 |
'users' => esc_html__( 'Specific Users', 'merchant' ), |
| 180 |
), |
| 181 |
'default' => 'all', |
| 182 |
'conditions' => array( |
| 183 |
'relation' => 'AND', |
| 184 |
'terms' => array( |
| 185 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 186 |
), |
| 187 |
), |
| 188 |
) ); |
| 189 |
|
| 190 |
$this->add_field( '_merchant_pre_order_user_condition_roles', array( |
| 191 |
'section' => 'merchant_pre_orders', |
| 192 |
'type' => 'select', |
| 193 |
'title' => esc_html__( 'Allowed Roles', 'merchant' ), |
| 194 |
'desc' => esc_html__( 'This will limit the rule to users with these roles.', 'merchant' ), |
| 195 |
'options' => $this->get_user_roles_choices(), |
| 196 |
'multiple' => true, |
| 197 |
'conditions' => array( |
| 198 |
'relation' => 'AND', |
| 199 |
'terms' => array( |
| 200 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 201 |
array( '_merchant_pre_order_user_condition', '==', 'roles' ), |
| 202 |
), |
| 203 |
), |
| 204 |
) ); |
| 205 |
|
| 206 |
$this->add_field( '_merchant_pre_order_user_condition_users', array( |
| 207 |
'section' => 'merchant_pre_orders', |
| 208 |
'type' => 'select-ajax', |
| 209 |
'title' => esc_html__( 'Allowed Users', 'merchant' ), |
| 210 |
'desc' => esc_html__( 'This will limit the rule to the selected customers.', 'merchant' ), |
| 211 |
'source' => 'user', |
| 212 |
'multiple' => true, |
| 213 |
'conditions' => array( |
| 214 |
'relation' => 'AND', |
| 215 |
'terms' => array( |
| 216 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 217 |
array( '_merchant_pre_order_user_condition', '==', 'users' ), |
| 218 |
), |
| 219 |
), |
| 220 |
) ); |
| 221 |
|
| 222 |
$this->add_field( '_merchant_pre_order_user_exclusion_enabled', array( |
| 223 |
'section' => 'merchant_pre_orders', |
| 224 |
'type' => 'switcher', |
| 225 |
'title' => esc_html__( 'Enable Exclusions', 'merchant' ), |
| 226 |
'desc' => esc_html__( 'Select the users that will not show the offer.', 'merchant' ), |
| 227 |
'default' => 0, |
| 228 |
'conditions' => array( |
| 229 |
'relation' => 'AND', |
| 230 |
'terms' => array( |
| 231 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 232 |
), |
| 233 |
), |
| 234 |
) ); |
| 235 |
|
| 236 |
$this->add_field( '_merchant_pre_order_exclude_roles', array( |
| 237 |
'section' => 'merchant_pre_orders', |
| 238 |
'type' => 'select', |
| 239 |
'title' => esc_html__( 'Excluded Roles', 'merchant' ), |
| 240 |
'desc' => esc_html__( 'This will exclude the offer for users with these roles.', 'merchant' ), |
| 241 |
'options' => $this->get_user_roles_choices(), |
| 242 |
'multiple' => true, |
| 243 |
'conditions' => array( |
| 244 |
'relation' => 'AND', |
| 245 |
'terms' => array( |
| 246 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 247 |
array( '_merchant_pre_order_user_exclusion_enabled', '==', '1' ), |
| 248 |
), |
| 249 |
), |
| 250 |
) ); |
| 251 |
|
| 252 |
$this->add_field( '_merchant_pre_order_exclude_users', array( |
| 253 |
'section' => 'merchant_pre_orders', |
| 254 |
'type' => 'select-ajax', |
| 255 |
'title' => esc_html__( 'Excluded Users', 'merchant' ), |
| 256 |
'desc' => esc_html__( 'This will exclude the offer for the selected customers.', 'merchant' ), |
| 257 |
'source' => 'user', |
| 258 |
'multiple' => true, |
| 259 |
'conditions' => array( |
| 260 |
'relation' => 'AND', |
| 261 |
'terms' => array( |
| 262 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 263 |
array( '_merchant_pre_order_user_exclusion_enabled', '==', '1' ), |
| 264 |
), |
| 265 |
), |
| 266 |
) ); |
| 267 |
|
| 268 |
// Styles / Text Tab Fields |
| 269 |
$this->add_field( '_merchant_pre_order_button_text', array( |
| 270 |
'section' => 'merchant_pre_orders', |
| 271 |
'type' => 'text', |
| 272 |
'title' => esc_html__( 'Button Text', 'merchant' ), |
| 273 |
'desc' => esc_html__( 'Custom text for the pre-order button.', 'merchant' ), |
| 274 |
'placeholder' => esc_html__( 'Pre-Order Now', 'merchant' ), |
| 275 |
'conditions' => array( |
| 276 |
'relation' => 'AND', |
| 277 |
'terms' => array( |
| 278 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 279 |
), |
| 280 |
), |
| 281 |
) ); |
| 282 |
|
| 283 |
$this->add_field( '_merchant_pre_order_cart_label_text', array( |
| 284 |
'section' => 'merchant_pre_orders', |
| 285 |
'type' => 'text', |
| 286 |
'title' => esc_html__( 'Cart Label Text', 'merchant' ), |
| 287 |
'desc' => esc_html__( 'Text to display in the cart for pre-order items.', 'merchant' ), |
| 288 |
'placeholder' => esc_html__( 'Pre-Order', 'merchant' ), |
| 289 |
'conditions' => array( |
| 290 |
'relation' => 'AND', |
| 291 |
'terms' => array( |
| 292 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 293 |
), |
| 294 |
), |
| 295 |
) ); |
| 296 |
|
| 297 |
$this->add_field( '_merchant_pre_order_additional_text', array( |
| 298 |
'section' => 'merchant_pre_orders', |
| 299 |
'type' => 'text', |
| 300 |
'title' => esc_html__( 'Additional Information Text', 'merchant' ), |
| 301 |
'desc' => __( 'Additional text to display on the product page, use <strong>{date}</strong> to display the shipping date.', 'merchant' ), |
| 302 |
'placeholder' => esc_html__( 'Ships on {date}', 'merchant' ), |
| 303 |
'conditions' => array( |
| 304 |
'relation' => 'AND', |
| 305 |
'terms' => array( |
| 306 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 307 |
), |
| 308 |
), |
| 309 |
) ); |
| 310 |
|
| 311 |
$this->add_field( '_merchant_pre_order_placement', array( |
| 312 |
'section' => 'merchant_pre_orders', |
| 313 |
'type' => 'radio', |
| 314 |
'title' => esc_html__( 'Placement', 'merchant' ), |
| 315 |
'desc' => esc_html__( 'Choose where to display the pre-order information.', 'merchant' ), |
| 316 |
'options' => array( |
| 317 |
'before' => esc_html__( 'Before Button', 'merchant' ), |
| 318 |
'after' => esc_html__( 'After Button', 'merchant' ), |
| 319 |
), |
| 320 |
'default' => 'before', |
| 321 |
'conditions' => array( |
| 322 |
'relation' => 'AND', |
| 323 |
'terms' => array( |
| 324 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 325 |
), |
| 326 |
), |
| 327 |
) ); |
| 328 |
|
| 329 |
$this->add_field( '_merchant_pre_order_text_color', array( |
| 330 |
'section' => 'merchant_pre_orders', |
| 331 |
'type' => 'color', |
| 332 |
'title' => esc_html__( 'Button Text Color', 'merchant' ), |
| 333 |
'default' => '#ffffff', |
| 334 |
'conditions' => array( |
| 335 |
'relation' => 'AND', |
| 336 |
'terms' => array( |
| 337 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 338 |
), |
| 339 |
), |
| 340 |
) ); |
| 341 |
|
| 342 |
$this->add_field( '_merchant_pre_order_text_hover_color', array( |
| 343 |
'section' => 'merchant_pre_orders', |
| 344 |
'type' => 'color', |
| 345 |
'title' => esc_html__( 'Button Text Color (Hover)', 'merchant' ), |
| 346 |
'default' => '#ffffff', |
| 347 |
'conditions' => array( |
| 348 |
'relation' => 'AND', |
| 349 |
'terms' => array( |
| 350 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 351 |
), |
| 352 |
), |
| 353 |
) ); |
| 354 |
|
| 355 |
$this->add_field( '_merchant_pre_order_border_color', array( |
| 356 |
'section' => 'merchant_pre_orders', |
| 357 |
'type' => 'color', |
| 358 |
'title' => esc_html__( 'Button Border Color', 'merchant' ), |
| 359 |
'default' => '#212121', |
| 360 |
'conditions' => array( |
| 361 |
'relation' => 'AND', |
| 362 |
'terms' => array( |
| 363 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 364 |
), |
| 365 |
), |
| 366 |
) ); |
| 367 |
|
| 368 |
$this->add_field( '_merchant_pre_order_border_hover_color', array( |
| 369 |
'section' => 'merchant_pre_orders', |
| 370 |
'type' => 'color', |
| 371 |
'title' => esc_html__( 'Button Border Color (Hover)', 'merchant' ), |
| 372 |
'default' => '#000000', |
| 373 |
'conditions' => array( |
| 374 |
'relation' => 'AND', |
| 375 |
'terms' => array( |
| 376 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 377 |
), |
| 378 |
), |
| 379 |
) ); |
| 380 |
|
| 381 |
$this->add_field( '_merchant_pre_order_border_width', array( |
| 382 |
'section' => 'merchant_pre_orders', |
| 383 |
'type' => 'range', |
| 384 |
'title' => esc_html__( 'Button Border Width (px)', 'merchant' ), |
| 385 |
'min' => 0, |
| 386 |
'max' => 10, |
| 387 |
'step' => 1, |
| 388 |
'default' => 0, |
| 389 |
'conditions' => array( |
| 390 |
'relation' => 'AND', |
| 391 |
'terms' => array( |
| 392 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 393 |
), |
| 394 |
), |
| 395 |
) ); |
| 396 |
|
| 397 |
$this->add_field( '_merchant_pre_order_border_radius', array( |
| 398 |
'section' => 'merchant_pre_orders', |
| 399 |
'type' => 'range', |
| 400 |
'title' => esc_html__( 'Button Border Radius (px)', 'merchant' ), |
| 401 |
'min' => 0, |
| 402 |
'max' => 50, |
| 403 |
'step' => 1, |
| 404 |
'default' => 0, |
| 405 |
'conditions' => array( |
| 406 |
'relation' => 'AND', |
| 407 |
'terms' => array( |
| 408 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 409 |
), |
| 410 |
), |
| 411 |
) ); |
| 412 |
|
| 413 |
$this->add_field( '_merchant_pre_order_background_color', array( |
| 414 |
'section' => 'merchant_pre_orders', |
| 415 |
'type' => 'color', |
| 416 |
'title' => esc_html__( 'Button Background Color', 'merchant' ), |
| 417 |
'default' => '#212121', |
| 418 |
'conditions' => array( |
| 419 |
'relation' => 'AND', |
| 420 |
'terms' => array( |
| 421 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 422 |
), |
| 423 |
), |
| 424 |
) ); |
| 425 |
|
| 426 |
$this->add_field( '_merchant_pre_order_background_hover_color', array( |
| 427 |
'section' => 'merchant_pre_orders', |
| 428 |
'type' => 'color', |
| 429 |
'title' => esc_html__( 'Button Background Color (Hover)', 'merchant' ), |
| 430 |
'default' => '#000000', |
| 431 |
'conditions' => array( |
| 432 |
'relation' => 'AND', |
| 433 |
'terms' => array( |
| 434 |
array( '_merchant_pre_order_mode', '==', 'product' ), |
| 435 |
), |
| 436 |
), |
| 437 |
) ); |
| 438 |
|
| 439 |
return $options; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Get user roles for select choices. |
| 444 |
* |
| 445 |
* @return array User roles choices. |
| 446 |
*/ |
| 447 |
private function get_user_roles_choices() { |
| 448 |
$user_roles = Merchant_Admin_Options::get_user_roles_select2_choices(); |
| 449 |
|
| 450 |
if ( empty( $user_roles ) || ! is_array( $user_roles ) ) { |
| 451 |
return array(); |
| 452 |
} |
| 453 |
|
| 454 |
return array_column( $user_roles, 'text', 'id' ); |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
// Initialize the metabox |
| 459 |
new Merchant_Pre_Orders_Metabox(); |
| 460 |
|