| @@ -45,24 +45,8 @@ | ||
| 45 | 45 | /** |
| 46 | 46 | * Add each campaign ID as separate meta entry. |
| 47 | 47 | * Using unique=false allows multiple campaign IDs per order. |
| 48 | 48 | */ |
| 49 | - /** | |
| 50 | - * Guard against double execution. | |
| 51 | - * | |
| 52 | - * This callback fires on both woocommerce_thankyou and | |
| 53 | - * woocommerce_payment_complete. Depending on the gateway and page | |
| 54 | - * reloads either can run first, so skip if campaign meta is already | |
| 55 | - * stored to avoid duplicate disco_campaign entries. | |
| 56 | - */ | |
| 57 | - $existing = $order->get_meta( 'disco_campaign', false ); | |
| 58 | - | |
| 59 | - if ( ! empty( $existing ) ) { | |
| 60 | - WC()->session->__unset( 'disco_campaign' ); | |
| 61 | - | |
| 62 | - return; | |
| 63 | - } | |
| 64 | - | |
| 65 | 49 | foreach ( $campaigns as $campaign_id ) { |
| 66 | 50 | $order->add_meta_data( 'disco_campaign', (int) $campaign_id, false ); |
| 67 | 51 | } |
| 68 | 52 | |
| @@ -106,53 +90,5 @@ | ||
| 106 | 90 | WC()->session->__unset( 'disco_campaign' ); |
| 107 | 91 | } |
| 108 | 92 | |
| 109 | 93 | add_action( 'woocommerce_before_calculate_totals', 'disco_reset_campaign_session', 0 ); |
| 110 | -} | |
| 111 | - | |
| 112 | -if ( ! function_exists( 'disco_add_free_shipping_order_meta' ) ) { | |
| 113 | - | |
| 114 | - /** | |
| 115 | - * Persist free-shipping campaign IDs to the order meta during checkout. | |
| 116 | - * | |
| 117 | - * Free shipping is applied through the woocommerce_package_rates filter, | |
| 118 | - * whose results WooCommerce caches per package. Because of that cache, the | |
| 119 | - * filter is not guaranteed to run on the final order-placement recalculation, | |
| 120 | - * so the campaign ID cannot be reliably staged in the WC session like the | |
| 121 | - * product and cart intents are. Instead we evaluate the Shipping intents | |
| 122 | - * fresh here, where the order object and cart are both available, and write | |
| 123 | - * the campaign IDs straight to the order. | |
| 124 | - * | |
| 125 | - * @param \WC_Order $order The order being created. | |
| 126 | - * @return void | |
| 127 | - */ | |
| 128 | - function disco_add_free_shipping_order_meta( $order ) { | |
| 129 | - if ( ! $order instanceof WC_Order ) { | |
| 130 | - return; | |
| 131 | - } | |
| 132 | - | |
| 133 | - $campaign_ids = ( new \Disco\App\Disco )->get_applied_free_shipping_campaign_ids(); | |
| 134 | - | |
| 135 | - if ( empty( $campaign_ids ) ) { | |
| 136 | - return; | |
| 137 | - } | |
| 138 | - | |
| 139 | - // Avoid duplicating IDs already staged from product/cart intents. | |
| 140 | - $existing = array_map( 'intval', $order->get_meta( 'disco_campaign', false ) ? wp_list_pluck( $order->get_meta( 'disco_campaign', false ), 'value' ) : array() ); | |
| 141 | - | |
| 142 | - foreach ( $campaign_ids as $campaign_id ) { | |
| 143 | - if ( in_array( (int) $campaign_id, $existing, true ) ) { | |
| 144 | - continue; | |
| 145 | - } | |
| 146 | - | |
| 147 | - $order->add_meta_data( 'disco_campaign', (int) $campaign_id, false ); | |
| 148 | - } | |
| 149 | - } | |
| 150 | - | |
| 151 | - // Classic (shortcode) checkout: order is saved by WC after this action. | |
| 152 | - add_action( 'woocommerce_checkout_create_order', 'disco_add_free_shipping_order_meta', 20 ); | |
| 153 | - | |
| 154 | - // Block / Store API checkout: WC_Checkout::create_order() does not run, so | |
| 155 | - // the action above never fires. This Store API hook also passes the WC_Order | |
| 156 | - // (as its first arg) before the order is persisted. | |
| 157 | - add_action( 'woocommerce_store_api_checkout_update_order_meta', 'disco_add_free_shipping_order_meta', 20 ); | |
| 158 | 94 | } |