cart ) { return; } $seen = array(); foreach ( WC()->cart->get_cart() as $item ) { $root = HyperDrop_Drop::root_id( $item['data'] ); if ( isset( $seen[ $root ] ) || ! HyperDrop_Drop::is_gated( $root ) ) { continue; } $seen[ $root ] = true; if ( ! HyperDrop_Drop::shopper_has_access( $root ) ) { $name = HyperDrop_Drop::can_view( $root ) ? get_the_title( $root ) : __( 'An exclusive item', 'hyperdrop' ); /* translators: %s: product name */ wc_add_notice( sprintf( __( '"%s" is an exclusive drop and needs a valid access code.', 'hyperdrop' ), $name ), 'error' ); } } $err = HyperDrop_Limits::cart_error(); if ( $err ) { wc_add_notice( $err, 'error' ); } // Logged-in customers / code owners: tell them early, before they fill in checkout. $customer_email = WC()->customer ? WC()->customer->get_billing_email() : ''; $err = HyperDrop_Limits::orders_error( $customer_email ); if ( $err ) { wc_add_notice( $err, 'error' ); } } /** * Classic checkout: orders-per-customer check with the billing email typed in. */ public static function checkout_validation( $data, $errors ) { $err = HyperDrop_Limits::orders_error( isset( $data['billing_email'] ) ? $data['billing_email'] : '' ); if ( $err ) { $errors->add( 'hyperdrop_limit', $err ); } } /** * Block checkout (Store API): same check once the billing email is on the order. * * @param WC_Order $order */ public static function store_api_validation( $order ) { $err = HyperDrop_Limits::orders_error( $order->get_billing_email() ); if ( $err ) { throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'hyperdrop_limit', esc_html( $err ), 400 ); } } /* ------------------------------------------------------------------ * Orders * ------------------------------------------------------------------ */ public static function line_item_meta( $item, $cart_item_key, $values, $order ) { $root = HyperDrop_Drop::root_id( $values['data'] ); $code_id = 0; $count = 1; if ( HyperDrop_Drop::is_product_gated( $root ) ) { $code_id = HyperDrop_Drop::code_for( $root ); } else { foreach ( HyperDrop_Drop::gated_terms_for_product( $root ) as $t ) { $code_id = HyperDrop_Drop::term_code( $t ); if ( $code_id ) { $count = 0; // Category codes are limited by activations, not orders. break; } } } $scopes = HyperDrop_Limits::scopes_for( $root ); if ( $scopes ) { $item->add_meta_data( '_hyperdrop_scopes', implode( ',', $scopes ), true ); } $row = $code_id ? HyperDrop_Codes::get( $code_id ) : null; if ( $row ) { $item->add_meta_data( '_hyperdrop_code_id', (int) $row->id, true ); $item->add_meta_data( '_hyperdrop_code', $row->code, true ); $item->add_meta_data( '_hyperdrop_count', $count, true ); } } public static function meta_label( $key, $meta ) { return '_hyperdrop_code' === $meta->key ? __( 'Access code', 'hyperdrop' ) : $key; } /** * Count a code as used once per order (reserved at order creation). * * @param int|WC_Order $order */ public static function redeem( $order ) { $order = $order instanceof WC_Order ? $order : wc_get_order( $order ); if ( ! $order || $order->get_meta( '_hyperdrop_redeemed' ) ) { return; } $used = array(); $scopes = array(); foreach ( $order->get_items() as $item ) { foreach ( array_filter( explode( ',', (string) $item->get_meta( '_hyperdrop_scopes' ) ) ) as $sc ) { $scopes[ $sc ] = true; } $code_id = (int) $item->get_meta( '_hyperdrop_code_id' ); if ( ! $code_id ) { continue; } $root = HyperDrop_Drop::root_id( $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id() ); $key = $code_id . ':' . $root; if ( ! isset( $used[ $key ] ) ) { $count = $item->get_meta( '_hyperdrop_count' ); $used[ $key ] = array( $code_id, $root, 0, '' === $count ? true : (bool) $count ); } $used[ $key ][2] += (int) $item->get_quantity(); } if ( ! $used && ! $scopes ) { return; } $who = array( 'email' => strtolower( (string) $order->get_billing_email() ), 'user_id' => (int) $order->get_customer_id(), ); $code_ids = array(); foreach ( $used as $u ) { HyperDrop_Codes::record_redemption( $u[0], $order->get_id(), $u[1], $u[2], $u[3], $who ); $code_ids[] = $u[0]; } // One row per locked scope in the order — this is what "orders per customer" counts. // Also recorded under the email each access code was sent to, so the code owner // can't get around the limit by typing a different billing email. $identities = array( $who ); foreach ( array_unique( $code_ids ) as $cid ) { $row = HyperDrop_Codes::get( $cid ); if ( $row && $row->email && strtolower( $row->email ) !== $who['email'] ) { $identities[] = array( 'email' => strtolower( $row->email ), 'user_id' => 0, ); } } foreach ( array_keys( $scopes ) as $sc ) { foreach ( $identities as $id ) { HyperDrop_Codes::record_scope_order( $sc, $order->get_id(), $id ); } } $order->update_meta_data( '_hyperdrop_redeemed', $code_ids ? implode( ',', array_unique( $code_ids ) ) : 'scope' ); $order->save(); do_action( 'hyperdrop_order_redeemed', $order, $used ); } /** * Give the use back if the order is cancelled or fails. */ public static function release( $order_id ) { global $wpdb; $order = wc_get_order( $order_id ); if ( ! $order || ! $order->get_meta( '_hyperdrop_redeemed' ) ) { return; } $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE order_id = %d', HyperDrop_DB::redemptions_table(), $order_id ) ); foreach ( $rows as $r ) { if ( isset( $r->counted ) && ! (int) $r->counted ) { continue; } $wpdb->query( $wpdb->prepare( 'UPDATE %i SET uses = CASE WHEN uses > 0 THEN uses - 1 ELSE 0 END WHERE id = %d', HyperDrop_DB::codes_table(), $r->code_id ) ); } $wpdb->delete( HyperDrop_DB::redemptions_table(), array( 'order_id' => (int) $order_id ) ); $order->delete_meta_data( '_hyperdrop_redeemed' ); $order->save(); } }