Files
nolimit-discounts/includes/class-admin-shipping.php
T
kristianwindandClaude Opus 5.5 69f09574da
CI / WordPress latest + WooCommerce latest on PHP 8.3 (pull_request) Successful in 1m12s
Compatibility / WP latest / WC latest / PHP 7.4 (pull_request) Skipped
Compatibility / WP latest / WC beta / PHP 8.3 (pull_request) Skipped
Compatibility / WP latest / WC latest / PHP 8.3 (pull_request) Skipped
Compatibility / WP nightly / WC latest / PHP 8.3 (pull_request) Skipped
Compatibility / WP latest / WC latest / PHP 8.4 (pull_request) Skipped
Compatibility / Keep the compatibility headers honest (pull_request) Skipped
CI / Static checks (pull_request) Successful in 4m48s
1.8.1: a group's own coupon and the group discount do not stack
A Netic member using the teamnetic coupon on garageristeriet.dk got 20%
from the coupon and then 20% off the remainder as an "Email discount"
fee: 36% in all. When a cart holds one of the coupons the group lists
under Shipping Rules -> Coupon codes, the group's percentage fee is
skipped and the coupon is the discount. Coupons the group does not list
stack as before.

Smoke tests: 104 assertions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-18 12:33:20 +01:00

221 lines
8.9 KiB
PHP

