| @@ -70,8 +70,13 @@ | ||
| 70 | 70 | $order->save(); |
| 71 | 71 | |
| 72 | 72 | WC()->session->__unset( 'disco_campaign' ); |
| 73 | 73 | |
| 74 | + // Drop the cached usage counts for the campaigns this order just used. | |
| 75 | + foreach ( $campaigns as $campaign_id ) { | |
| 76 | + \Disco\App\Features\UserLimit::flush_cache( (int) $campaign_id ); | |
| 77 | + } | |
| 78 | + | |
| 74 | 79 | // Clear price cache so user limits are re-evaluated |
| 75 | 80 | if ( !function_exists( 'disco_clear_price_cache' ) ) { |
| 76 | 81 | return; |
| 77 | 82 | } |
| @@ -80,8 +85,83 @@ | ||
| 80 | 85 | } |
| 81 | 86 | |
| 82 | 87 | add_action( 'woocommerce_thankyou', 'disco_add_order_meta', PHP_INT_MAX ); |
| 83 | 88 | add_action( 'woocommerce_payment_complete', 'disco_add_order_meta', PHP_INT_MAX ); |
| 89 | +} | |
| 90 | + | |
| 91 | +if ( ! function_exists( 'disco_flush_campaign_usage_cache_on_status_change' ) ) { | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Invalidate cached campaign usage counts when an order changes status. | |
| 95 | + * | |
| 96 | + * The usage count only counts orders in processing / on-hold / completed, so | |
| 97 | + * any status transition can change it. Flushing here keeps the cached count | |
| 98 | + * correct without querying on every cart or fragment-refresh request. | |
| 99 | + * | |
| 100 | + * @param int $order_id Order ID. | |
| 101 | + * @return void | |
| 102 | + */ | |
| 103 | + function disco_flush_campaign_usage_cache_on_status_change( $order_id ) { | |
| 104 | + $order = wc_get_order( $order_id ); | |
| 105 | + | |
| 106 | + if ( ! $order instanceof WC_Order ) { | |
| 107 | + return; | |
| 108 | + } | |
| 109 | + | |
| 110 | + $meta = $order->get_meta( 'disco_campaign', false ); | |
| 111 | + | |
| 112 | + if ( empty( $meta ) || ! is_array( $meta ) ) { | |
| 113 | + return; | |
| 114 | + } | |
| 115 | + | |
| 116 | + foreach ( wp_list_pluck( $meta, 'value' ) as $campaign_id ) { | |
| 117 | + \Disco\App\Features\UserLimit::flush_cache( (int) $campaign_id ); | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 121 | + add_action( 'woocommerce_order_status_changed', 'disco_flush_campaign_usage_cache_on_status_change', 10, 1 ); | |
| 122 | + add_action( 'woocommerce_trash_order', 'disco_flush_campaign_usage_cache_on_status_change', 10, 1 ); | |
| 123 | + add_action( 'woocommerce_untrash_order', 'disco_flush_campaign_usage_cache_on_status_change', 10, 1 ); | |
| 124 | + add_action( 'woocommerce_before_delete_order', 'disco_flush_campaign_usage_cache_on_status_change', 10, 1 ); | |
| 125 | + add_action( 'woocommerce_delete_order', 'disco_flush_campaign_usage_cache_on_status_change', 10, 1 ); | |
| 126 | +} | |
| 127 | + | |
| 128 | +if ( ! function_exists( 'disco_flush_campaign_usage_cache_on_meta_write' ) ) { | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * Invalidate cached campaign usage counts when disco_campaign meta is written | |
| 132 | + * outside of this plugin. | |
| 133 | + * | |
| 134 | + * Imports, migrations and third-party code write order meta directly rather | |
| 135 | + * than going through disco_add_order_meta(), so without this the cached count | |
| 136 | + * would stay stale until its TTL expired. | |
| 137 | + * | |
| 138 | + * @param int|array $meta_id Meta ID (array on delete). | |
| 139 | + * @param int $object_id Order ID. | |
| 140 | + * @param string $meta_key Meta key. | |
| 141 | + * @param mixed $meta_value Meta value. | |
| 142 | + * @return void | |
| 143 | + */ | |
| 144 | + // $meta_id and $object_id are unused but required: the meta hooks pass their | |
| 145 | + // arguments positionally, so $meta_key and $meta_value cannot be reached | |
| 146 | + // without declaring them. | |
| 147 | + function disco_flush_campaign_usage_cache_on_meta_write( $meta_id, $object_id, $meta_key, $meta_value ) { //phpcs:ignore | |
| 148 | + if ( 'disco_campaign' !== $meta_key ) { | |
| 149 | + return; | |
| 150 | + } | |
| 151 | + | |
| 152 | + \Disco\App\Features\UserLimit::flush_cache( (int) $meta_value ); | |
| 153 | + } | |
| 154 | + | |
| 155 | + // Legacy (post table) orders. | |
| 156 | + add_action( 'added_post_meta', 'disco_flush_campaign_usage_cache_on_meta_write', 10, 4 ); | |
| 157 | + add_action( 'updated_post_meta', 'disco_flush_campaign_usage_cache_on_meta_write', 10, 4 ); | |
| 158 | + add_action( 'deleted_post_meta', 'disco_flush_campaign_usage_cache_on_meta_write', 10, 4 ); | |
| 159 | + | |
| 160 | + // HPOS orders. | |
| 161 | + add_action( 'added_wc_order_meta', 'disco_flush_campaign_usage_cache_on_meta_write', 10, 4 ); | |
| 162 | + add_action( 'updated_wc_order_meta', 'disco_flush_campaign_usage_cache_on_meta_write', 10, 4 ); | |
| 163 | + add_action( 'deleted_wc_order_meta', 'disco_flush_campaign_usage_cache_on_meta_write', 10, 4 ); | |
| 84 | 164 | } |
| 85 | 165 | |
| 86 | 166 | if ( ! function_exists( 'disco_reset_campaign_session' ) ) { |
| 87 | 167 | |