Skip to content

Commit

Permalink
v3.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-sg-pacheco committed Jul 28, 2023
1 parent 73bc8b8 commit 29136fa
Show file tree
Hide file tree
Showing 7 changed files with 1,327 additions and 1,371 deletions.
25 changes: 21 additions & 4 deletions cost-of-goods-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
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: 3.0.1
Version: 3.0.2
Author: WPFactory
Author URI: https://wpfactory.com
Text Domain: cost-of-goods-for-woocommerce
Domain Path: /langs
Copyright: © 2023 WPFactory
WC tested up to: 7.8
WC tested up to: 7.9
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
Expand Down Expand Up @@ -72,7 +72,7 @@ final class Alg_WC_Cost_of_Goods {
* @var string
* @since 1.0.0
*/
public $version = '3.0.1';
public $version = '3.0.2';

/**
* @var Alg_WC_Cost_of_Goods The single instance of the class
Expand Down Expand Up @@ -109,13 +109,16 @@ public static function instance() {
/**
* Initializes.
*
* @version 2.8.1
* @version 3.0.2
* @since 2.8.1
*/
function init(){
// Localization
add_action( 'init', array( $this, 'localize' ) );

// Declare compatibility with custom order tables for WooCommerce
add_action( 'before_woocommerce_init', array( $this, 'declare_compatibility_with_hpos' ) );

// Pro
if ( 'cost-of-goods-for-woocommerce-pro.php' === basename( __FILE__ ) ) {
$this->pro = require_once( 'includes/pro/class-alg-wc-cog-pro.php' );
Expand Down Expand Up @@ -161,6 +164,20 @@ function localize() {
load_plugin_textdomain( 'cost-of-goods-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/langs/' );
}

/**
* declare_compatibility_with_hpos.
*
* @version 3.0.2
* @since 3.0.2
*
* @see https://github.com/woocommerce/woocommerce/wiki/High-Performance-Order-Storage-Upgrade-Recipe-Book#declaring-extension-incompatibility
*/
function declare_compatibility_with_hpos() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}

/**
* Include required core files used in admin and on the frontend.
*
Expand Down
46 changes: 33 additions & 13 deletions includes/analytics/class-alg-wc-cog-analytics-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
/**
* Cost of Goods for WooCommerce - Analytics - Orders.
*
* @version 3.0.0
* @version 3.0.2
* @since 2.4.5
* @author WPFactory
*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

use Automattic\WooCommerce\Utilities\OrderUtil;

if ( ! class_exists( 'Alg_WC_Cost_of_Goods_Analytics_Orders' ) ) :

class Alg_WC_Cost_of_Goods_Analytics_Orders {
Expand Down Expand Up @@ -244,7 +246,7 @@ function add_costs_select_orders_subquery( $clauses ) {
/**
* add_costs_join_orders.
*
* @version 2.9.8
* @version 3.0.2
* @since 2.4.1
*
* @param $clauses
Expand All @@ -253,17 +255,30 @@ function add_costs_select_orders_subquery( $clauses ) {
*/
function add_costs_join_orders( $clauses ) {
global $wpdb;
$clauses[] = "LEFT JOIN {$wpdb->postmeta} order_cost_postmeta ON {$wpdb->prefix}wc_order_stats.order_id = order_cost_postmeta.post_id AND order_cost_postmeta.meta_key = '_alg_wc_cog_order_cost'";
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta order_cost_postmeta ON {$wpdb->prefix}wc_order_stats.order_id = order_cost_postmeta.order_id AND order_cost_postmeta.meta_key = '_alg_wc_cog_order_cost'";
} else {
$clauses[] = "LEFT JOIN {$wpdb->postmeta} order_cost_postmeta ON {$wpdb->prefix}wc_order_stats.order_id = order_cost_postmeta.post_id AND order_cost_postmeta.meta_key = '_alg_wc_cog_order_cost'";
}

if ( 'yes' === get_option( 'alg_wc_cog_analytics_orders_individual_costs', 'no' ) ) {
$clauses[] = "LEFT JOIN {$wpdb->postmeta} items_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = items_cost_pm.post_id AND items_cost_pm.meta_key = '_alg_wc_cog_order_items_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} shipping_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = shipping_cost_pm.post_id AND shipping_cost_pm.meta_key = '_alg_wc_cog_order_shipping_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} gateway_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = gateway_cost_pm.post_id AND gateway_cost_pm.meta_key = '_alg_wc_cog_order_gateway_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} extra_cost_per_orders_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_per_orders_pm.post_id AND extra_cost_per_orders_pm.meta_key = '_alg_wc_cog_order_extra_cost_per_order'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} extra_cost_all_orders_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_all_orders_pm.post_id AND extra_cost_all_orders_pm.meta_key = '_alg_wc_cog_order_extra_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} extra_cost_from_meta_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_from_meta_pm.post_id AND extra_cost_from_meta_pm.meta_key = '_alg_wc_cog_order_extra_cost_from_meta'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} shipping_classes_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = shipping_classes_cost_pm.post_id AND shipping_classes_cost_pm.meta_key = '_alg_wc_cog_order_shipping_classes_cost'";

if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta items_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = items_cost_pm.order_id AND items_cost_pm.meta_key = '_alg_wc_cog_order_items_cost'";
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta shipping_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = shipping_cost_pm.order_id AND shipping_cost_pm.meta_key = '_alg_wc_cog_order_shipping_cost'";
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta gateway_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = gateway_cost_pm.order_id AND gateway_cost_pm.meta_key = '_alg_wc_cog_order_gateway_cost'";
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta extra_cost_per_orders_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_per_orders_pm.order_id AND extra_cost_per_orders_pm.meta_key = '_alg_wc_cog_order_extra_cost_per_order'";
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta extra_cost_all_orders_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_all_orders_pm.order_id AND extra_cost_all_orders_pm.meta_key = '_alg_wc_cog_order_extra_cost'";
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta extra_cost_from_meta_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_from_meta_pm.order_id AND extra_cost_from_meta_pm.meta_key = '_alg_wc_cog_order_extra_cost_from_meta'";
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta shipping_classes_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = shipping_classes_cost_pm.order_id AND shipping_classes_cost_pm.meta_key = '_alg_wc_cog_order_shipping_classes_cost'";
} else {
$clauses[] = "LEFT JOIN {$wpdb->postmeta} items_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = items_cost_pm.post_id AND items_cost_pm.meta_key = '_alg_wc_cog_order_items_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} shipping_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = shipping_cost_pm.post_id AND shipping_cost_pm.meta_key = '_alg_wc_cog_order_shipping_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} gateway_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = gateway_cost_pm.post_id AND gateway_cost_pm.meta_key = '_alg_wc_cog_order_gateway_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} extra_cost_per_orders_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_per_orders_pm.post_id AND extra_cost_per_orders_pm.meta_key = '_alg_wc_cog_order_extra_cost_per_order'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} extra_cost_all_orders_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_all_orders_pm.post_id AND extra_cost_all_orders_pm.meta_key = '_alg_wc_cog_order_extra_cost'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} extra_cost_from_meta_pm ON {$wpdb->prefix}wc_order_stats.order_id = extra_cost_from_meta_pm.post_id AND extra_cost_from_meta_pm.meta_key = '_alg_wc_cog_order_extra_cost_from_meta'";
$clauses[] = "LEFT JOIN {$wpdb->postmeta} shipping_classes_cost_pm ON {$wpdb->prefix}wc_order_stats.order_id = shipping_classes_cost_pm.post_id AND shipping_classes_cost_pm.meta_key = '_alg_wc_cog_order_shipping_classes_cost'";
}
}
// If we need to get something fron the options database
//$clauses[] = "JOIN {$wpdb->options} wpo ON option_name LIKE '%alg_wc_cog_currencies_rates%'";
Expand Down Expand Up @@ -330,7 +345,7 @@ function add_profit_select_orders_subquery( $clauses ) {
/**
* add_profit_join_orders.
*
* @version 2.4.1
* @version 3.0.2
* @since 2.4.1
*
* @param $clauses
Expand All @@ -339,7 +354,12 @@ function add_profit_select_orders_subquery( $clauses ) {
*/
function add_profit_join_orders( $clauses ) {
global $wpdb;
$clauses[] = "LEFT JOIN {$wpdb->postmeta} order_profit_postmeta ON {$wpdb->prefix}wc_order_stats.order_id = order_profit_postmeta.post_id AND order_profit_postmeta.meta_key = '_alg_wc_cog_order_profit'";
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$clauses[] = "LEFT JOIN {$wpdb->prefix}wc_orders_meta order_profit_postmeta ON {$wpdb->prefix}wc_order_stats.order_id = order_profit_postmeta.order_id AND order_profit_postmeta.meta_key = '_alg_wc_cog_order_profit'";
} else {
$clauses[] = "LEFT JOIN {$wpdb->postmeta} order_profit_postmeta ON {$wpdb->prefix}wc_order_stats.order_id = order_profit_postmeta.post_id AND order_profit_postmeta.meta_key = '_alg_wc_cog_order_profit'";
}

return $clauses;
}

Expand Down
45 changes: 26 additions & 19 deletions includes/class-alg-wc-cog-orders-meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Orders Meta Boxes Class.
*
* @version 2.9.0
* @version 3.0.2
* @since 2.2.0
* @author WPFactory
*/
Expand Down Expand Up @@ -101,42 +101,45 @@ function save_order_cost_manually( $order_cost ) {
/**
* add_order_meta_box.
*
* @version 2.3.4
* @version 3.0.2
* @since 1.4.0
*/
function add_order_meta_box() {
if ( ! apply_filters( 'alg_wc_cog_create_order_meta_box_validation', true ) ) {
return;
}
$screen = alg_wc_cog()->core->orders->get_shop_order_screen_id();
if ( alg_wc_cog()->core->orders->is_order_meta_box ) {
add_meta_box( 'alg-wc-cog',
__( 'Cost of Goods', 'cost-of-goods-for-woocommerce' ),
array( $this, 'render_order_meta_box' ),
'shop_order',
'side'
$screen,
'side',
'high'
);
}
}

/**
* render_order_meta_box.
*
* @version 2.9.0
* @version 3.0.2
* @since 1.4.0
* @todo [maybe] order total
*/
function render_order_meta_box( $post ) {
$order_id = get_the_ID();
$cost = get_post_meta( $order_id, '_alg_wc_cog_order_' . 'cost', true );
$handling_fee = get_post_meta( $order_id, '_alg_wc_cog_order_' . 'handling_fee', true );
$profit = get_post_meta( $order_id, '_alg_wc_cog_order_' . 'profit', true );
$profit_percent = get_post_meta( $order_id, '_alg_wc_cog_order_' . 'profit_percent', true );
$profit_margin = get_post_meta( $order_id, '_alg_wc_cog_order_' . 'profit_margin', true );
$order = wc_get_order( $order_id );
$cost = $order->get_meta( '_alg_wc_cog_order_' . 'cost', true );
$handling_fee = $order->get_meta( '_alg_wc_cog_order_' . 'handling_fee', true );
$profit = $order->get_meta( '_alg_wc_cog_order_' . 'profit', true );
$profit_percent = $order->get_meta( '_alg_wc_cog_order_' . 'profit_percent', true );
$profit_margin = $order->get_meta( '_alg_wc_cog_order_' . 'profit_margin', true );
$profit_template = get_option( 'alg_wc_cog_orders_profit_html_template', '%profit%' );
$profit_placeholders = array(
'%profit%' => alg_wc_cog()->core->orders->format_order_column_value( $profit, 'profit' ),
'%profit%' => alg_wc_cog()->core->orders->format_order_column_value( $profit, 'profit' ),
'%profit_percent%' => alg_wc_cog()->core->orders->format_order_column_value( $profit_percent, 'profit_percent' ),
'%profit_margin%' => alg_wc_cog()->core->orders->format_order_column_value( $profit_margin, 'profit_margin' ),
'%profit_margin%' => alg_wc_cog()->core->orders->format_order_column_value( $profit_margin, 'profit_margin' ),
);
$profit_html = str_replace( array_keys( $profit_placeholders ), $profit_placeholders, $profit_template );
$table_args = array( 'table_heading_type' => 'vertical', 'table_class' => 'widefat', 'columns_styles' => array( '', 'text-align:right;' ) );
Expand Down Expand Up @@ -166,7 +169,7 @@ function render_order_meta_box( $post ) {
);
$cost_meta_keys = apply_filters( 'alg_wc_cog_cost_meta_keys', $cost_meta_keys );
foreach ( $cost_meta_keys as $key => $value ) {
$cost = get_post_meta( $order_id, $key, true );
$cost = $order->get_meta( $key, true );
if ( 0 != $cost ) {
$table_data[] = array( $value, alg_wc_cog_format_cost( $cost ) );
}
Expand All @@ -180,7 +183,7 @@ function render_order_meta_box( $post ) {
$extra_profit_meta_keys = array(); // Example: '_alg_wc_cog_order_shipping_extra_profit'] = __( 'Shipping to profit', 'cost-of-goods-for-woocommerce' );
$extra_profit_meta_keys = apply_filters( 'alg_wc_cog_extra_profit_meta_keys', $extra_profit_meta_keys );
foreach ( $extra_profit_meta_keys as $key => $value ) {
$cost = get_post_meta( $order_id, $key, true );
$cost = $order->get_meta( $key, true );
if ( 0 != $cost && ! empty( $cost ) ) {
$table_data[] = array( $value, alg_wc_cog_format_cost( $cost ) );
}
Expand All @@ -194,19 +197,21 @@ function render_order_meta_box( $post ) {
/**
* add_order_extra_cost_meta_box.
*
* @version 2.3.4
* @version 3.0.2
* @since 1.7.0
*/
function add_order_extra_cost_meta_box() {
if ( ! apply_filters( 'alg_wc_cog_create_order_meta_box_validation', true, 'extra_cost' ) ) {
return;
}
if ( in_array( true, alg_wc_cog()->core->orders->is_order_extra_cost_per_order ) ) {
$screen = alg_wc_cog()->core->orders->get_shop_order_screen_id();
add_meta_box( 'alg-wc-cog-extra-cost',
__( 'Cost of Goods', 'cost-of-goods-for-woocommerce' ) . ': ' . __( 'Extra costs', 'cost-of-goods-for-woocommerce' ),
array( $this, 'render_order_extra_cost_meta_box' ),
'shop_order',
'side'
$screen,
'side',
'high'
);
}
}
Expand Down Expand Up @@ -238,7 +243,7 @@ function render_order_extra_cost_meta_box( $post ) {
/**
* save_order_extra_cost.
*
* @version 2.2.0
* @version 3.0.2
* @since 1.7.0
*/
function save_order_extra_cost( $order_id, $post ) {
Expand All @@ -248,7 +253,9 @@ function save_order_extra_cost( $order_id, $post ) {
$id = 'alg_wc_cog_order_' . $fee_type . '_fee';
if ( isset( $_POST[ $id ] ) ) {
$value = floatval( $_POST[ $id ] );
update_post_meta( $order_id, '_' . $id, $value );
$order = wc_get_order( $order_id );
$order->update_meta_data( '_' . $id, $value );
$order->save();
}
}
}
Expand Down
Loading

0 comments on commit 29136fa

Please sign in to comment.