<?php
/**
* Shipping rules admin.
*
* @package WC_Email_Discount_Manager
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Configure per-group show/hide rules for shipping methods.
*/
class WC_EDM_Admin_Shipping {
/**
* Handle save actions for the shipping tab. Runs on admin_init.
*/
public function handle_actions() {
if ( ! isset( $_POST['wc_edm_action'] ) || 'save_shipping_rules' !== $_POST['wc_edm_action'] ) {
return;
}
check_admin_referer( 'wc_edm_save_shipping_rules' );
$group_id = isset( $_POST['group_id'] ) ? absint( $_POST['group_id'] ) : 0;
if ( ! $group_id || ! WC_EDM_DB::get_group( $group_id ) ) {
wp_safe_redirect( WC_EDM_Helpers::admin_url( array( 'tab' => 'shipping', 'message' => 'error' ) ) );
exit;
}
$order = isset( $_POST['method_order'] ) && is_array( $_POST['method_order'] )
? array_map( 'sanitize_text_field', wp_unslash( $_POST['method_order'] ) )
: array();
$raw_rules = isset( $_POST['rule'] ) && is_array( $_POST['rule'] )
? array_map( 'sanitize_text_field', wp_unslash( $_POST['rule'] ) )
: array();
$known_methods = WC_EDM_Helpers::get_all_shipping_methods();
// Rules are replaced wholesale: only methods with an explicit show/hide choice are stored.
$rules = array();
foreach ( $order as $index => $method_id ) {
if ( ! isset( $known_methods[ $method_id ] ) ) {
continue;
}
$action = isset( $raw_rules[ $method_id ] ) ? $raw_rules[ $method_id ] : '';
if ( ! in_array( $action, array( 'show', 'hide', 'only' ), true ) ) {
continue;
}
$rules[] = array(
'shipping_method_id' => $method_id,
'action' => $action,
'sort_order' => $index,
);
}
WC_EDM_DB::replace_shipping_rules( $group_id, $rules );
$coupons = isset( $_POST['shipping_coupons'] ) ? sanitize_text_field( wp_unslash( $_POST['shipping_coupons'] ) ) : '';
WC_EDM_DB::set_group_shipping_coupons( $group_id, WC_EDM_DB::parse_coupon_list( $coupons ) );
wp_safe_redirect(
WC_EDM_Helpers::admin_url(
array(
'tab' => 'shipping',
'group_id' => $group_id,
'message' => 'saved',
)
)
);
exit;
}
/**
* Render the shipping rules tab.
*/
public function render() {
$groups = WC_EDM_DB::get_groups();
?>
<h2><?php esc_html_e( 'Shipping Rules', 'wc-email-discount' ); ?></h2>
<p class="description">
<?php esc_html_e( 'Choose which shipping methods are shown or hidden for customers in a discount group. If a group has no rules, all shipping methods behave normally. If any method is set to "Show" or "Only", only those methods are offered to the group. "Only" also hides the method from every customer outside the group, which is what free delivery for one company needs; "Show" leaves it available to everybody.', 'wc-email-discount' ); ?>
</p>
<?php if ( empty( $groups ) ) : ?>
<p>
<?php esc_html_e( 'Create a discount group first to configure shipping rules.', 'wc-email-discount' ); ?>
<a href="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'groups', 'action' => 'new' ) ) ); ?>">
<?php esc_html_e( 'Add Discount Group', 'wc-email-discount' ); ?>
</a>
</p>
<?php return; ?>
<?php endif; ?>
<?php
$group_id = isset( $_GET['group_id'] ) ? absint( $_GET['group_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
?>
<form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" class="wc-edm-group-selector">
<input type="hidden" name="page" value="wc-email-discounts">
<input type="hidden" name="tab" value="shipping">
<label for="wc-edm-shipping-group"><?php esc_html_e( 'Discount group:', 'wc-email-discount' ); ?></label>
<select id="wc-edm-shipping-group" name="group_id">
<option value=""><?php esc_html_e( '— Select a group —', 'wc-email-discount' ); ?></option>
<?php foreach ( $groups as $group ) : ?>
<option value="<?php echo esc_attr( (int) $group->id ); ?>" <?php selected( $group_id, (int) $group->id ); ?>>
<?php echo esc_html( $group->name ); ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit" class="button"><?php esc_html_e( 'Select', 'wc-email-discount' ); ?></button>
</form>
<?php
if ( ! $group_id || ! WC_EDM_DB::get_group( $group_id ) ) {
return;
}
$this->render_rules_form( $group_id );
}
/**
* Render the rules form for a selected group.
*
* @param int $group_id Group ID.
*/
private function render_rules_form( $group_id ) {
$methods = WC_EDM_Helpers::get_all_shipping_methods();
$rules = WC_EDM_DB::get_shipping_rules( $group_id );
if ( empty( $methods ) ) {
echo '<p>' . esc_html__( 'No shipping methods found. Configure shipping zones in WooCommerce first.', 'wc-email-discount' ) . '</p>';
return;
}
// Existing rules by method id, and the display order: ruled methods first (by sort_order), then the rest.
$rules_by_method = array();
foreach ( $rules as $rule ) {
$rules_by_method[ $rule->shipping_method_id ] = $rule;
}
$ordered_keys = array_merge(
array_values( array_intersect( array_keys( $rules_by_method ), array_keys( $methods ) ) ),
array_values( array_diff( array_keys( $methods ), array_keys( $rules_by_method ) ) )
);
?>
<form method="post" action="<?php echo esc_url( WC_EDM_Helpers::admin_url( array( 'tab' => 'shipping' ) ) ); ?>">
<?php wp_nonce_field( 'wc_edm_save_shipping_rules' ); ?>
<input type="hidden" name="wc_edm_action" value="save_shipping_rules">
<input type="hidden" name="group_id" value="<?php echo esc_attr( $group_id ); ?>">
<ul id="wc-edm-shipping-methods" class="wc-edm-sortable wc-edm-shipping-list">
<?php foreach ( $ordered_keys as $method_id ) : ?>
<?php
$method = $methods[ $method_id ];
$action = isset( $rules_by_method[ $method_id ] ) ? $rules_by_method[ $method_id ]->action : '';
?>
<li>
<span class="dashicons dashicons-menu wc-edm-drag-handle"></span>
<span class="wc-edm-item-label">
<strong><?php echo esc_html( $method['label'] ); ?></strong>
<span class="wc-edm-item-meta"><?php echo esc_html( $method['zone'] ); ?> &middot; <code><?php echo esc_html( $method_id ); ?></code></span>
</span>
<span class="wc-edm-rule-options">
<label>
<input type="radio" name="rule[<?php echo esc_attr( $method_id ); ?>]" value="" <?php checked( $action, '' ); ?>>
<?php esc_html_e( 'Default', 'wc-email-discount' ); ?>
</label>
<label>
<input type="radio" name="rule[<?php echo esc_attr( $method_id ); ?>]" value="show" <?php checked( $action, 'show' ); ?>>
<?php esc_html_e( 'Show', 'wc-email-discount' ); ?>
</label>
<label title="<?php esc_attr_e( 'Shown to this group, and hidden from everybody else', 'wc-email-discount' ); ?>">
<input type="radio" name="rule[<?php echo esc_attr( $method_id ); ?>]" value="only" <?php checked( $action, 'only' ); ?>>
<?php esc_html_e( 'Only', 'wc-email-discount' ); ?>
</label>
<label>
<input type="radio" name="rule[<?php echo esc_attr( $method_id ); ?>]" value="hide" <?php checked( $action, 'hide' ); ?>>
<?php esc_html_e( 'Hide', 'wc-email-discount' ); ?>
</label>
</span>
<input type="hidden" name="method_order[]" value="<?php echo esc_attr( $method_id ); ?>">
</li>
<?php endforeach; ?>
</ul>
<p class="description"><?php esc_html_e( 'Drag to reorder — the order here controls the order shipping methods appear in at checkout.', 'wc-email-discount' ); ?></p>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="wc-edm-shipping-coupons"><?php esc_html_e( 'Coupon codes', 'wc-email-discount' ); ?></label></th>
<td>
<input type="text" id="wc-edm-shipping-coupons" name="shipping_coupons" class="regular-text" list="wc-edm-coupon-list"
value="<?php echo esc_attr( implode( ', ', WC_EDM_DB::get_group_shipping_coupons( $group_id ) ) ); ?>">
<datalist id="wc-edm-coupon-list">
<?php foreach ( WC_EDM_Helpers::get_coupon_codes() as $code ) : ?>
<option value="<?php echo esc_attr( $code ); ?>"></option>
<?php endforeach; ?>
</datalist>
<p class="description">
<?php esc_html_e( 'A customer with one of these coupons in the cart gets this group\'s shipping rules too, whatever their email address. Separate several with commas. Only the shipping rules follow the coupon — the coupon gives its own discount, not the group\'s. A group member who uses one of these coupons gets the coupon\'s discount instead of the group\'s, never both. To keep the shipping for the right people, restrict the coupon to their addresses under the coupon\'s "Usage restriction" → "Allowed emails" in WooCommerce.', 'wc-email-discount' ); ?>
</p>
</td>
</tr>
</table>
<p class="submit">
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Shipping Rules', 'wc-email-discount' ); ?></button>
</p>
</form>
<?php
}
}