Skip to content

Commit

Permalink
v2.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-sg-pacheco committed Feb 10, 2022
1 parent 2ad3752 commit 815b5a7
Show file tree
Hide file tree
Showing 24 changed files with 2,317 additions and 623 deletions.
5 changes: 3 additions & 2 deletions cost-of-goods-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Cost of Goods for WooCommerce
Plugin URI: https://wpfactory.com/item/cost-of-goods-for-woocommerce/
Description: Save product purchase costs (cost of goods) in WooCommerce. Beautifully.
Version: 2.5.0
Version: 2.5.1
Author: WPFactory
Author URI: https://wpfactory.com
Text Domain: cost-of-goods-for-woocommerce
Expand Down Expand Up @@ -63,7 +63,8 @@ final class Alg_WC_Cost_of_Goods {
* @var string
* @since 1.0.0
*/
public $version = '2.5.0';

public $version = '2.5.1';

/**
* @var Alg_WC_Cost_of_Goods The single instance of the class
Expand Down
16 changes: 15 additions & 1 deletion includes/alg-wc-cog-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Functions
*
* @version 2.4.2
* @version 2.5.1
* @since 1.4.0
* @author WPFactory
*/
Expand Down Expand Up @@ -199,3 +199,17 @@ function alg_wc_cog_array_to_string( $arr, $args = array() ) {
return implode( $args['glue'], $transformed_arr );
}
}

if ( ! function_exists( 'alg_wc_cog_get_blocked_options_message' ) ) {
/**
* alg_wc_cog_get_blocked_options_message.
*
* @version 2.5.1
* @since 2.5.1
*
* @return string
*/
function alg_wc_cog_get_blocked_options_message() {
return sprintf( __( 'Disabled options can be unlocked using <a href="%s" target="_blank"><strong>%s</strong></a>', 'cost-of-goods-for-woocommerce' ), 'https://wpfactory.com/item/cost-of-goods-for-woocommerce/', __( 'Cost of Goods for WooCommerce Pro', 'cost-of-goods-for-woocommerce' ) );
}
}
2 changes: 1 addition & 1 deletion includes/analytics/build/index.asset.php
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');
675 changes: 674 additions & 1 deletion includes/analytics/build/index.js

Large diffs are not rendered by default.

185 changes: 185 additions & 0 deletions includes/analytics/class-alg-wc-cog-analytics-products.php
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();
7 changes: 5 additions & 2 deletions includes/analytics/class-alg-wc-cog-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Analytics Class.
*
* @version 2.4.8
* @version 2.5.1
* @since 1.7.0
* @author WPFactory
*/
Expand All @@ -16,7 +16,7 @@ class Alg_WC_Cost_of_Goods_Analytics {
/**
* Constructor.
*
* @version 2.4.8
* @version 2.5.1
* @since 1.7.0
*
*/
Expand All @@ -31,6 +31,9 @@ function __construct() {

// Analytics > Stock.
require_once('class-alg-wc-cog-analytics-stock.php');

// Analytics > Products.
require_once('class-alg-wc-cog-analytics-products.php');
}

/**
Expand Down
8 changes: 6 additions & 2 deletions includes/analytics/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Analytics.
*
* @version 2.4.5
* @version 2.5.1
* @since 2.4.5
* @author WPFactory
*/
Expand All @@ -16,4 +16,8 @@ revenue.init();

// Stock.
import stock from './modules/stock';
stock.init();
stock.init();

// Products.
import products from './modules/products';
products.init();
94 changes: 94 additions & 0 deletions includes/analytics/src/modules/products.js
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;

4 changes: 1 addition & 3 deletions includes/analytics/src/modules/revenue.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* Cost of Goods for WooCommerce - Analytics > Orders (WooCommerce Admin) Report.
*
* @see https://github.com/woocommerce/woocommerce-admin/blob/main/client/analytics/report/orders/config.js#L50-L62
* Cost of Goods for WooCommerce - Analytics > Revenue (WooCommerce Admin) Report.
*
*/

Expand Down
2 changes: 0 additions & 2 deletions includes/analytics/src/modules/stock.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* Cost of Goods for WooCommerce - Analytics > Stock (WooCommerce Admin) Report.
*
* @see https://github.com/woocommerce/woocommerce-admin/blob/main/client/analytics/report/orders/config.js#L50-L62
*
*/

import {addFilter} from '@wordpress/hooks';
Expand Down
Loading

0 comments on commit 815b5a7

Please sign in to comment.