Automattic\WooCommerce\Admin\API\Reports\Coupons
DataStore::sync_order_coupons()
Create or update an an entry in the wc_order_coupon_lookup table for an order.
Метод класса: DataStore{}
Хуки из метода
Возвращает
int|true|false
. Returns -1 if order won't be processed, or a boolean indicating processing success.
Использование
$result = DataStore::sync_order_coupons( $order_id );
- $order_id(int) (обязательный)
- Order ID.
Список изменений
С версии 3.5.0 | Введена. |
Код DataStore::sync_order_coupons() DataStore::sync order coupons WC 9.7.1
public static function sync_order_coupons( $order_id ) { global $wpdb; $order = wc_get_order( $order_id ); if ( ! $order ) { return -1; } // Refunds don't affect coupon stats so return successfully if one is called here. if ( 'shop_order_refund' === $order->get_type() ) { return true; } $table_name = self::get_db_table_name(); $existing_items = $wpdb->get_col( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "SELECT coupon_id FROM {$table_name} WHERE order_id = %d", $order_id ) ); $existing_items = array_flip( $existing_items ); $coupon_items = $order->get_items( 'coupon' ); $coupon_items_count = count( $coupon_items ); $num_updated = 0; $num_deleted = 0; foreach ( $coupon_items as $coupon_item ) { $coupon_id = self::get_coupon_id( $coupon_item ); unset( $existing_items[ $coupon_id ] ); if ( ! $coupon_id ) { // Insert a unique, but obviously invalid ID for this deleted coupon. $num_deleted++; $coupon_id = -1 * $num_deleted; } $result = $wpdb->replace( self::get_db_table_name(), array( 'order_id' => $order_id, 'coupon_id' => $coupon_id, 'discount_amount' => $coupon_item->get_discount(), 'date_created' => $order->get_date_created( 'edit' )->date( TimeInterval::$sql_datetime_format ), ), array( '%d', '%d', '%f', '%s', ) ); /** * Fires when coupon's reports are updated. * * @param int $coupon_id Coupon ID. * @param int $order_id Order ID. */ do_action( 'woocommerce_analytics_update_coupon', $coupon_id, $order_id ); // Sum the rows affected. Using REPLACE can affect 2 rows if the row already exists. $num_updated += 2 === intval( $result ) ? 1 : intval( $result ); } if ( ! empty( $existing_items ) ) { $existing_items = array_flip( $existing_items ); $format = array_fill( 0, count( $existing_items ), '%d' ); $format = implode( ',', $format ); array_unshift( $existing_items, $order_id ); $wpdb->query( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "DELETE FROM {$table_name} WHERE order_id = %d AND coupon_id in ({$format})", $existing_items ) ); } return ( $coupon_items_count === $num_updated ); }