Initial commit: WooCommerce Email Discount Manager plugin
This commit is contained in:
@@ -0,0 +1,195 @@
|
||||
<?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' ), true ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rules[] = array(
|
||||
'shipping_method_id' => $method_id,
|
||||
'action' => $action,
|
||||
'sort_order' => $index,
|
||||
);
|
||||
}
|
||||
|
||||
WC_EDM_DB::replace_shipping_rules( $group_id, $rules );
|
||||
|
||||
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", only "Show" methods are offered.', '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'] ); ?> · <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>
|
||||
<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>
|
||||
|
||||
<p class="submit">
|
||||
<button type="submit" class="button button-primary"><?php esc_html_e( 'Save Shipping Rules', 'wc-email-discount' ); ?></button>
|
||||
</p>
|
||||
</form>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user