| 1 |
<?php namespace TierPricingTable\Addons\NonLoggedInUsers\CartRules; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\NonLoggedInUsers\Roles; |
| 4 |
|
| 5 |
/** |
| 6 |
* The WooCommerce settings field with one row per role: minimum order amount and minimum quantity. |
| 7 |
* Saved as JSON. |
| 8 |
*/ |
| 9 |
class RoleMinimumsOption { |
| 10 |
|
| 11 |
const FIELD_TYPE = 'tpt_role_minimums'; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
add_action( 'woocommerce_admin_field_' . self::FIELD_TYPE, array( $this, 'render' ) ); |
| 15 |
|
| 16 |
add_filter( 'woocommerce_admin_settings_sanitize_option', function ( $value, $option, $rawValue ) { |
| 17 |
if ( self::FIELD_TYPE !== ( $option['type'] ?? '' ) ) { |
| 18 |
return $value; |
| 19 |
} |
| 20 |
|
| 21 |
return wp_json_encode( CartRules::normalizeMinimums( is_array( $rawValue ) ? wp_unslash( $rawValue ) : array() ) ); |
| 22 |
}, 10, 3 ); |
| 23 |
} |
| 24 |
|
| 25 |
public function render( $value ) { |
| 26 |
// the description is plugin-defined text (settings arrays), read once for output |
| 27 |
$description = (string) ( $value['desc'] ?? '' ); |
| 28 |
$id = (string) ( $value['id'] ?? '' ); |
| 29 |
$minimums = CartRules::normalizeMinimums( \WC_Admin_Settings::get_option( $id, '' ) ); |
| 30 |
?> |
| 31 |
<tr valign="top"> |
| 32 |
<th scope="row" class="titledesc"> |
| 33 |
<label><?php echo esc_html( $value['title'] ?? '' ); ?></label> |
| 34 |
</th> |
| 35 |
<td class="forminp"> |
| 36 |
<div class="tpt-role-minimums" id="<?php echo esc_attr( $id ); ?>"> |
| 37 |
<input type="hidden" name="<?php echo esc_attr( $id ); ?>[__none__][amount]" value=""> |
| 38 |
<table class="widefat tpt-role-minimums__table"> |
| 39 |
<thead> |
| 40 |
<tr> |
| 41 |
<th><?php esc_html_e( 'Role', 'tier-pricing-table' ); ?></th> |
| 42 |
<th> |
| 43 |
<?php |
| 44 |
/* translators: %s: currency symbol */ |
| 45 |
echo esc_html( sprintf( __( 'Minimum order amount (%s)', 'tier-pricing-table' ), html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, 'UTF-8' ) ) ); |
| 46 |
?> |
| 47 |
</th> |
| 48 |
<th><?php esc_html_e( 'Minimum quantity', 'tier-pricing-table' ); ?></th> |
| 49 |
</tr> |
| 50 |
</thead> |
| 51 |
<tbody> |
| 52 |
<?php foreach ( Roles::getOptions() as $role => $label ) : ?> |
| 53 |
<?php $row = $minimums[ $role ] ?? array( 'amount' => 0, 'quantity' => 0 ); ?> |
| 54 |
<tr> |
| 55 |
<td><?php echo esc_html( $label ); ?></td> |
| 56 |
<td> |
| 57 |
<input type="number" min="0" step="0.01" name="<?php echo esc_attr( $id ); ?>[<?php echo esc_attr( $role ); ?>][amount]" |
| 58 |
value="<?php echo esc_attr( $row['amount'] > 0 ? wc_format_localized_price( $row['amount'] ) : '' ); ?>" placeholder="—"> |
| 59 |
</td> |
| 60 |
<td> |
| 61 |
<input type="number" min="0" step="1" name="<?php echo esc_attr( $id ); ?>[<?php echo esc_attr( $role ); ?>][quantity]" |
| 62 |
value="<?php echo esc_attr( $row['quantity'] > 0 ? $row['quantity'] : '' ); ?>" placeholder="—"> |
| 63 |
</td> |
| 64 |
</tr> |
| 65 |
<?php endforeach; ?> |
| 66 |
</tbody> |
| 67 |
</table> |
| 68 |
<?php if ( ! empty( $value['desc'] ) ) : ?> |
| 69 |
<p class="description"><?php echo wp_kses_post( $description ); // nosemgrep ?></p> |
| 70 |
<?php endif; ?> |
| 71 |
</div> |
| 72 |
<style> |
| 73 |
.woocommerce table.form-table .tpt-role-minimums { max-width: 640px; } |
| 74 |
.woocommerce table.form-table .tpt-role-minimums__table th, |
| 75 |
.woocommerce table.form-table .tpt-role-minimums__table td { padding: 8px 10px; vertical-align: middle; } |
| 76 |
.woocommerce table.form-table .tpt-role-minimums__table th { font-weight: 600; } |
| 77 |
.woocommerce table.form-table .tpt-role-minimums__table input[type=number] { width: 140px; } |
| 78 |
</style> |
| 79 |
</td> |
| 80 |
</tr> |
| 81 |
<?php |
| 82 |
} |
| 83 |
} |
| 84 |
|