-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ad3752
commit 815b5a7
Showing
24 changed files
with
2,317 additions
and
623 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<?php return array('dependencies' => array('wc-currency', 'wp-hooks', 'wp-i18n'), 'version' => 'c8a835c8dd767411a52fc60d79167167'); | ||
<?php return array('dependencies' => array('wc-currency', 'wp-hooks', 'wp-i18n'), 'version' => '788d1a21f794f9834f9046d4d5b938c9'); |
Large diffs are not rendered by default.
Oops, something went wrong.
185 changes: 185 additions & 0 deletions
185
includes/analytics/class-alg-wc-cog-analytics-products.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<?php | ||
/** | ||
* Cost of Goods for WooCommerce - Analytics - Products. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* @author WPFactory | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | ||
|
||
if ( ! class_exists( 'Alg_WC_Cost_of_Goods_Analytics_Products' ) ) : | ||
|
||
class Alg_WC_Cost_of_Goods_Analytics_Products { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
*/ | ||
function __construct() { | ||
// Script localization info. | ||
add_filter( 'alg_wc_cog_analytics_localization_info', array( $this, 'add_analytics_localization_info' ) ); | ||
// Costs | ||
add_filter( 'woocommerce_analytics_clauses_join_products_subquery', array( $this, 'add_costs_to_join_products' ) ); | ||
add_filter( 'woocommerce_analytics_clauses_join_products_stats_total', array( $this, 'add_costs_to_join_products' ) ); | ||
add_filter( 'woocommerce_analytics_clauses_join_products_stats_interval', array( $this, 'add_costs_to_join_products' ) ); | ||
add_filter( 'woocommerce_analytics_clauses_select_products', array( $this, 'add_costs_to_select_products' ) ); | ||
add_filter( 'woocommerce_analytics_clauses_select_products_subquery', array( $this, 'add_costs_to_select_products_subquery' ) ); | ||
// Costs total | ||
add_filter( 'woocommerce_analytics_clauses_select_products_stats_total', array( $this, 'add_costs_total_to_select_products_stats_total' ) ); | ||
add_filter( 'woocommerce_analytics_clauses_select_products_stats_interval', array( $this, 'add_costs_total_to_select_products_stats_total' ) ); | ||
// Profit | ||
add_filter( 'woocommerce_analytics_clauses_select_products', array( $this, 'add_profit_to_select_products' ) ); | ||
add_filter( 'woocommerce_analytics_clauses_select_products_subquery', array( $this, 'add_profit_to_select_products_subquery' ) ); | ||
// Profit total | ||
add_filter( 'woocommerce_analytics_clauses_select_products_stats_total', array( $this, 'add_profit_total_to_select_products_stats_total' ) ); | ||
add_filter( 'woocommerce_analytics_clauses_select_products_stats_interval', array( $this, 'add_profit_total_to_select_products_stats_total' ) ); | ||
} | ||
|
||
/** | ||
* add_costs_total_to_select_products_stats_total. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $clauses | ||
* | ||
* @return array | ||
*/ | ||
function add_costs_total_to_select_products_stats_total( $clauses ) { | ||
if ( 'yes' === get_option( 'alg_wc_cog_cost_and_profit_totals_on_products_tab', 'no' ) ) { | ||
$clauses[] = ', SUM(alg_cog_oimc.meta_value) AS costs_total'; | ||
} | ||
return $clauses; | ||
} | ||
|
||
/** | ||
* add_profit_total_to_select_products_stats_total. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $clauses | ||
* | ||
* @return array | ||
*/ | ||
function add_profit_total_to_select_products_stats_total( $clauses ) { | ||
global $wpdb; | ||
if ( 'yes' === get_option( 'alg_wc_cog_cost_and_profit_totals_on_products_tab', 'no' ) ) { | ||
$clauses[] = ", SUM(IFNULL({$wpdb->prefix}wc_order_product_lookup.product_net_revenue - alg_cog_oimc.meta_value, 0)) AS profit_total"; | ||
} | ||
return $clauses; | ||
} | ||
|
||
/** | ||
* add_profit_to_select_products. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $clauses | ||
* | ||
* @return array | ||
*/ | ||
function add_profit_to_select_products( $clauses ) { | ||
if ( 'yes' === get_option( 'alg_wc_cog_cost_and_profit_column_on_products_tab', 'no' ) ) { | ||
$clauses[] = ', profit'; | ||
} | ||
return $clauses; | ||
} | ||
|
||
/** | ||
* add_profit_to_select_products_subquery. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $clauses | ||
* | ||
* @return array | ||
*/ | ||
function add_profit_to_select_products_subquery( $clauses ) { | ||
if ( 'yes' === get_option( 'alg_wc_cog_cost_and_profit_column_on_products_tab', 'no' ) ) { | ||
global $wpdb; | ||
$clauses[] = ", SUM(IFNULL({$wpdb->prefix}wc_order_product_lookup.product_net_revenue - alg_cog_oimc.meta_value, 0)) AS profit"; | ||
} | ||
return $clauses; | ||
} | ||
|
||
/** | ||
* add_costs_to_join_products. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $clauses | ||
* | ||
* @return array | ||
*/ | ||
function add_costs_to_join_products( $clauses ) { | ||
if ( 'yes' === get_option( 'alg_wc_cog_cost_and_profit_column_on_products_tab', 'no' ) ) { | ||
global $wpdb; | ||
$clauses[] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta alg_cog_oimc ON {$wpdb->prefix}wc_order_product_lookup.order_item_id = alg_cog_oimc.order_item_id AND alg_cog_oimc.meta_key = '_alg_wc_cog_item_cost'"; | ||
} | ||
return $clauses; | ||
} | ||
|
||
/** | ||
* add_costs_to_select_products_subquery. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $clauses | ||
* | ||
* @return array | ||
*/ | ||
function add_costs_to_select_products_subquery( $clauses ) { | ||
if ( 'yes' === get_option( 'alg_wc_cog_cost_and_profit_column_on_products_tab', 'no' ) ) { | ||
$clauses[] = ', SUM(IFNULL(alg_cog_oimc.meta_value, 0)) AS cost'; | ||
} | ||
return $clauses; | ||
} | ||
|
||
/** | ||
* add_costs_to_select_products. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $clauses | ||
* | ||
* @return array | ||
*/ | ||
function add_costs_to_select_products( $clauses ) { | ||
if ( 'yes' === get_option( 'alg_wc_cog_cost_and_profit_column_on_products_tab', 'no' ) ) { | ||
$clauses[] = ', cost'; | ||
} | ||
return $clauses; | ||
} | ||
|
||
/** | ||
* add_analytics_localization_info. | ||
* | ||
* @version 2.5.1 | ||
* @since 2.5.1 | ||
* | ||
* @param $info | ||
* | ||
* @return mixed | ||
*/ | ||
function add_analytics_localization_info( $info ) { | ||
$info['cost_and_profit_totals_enabled_on_products'] = 'yes' === get_option( 'alg_wc_cog_cost_and_profit_totals_on_products_tab', 'no' ); | ||
$info['cost_and_profit_columns_enabled_on_products'] = 'yes' === get_option( 'alg_wc_cog_cost_and_profit_column_on_products_tab', 'no' ); | ||
return $info; | ||
} | ||
|
||
} | ||
|
||
endif; | ||
|
||
return new Alg_WC_Cost_of_Goods_Analytics_Products(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Cost of Goods for WooCommerce - Analytics > Products (WooCommerce Admin) Report. | ||
* | ||
*/ | ||
|
||
import {addFilter} from '@wordpress/hooks'; | ||
import {__} from '@wordpress/i18n'; | ||
import CurrencyFactory from '@woocommerce/currency'; | ||
|
||
const storeCurrency = CurrencyFactory(wcSettings.currency); | ||
|
||
let products = { | ||
init: function () { | ||
// Reports table | ||
addFilter( | ||
'woocommerce_admin_report_table', | ||
'cost-of-goods-for-woocommerce', | ||
(reportTableData) => { | ||
if ( | ||
reportTableData.endpoint !== 'products' || | ||
!reportTableData.items || | ||
!reportTableData.items.data || | ||
!reportTableData.items.data.length || | ||
!alg_wc_cog_analytics_obj.cost_and_profit_columns_enabled_on_products | ||
) { | ||
return reportTableData; | ||
} | ||
const newHeaders = [ | ||
...reportTableData.headers, | ||
{ | ||
label: __('Cost', 'cost-of-goods-for-woocommerce'), | ||
key: 'cost', | ||
isNumeric: true, | ||
//isSortable: true, | ||
}, | ||
{ | ||
label: __('Profit', 'cost-of-goods-for-woocommerce'), | ||
key: 'profit', | ||
isNumeric: true, | ||
//isSortable: true, | ||
}, | ||
]; | ||
const newRows = reportTableData.rows.map((row, index) => { | ||
const item = reportTableData.items.data[index]; | ||
console.log(item); | ||
const newRow = [ | ||
...row, | ||
{ | ||
display: storeCurrency.formatAmount(item.cost), | ||
value: item.cost, | ||
type: 'currency' | ||
}, | ||
{ | ||
display: storeCurrency.formatAmount(item.profit), | ||
value: item.profit, | ||
type: 'currency' | ||
}, | ||
]; | ||
return newRow; | ||
}); | ||
reportTableData.rows = newRows; | ||
reportTableData.headers = newHeaders; | ||
|
||
return reportTableData; | ||
} | ||
); | ||
// Charts | ||
/** | ||
* @see https://github.com/woocommerce/woocommerce-admin/blob/main/client/analytics/report/orders/config.js#L50-L62 | ||
*/ | ||
addFilter( | ||
'woocommerce_admin_products_report_charts', | ||
'cost-of-goods-for-woocommerce', | ||
(charts) => { | ||
if (alg_wc_cog_analytics_obj.cost_and_profit_totals_enabled_on_products) { | ||
charts = [...charts, | ||
{ | ||
key: 'costs_total', | ||
label: __('Costs total', 'cost-of-goods-for-woocommerce'), | ||
type: 'currency' | ||
}, | ||
{ | ||
key: 'profit_total', | ||
label: __('Profit total', 'cost-of-goods-for-woocommerce'), | ||
type: 'currency' | ||
}]; | ||
} | ||
return charts; | ||
} | ||
); | ||
} | ||
}; | ||
export default products; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.