Added code from feature/couponordertable.

This commit is contained in:
Peter Fabian 2018-09-18 13:29:58 +02:00
parent aaef322e00
commit b8732e157e
2 changed files with 46 additions and 0 deletions

View File

@ -204,6 +204,7 @@ class WC_Admin_Api_Init {
"{$wpdb->prefix}wc_admin_order_stats",
"{$wpdb->prefix}wc_admin_order_product_lookup",
"{$wpdb->prefix}wc_order_tax_lookup",
"{$wpdb->prefix}wc_order_coupon_lookup",
)
);
}
@ -253,6 +254,15 @@ class WC_Admin_Api_Init {
KEY order_id (order_id),
KEY tax_rate_id (tax_rate_id),
KEY date_created (date_created)
) $collate;
CREATE TABLE {$wpdb->prefix}wc_order_coupon_lookup (
order_id BIGINT UNSIGNED NOT NULL,
coupon_id BIGINT UNSIGNED NOT NULL,
date_created timestamp DEFAULT '0000-00-00 00:00:00' NOT NULL,
coupon_gross_discount double DEFAULT 0 NOT NULL,
KEY order_id (order_id),
KEY coupon_id (coupon_id),
KEY date_created (date_created)
) $collate;";
return $tables;

View File

@ -77,3 +77,39 @@ function wc_order_tax_lookup_entry( $order_id ) {
}
}
add_action( 'save_post', 'wc_order_tax_lookup_entry', 10, 1 );
/**
* Make an entry in the wc_order_coupon_lookup table for an order.
*
* @since 3.5.0
* @param int $order_id Order ID.
* @return void
*/
function wc_order_coupon_lookup_entry( $order_id ) {
global $wpdb;
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$coupon_items = $order->get_items( 'coupon' );
foreach ( $coupon_items as $coupon_item ) {
$wpdb->replace(
$wpdb->prefix . 'wc_order_coupon_lookup',
array(
'order_id' => $order_id,
'coupon_id' => $coupon_item->get_id(),
'coupon_gross_discount' => $coupon_item->get_discount(),
'date_created' => date( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
),
array(
'%d',
'%d',
'%f',
'%s',
)
);
}
}
add_action( 'save_post', 'wc_order_coupon_lookup_entry', 10, 1 );