Db update for product lookup table to enable segmenting.
This commit is contained in:
parent
1cb7378f76
commit
4d6ea24e9e
|
@ -395,6 +395,12 @@ class WC_Admin_Api_Init {
|
|||
date_created timestamp DEFAULT '0000-00-00 00:00:00' NOT NULL,
|
||||
product_qty INT UNSIGNED NOT NULL,
|
||||
product_net_revenue double DEFAULT 0 NOT NULL,
|
||||
product_gross_revenue double DEFAULT 0 NOT NULL,
|
||||
price double DEFAULT 0 NOT NULL,
|
||||
coupon_amount double DEFAULT 0 NOT NULL,
|
||||
tax_amount double DEFAULT 0 NOT NULL,
|
||||
shipping_amount double DEFAULT 0 NOT NULL,
|
||||
shipping_tax_amount double DEFAULT 0 NOT NULL,
|
||||
PRIMARY KEY (order_item_id),
|
||||
KEY order_id (order_id),
|
||||
KEY product_id (product_id),
|
||||
|
|
|
@ -21,17 +21,77 @@ function wc_admin_order_product_lookup_entry( $order_id ) {
|
|||
}
|
||||
|
||||
foreach ( $order->get_items() as $order_item ) {
|
||||
// Shipping amount based on woocommerce code in includes/admin/meta-boxes/views/html-order-item(s).php
|
||||
// distributed simply based on number of line items.
|
||||
$order_items = $order->get_item_count();
|
||||
$refunded = $order->get_total_shipping_refunded();
|
||||
if ( $refunded > 0 ) {
|
||||
$total_shipping_amount = $order->get_shipping_total() - $refunded;
|
||||
} else {
|
||||
$total_shipping_amount = $order->get_shipping_total();
|
||||
}
|
||||
$shipping_amount = $total_shipping_amount / $order_items;
|
||||
|
||||
// Shipping amount tax based on woocommerce code in includes/admin/meta-boxes/views/html-order-item(s).php
|
||||
// distribute simply based on number of line items.
|
||||
$shipping_tax_amount = 0;
|
||||
// TODO: if WC is currently not tax enabled, but it was before (or vice versa), would this work correctly?
|
||||
if ( wc_tax_enabled() ) {
|
||||
$order_taxes = $order->get_taxes();
|
||||
$line_items_shipping = $order->get_items( 'shipping' );
|
||||
$total_shipping_tax_amount = 0;
|
||||
foreach ( $line_items_shipping as $item_id => $item ) {
|
||||
$tax_data = $item->get_taxes();
|
||||
if ( $tax_data ) {
|
||||
foreach ( $order_taxes as $tax_item ) {
|
||||
$tax_item_id = $tax_item->get_rate_id();
|
||||
$tax_item_total = isset( $tax_data['total'][ $tax_item_id ] ) ? $tax_data['total'][ $tax_item_id ] : '';
|
||||
$refunded = $order->get_tax_refunded_for_item( $item_id, $tax_item_id, 'shipping' );
|
||||
if ( $refunded ) {
|
||||
$total_shipping_tax_amount += $tax_item_total - $refunded;
|
||||
} else {
|
||||
$total_shipping_tax_amount += $tax_item_total;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$shipping_tax_amount = $total_shipping_tax_amount / $order_items;
|
||||
}
|
||||
|
||||
// Tax amount.
|
||||
// TODO: check if this calculates tax correctly with refunds.
|
||||
$tax_amount = 0;
|
||||
if ( wc_tax_enabled() ) {
|
||||
$order_taxes = $order->get_taxes();
|
||||
$tax_data = $order_item->get_taxes();
|
||||
foreach ( $order_taxes as $tax_item ) {
|
||||
$tax_item_id = $tax_item->get_rate_id();
|
||||
$tax_amount += isset( $tax_data['total'][ $tax_item_id ] ) ? $tax_data['total'][ $tax_item_id ] : 0;
|
||||
}
|
||||
}
|
||||
|
||||
$net_revenue = $order_item->get_subtotal( 'edit' );
|
||||
|
||||
// Coupon calculation based on woocommerce code in includes/admin/meta-boxes/views/html-order-item.php.
|
||||
$coupon_amount = $order_item->get_subtotal( 'edit' ) - $order_item->get_total( 'edit' );
|
||||
|
||||
$wpdb->replace(
|
||||
$wpdb->prefix . 'wc_order_product_lookup',
|
||||
array(
|
||||
'order_item_id' => $order_item->get_id(),
|
||||
'order_id' => $order->get_id(),
|
||||
'product_id' => $order_item->get_product_id( 'edit' ),
|
||||
'variation_id' => $order_item->get_variation_id( 'edit' ),
|
||||
'customer_id' => ( 0 < $order->get_customer_id( 'edit' ) ) ? $order->get_customer_id( 'edit' ) : null,
|
||||
'product_qty' => $order_item->get_quantity( 'edit' ),
|
||||
'product_net_revenue' => $order_item->get_subtotal( 'edit' ),
|
||||
'date_created' => date( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
|
||||
'order_item_id' => $order_item->get_id(),
|
||||
'order_id' => $order->get_id(),
|
||||
'product_id' => $order_item->get_product_id( 'edit' ),
|
||||
'variation_id' => $order_item->get_variation_id( 'edit' ),
|
||||
'customer_id' => ( 0 < $order->get_customer_id( 'edit' ) ) ? $order->get_customer_id( 'edit' ) : null,
|
||||
'product_qty' => $order_item->get_quantity( 'edit' ),
|
||||
'product_net_revenue' => $net_revenue,
|
||||
'date_created' => date( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
|
||||
'price' => $order_item->get_subtotal( 'edit' ) / $order_item->get_quantity( 'edit' ),
|
||||
'coupon_amount' => $coupon_amount,
|
||||
'tax_amount' => $tax_amount,
|
||||
'shipping_amount' => $shipping_amount,
|
||||
'shipping_tax_amount' => $shipping_tax_amount,
|
||||
'product_gross_revenue' => $net_revenue + $tax_amount + $shipping_amount + $shipping_tax_amount,
|
||||
),
|
||||
array(
|
||||
'%d',
|
||||
|
@ -42,6 +102,12 @@ function wc_admin_order_product_lookup_entry( $order_id ) {
|
|||
'%d',
|
||||
'%f',
|
||||
'%s',
|
||||
'%f',
|
||||
'%f',
|
||||
'%f',
|
||||
'%f',
|
||||
'%f',
|
||||
'%f',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue