1.8.0: a coupon can carry a group's shipping rules
CI / WordPress latest + WooCommerce latest on PHP 8.3 (pull_request) Successful in 1m11s
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 4m26s
CI / WordPress latest + WooCommerce latest on PHP 8.3 (pull_request) Successful in 1m11s
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 4m26s
Shipping Rules gets a "Coupon codes" field per group. A customer with one of those coupons in the cart gets the group's shipping methods, including "Only" ones, whatever their email. Asked for on a live shop: free delivery reserved for one company's addresses had to reach people using that company's coupon too. Only the shipping follows the coupon; the group's percentage discount stays with its email assignments. An email assigned to a group still wins. The shipping package key now carries the effective group, so WooCommerce's rate cache follows a coupon being added or removed. Stored as groups.shipping_coupons (dbDelta adds the column on update). Smoke tests: 100 assertions. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
b9d8611ce2
commit
654d8632d2
@@ -64,6 +64,9 @@ class WC_EDM_Admin_Shipping {
|
||||
|
||||
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(
|
||||
@@ -190,6 +193,24 @@ class WC_EDM_Admin_Shipping {
|
||||
</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. 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>
|
||||
|
||||
@@ -45,6 +45,7 @@ class WC_EDM_DB {
|
||||
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
name varchar(191) NOT NULL,
|
||||
discount_percent decimal(5,2) NOT NULL DEFAULT 0.00,
|
||||
shipping_coupons text NULL,
|
||||
created_at datetime NOT NULL,
|
||||
updated_at datetime NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
@@ -298,6 +299,87 @@ class WC_EDM_DB {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coupon codes that give a customer this group's shipping rules.
|
||||
*
|
||||
* @param int $id Group ID.
|
||||
* @return string[] Normalised coupon codes.
|
||||
*/
|
||||
public static function get_group_shipping_coupons( $id ) {
|
||||
$group = self::get_group( $id );
|
||||
|
||||
return $group ? self::parse_coupon_list( isset( $group->shipping_coupons ) ? $group->shipping_coupons : '' ) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the coupon codes that give a customer this group's shipping rules.
|
||||
*
|
||||
* @param int $id Group ID.
|
||||
* @param string[] $codes Coupon codes.
|
||||
* @return bool
|
||||
*/
|
||||
public static function set_group_shipping_coupons( $id, $codes ) {
|
||||
global $wpdb;
|
||||
|
||||
$codes = self::parse_coupon_list( implode( ',', (array) $codes ) );
|
||||
|
||||
return false !== $wpdb->update(
|
||||
self::table( 'groups' ),
|
||||
array(
|
||||
'shipping_coupons' => $codes ? implode( ',', $codes ) : null,
|
||||
'updated_at' => current_time( 'mysql' ),
|
||||
),
|
||||
array( 'id' => $id ),
|
||||
array( '%s', '%s' ),
|
||||
array( '%d' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The group whose shipping rules one of these applied coupons unlocks.
|
||||
*
|
||||
* @param string[] $applied Coupon codes applied to the cart.
|
||||
* @return int Group ID, or 0.
|
||||
*/
|
||||
public static function group_for_coupons( $applied ) {
|
||||
$applied = self::parse_coupon_list( implode( ',', (array) $applied ) );
|
||||
|
||||
if ( empty( $applied ) ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Ordered by name, so the answer is the same on every request when
|
||||
// two groups claim the same coupon.
|
||||
foreach ( self::get_groups() as $group ) {
|
||||
$codes = self::parse_coupon_list( isset( $group->shipping_coupons ) ? $group->shipping_coupons : '' );
|
||||
|
||||
if ( array_intersect( $codes, $applied ) ) {
|
||||
return (int) $group->id;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a comma/whitespace separated list into normalised coupon codes.
|
||||
*
|
||||
* @param string $list Raw list.
|
||||
* @return string[]
|
||||
*/
|
||||
public static function parse_coupon_list( $list ) {
|
||||
$codes = array();
|
||||
|
||||
foreach ( preg_split( '/[\s,]+/', (string) $list ) as $code ) {
|
||||
$code = wc_format_coupon_code( $code );
|
||||
if ( '' !== $code ) {
|
||||
$codes[] = $code;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values( array_unique( $codes ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a group and its related rows.
|
||||
*
|
||||
|
||||
@@ -56,7 +56,7 @@ class WC_EDM_Frontend {
|
||||
*/
|
||||
public function key_packages_to_match( $packages ) {
|
||||
$match = $this->get_current_match();
|
||||
$key = $match ? (int) $match->id : 0;
|
||||
$key = ( $match ? (int) $match->id : 0 ) . ':' . $this->shipping_group_id( $match );
|
||||
|
||||
foreach ( $packages as $index => $package ) {
|
||||
$packages[ $index ]['wc_edm_match'] = $key;
|
||||
@@ -367,12 +367,13 @@ class WC_EDM_Frontend {
|
||||
* @return array
|
||||
*/
|
||||
public function filter_shipping_rates( $rates, $package ) {
|
||||
$match = $this->get_current_match();
|
||||
$match = $this->get_current_match();
|
||||
$group_id = $this->shipping_group_id( $match );
|
||||
|
||||
// $match is null for a customer who matches nothing — the common case,
|
||||
// and the one this whole method exists to constrain now.
|
||||
if ( $match && 'group' === $match->assignment_type && $match->group_id ) {
|
||||
$rates = $this->apply_group_shipping_rules( $rates, (int) $match->group_id );
|
||||
// 0 for a customer who matches nothing and holds no linked coupon —
|
||||
// the common case, and the one this whole method exists to constrain.
|
||||
if ( $group_id ) {
|
||||
$rates = $this->apply_group_shipping_rules( $rates, $group_id );
|
||||
} else {
|
||||
// 🔴 Everyone else — including a matched customer whose assignment
|
||||
// is not a group — must not see a method some group reserved.
|
||||
@@ -397,6 +398,28 @@ class WC_EDM_Frontend {
|
||||
return $rates;
|
||||
}
|
||||
|
||||
/**
|
||||
* The group whose shipping rules apply to this customer, or 0.
|
||||
*
|
||||
* An email assigned to a group wins. Otherwise a coupon in the cart that a
|
||||
* group lists under "Coupon codes" gives that group's shipping rules — the
|
||||
* shipping only; the group's percentage discount stays with its emails.
|
||||
*
|
||||
* @param object|null $match Email assignment row.
|
||||
* @return int
|
||||
*/
|
||||
private function shipping_group_id( $match ) {
|
||||
if ( $match && 'group' === $match->assignment_type && $match->group_id ) {
|
||||
return (int) $match->group_id;
|
||||
}
|
||||
|
||||
if ( ! function_exists( 'WC' ) || ! WC()->cart ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return WC_EDM_DB::group_for_coupons( WC()->cart->get_applied_coupons() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove every method some group reserved for itself.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user