Skip to content

Commit

Permalink
v2.9.5
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-sg-pacheco committed Apr 18, 2023
1 parent 1e21142 commit f830826
Show file tree
Hide file tree
Showing 11 changed files with 564 additions and 236 deletions.
4 changes: 2 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.9.4
Version: 2.9.5
Author: WPFactory
Author URI: https://wpfactory.com
Text Domain: cost-of-goods-for-woocommerce
Expand Down Expand Up @@ -72,7 +72,7 @@ final class Alg_WC_Cost_of_Goods {
* @var string
* @since 1.0.0
*/
public $version = '2.9.4';
public $version = '2.9.5';

/**
* @var Alg_WC_Cost_of_Goods The single instance of the class
Expand Down
32 changes: 31 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.8.7
* @version 2.9.5
* @since 1.4.0
* @author WPFactory
*/
Expand Down Expand Up @@ -219,6 +219,36 @@ function alg_wc_cog_get_blocked_options_message() {
}
}

if ( ! function_exists( 'alg_wc_cog_sanitize_number' ) ) {

/**
* alg_wc_cog_sanitize_number.
*
* @version 2.9.5
* @since 2.9.5
*
* @param $args
*
* @return numeric
*/
function alg_wc_cog_sanitize_number( $args = null ) {
$args = wp_parse_args( $args, array(
'number' => 0,
'dots_and_commas_operation' => 'comma-to-dot', // comma-to-dot | dot-to-comma | none
) );
$args = apply_filters( 'alg_wc_cog_sanitize_number_args', $args );
$number = $args['number'];
$dots_and_commas_operation = $args['dots_and_commas_operation'];
if ( 'comma-to-dot' === $dots_and_commas_operation ) {
$number = str_replace( ',', '.', $number );
} elseif ( 'dot-to-comma' === $dots_and_commas_operation ) {
$number = str_replace( '.', ',', (string) $number );
}

return $number;
}
}

if ( ! function_exists( 'alg_wc_cog_get_regular_price' ) ) {
/**
* alg_wc_cog_get_regular_price.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Cost of Goods for WooCommerce - Background Process - Update Cost
* Cost of Goods for WooCommerce - Background Process - Update Cost.
*
* @version 2.5.1
* @version 2.9.5
* @since 2.5.1
* @author WPFactory
*/
Expand Down Expand Up @@ -35,7 +35,7 @@ protected function get_action_label() {
/**
* task.
*
* @version 2.5.1
* @version 2.9.5
* @since 2.5.1
*
* @param mixed $item
Expand All @@ -46,10 +46,12 @@ protected function task( $item ) {
$product_id = isset( $item['product_id'] ) ? $item['product_id'] : '';
$percentage = isset( $item['percentage'] ) ? $item['percentage'] : '';
$update_type = isset( $item['update_type'] ) ? $item['update_type'] : '';
$costs_filter = isset( $item['costs_filter'] ) ? $item['costs_filter'] : '';
alg_wc_cog()->core->products->update_product_cost_by_percentage( array(
'product_id' => $product_id,
'percentage' => $percentage,
'update_type' => $update_type, // profit | price
'costs_filter' => $costs_filter,
'update_variations' => true
) );
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Cost of Goods for WooCommerce - Background Process - Update variation costs.
*
* @version 2.9.5
* @since 2.9.5
* @author WPFactory
*/

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( 'Alg_WC_Cost_of_Goods_Update_Variation_Costs_Bkg_Process' ) ) :

class Alg_WC_Cost_of_Goods_Update_Variation_Costs_Bkg_Process extends Alg_WC_Cost_of_Goods_Bkg_Process {

/**
* @var string
*/
protected $action = 'alg_wc_cog_update_variation_costs_bkg_process';

/**
* get_action_label.
*
* @since 2.9.5
* @version 2.9.5
*
* @return string
*/
protected function get_action_label() {
return __( 'Cost of Goods for WooCommerce - Bulk update variation costs', 'cost-of-goods-for-woocommerce' );
}

/**
* task.
*
* @version 2.9.5
* @since 2.9.5
*
* @param mixed $item
* @return bool|mixed
*/
protected function task( $item ) {
parent::task( $item );
alg_wc_cog()->core->products->update_variation_cost_from_parent( $item );
return false;
}
}
endif;
14 changes: 10 additions & 4 deletions includes/class-alg-wc-cog-orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Orders Class.
*
* @version 2.9.4
* @version 2.9.5
* @since 2.1.0
* @author WPFactory
*/
Expand Down Expand Up @@ -1069,7 +1069,7 @@ function get_shipping_total_for_percentage_fees( $order ) {
/**
* update_order_items_costs.
*
* @version 2.9.1
* @version 2.9.5
* @since 1.1.0
* @todo [maybe] filters: add more?
* @todo [maybe] `$total_price`: customizable calculation method (e.g. `$order->get_subtotal()`) (this will affect `_alg_wc_cog_order_profit_margin`)
Expand Down Expand Up @@ -1201,7 +1201,10 @@ function update_order_items_costs( $args ) {
// calculate total profit, cost, handling fee per order items.
$quantity = $calculate_qty_excluding_refunds ? $item->get_quantity() + $order->get_qty_refunded_for_item( $item_id ) : $item->get_quantity();
if ( '' !== $cost || '' !== $handling_fee ) {
$cost = str_replace( ',', '.', $cost );
$cost = alg_wc_cog_sanitize_number( array(
'number' => $cost,
'dots_and_commas_operation' => 'comma-to-dot'
) );
$cost = (float) $cost;
$line_cost = $cost * $quantity;
$item_line_total = $item['line_total'];
Expand All @@ -1213,7 +1216,10 @@ function update_order_items_costs( $args ) {
$items_cost += $line_cost;
$total_price += $line_total;
// handling fee.
$handling_fee = str_replace( ',', '.', $handling_fee );
$handling_fee = alg_wc_cog_sanitize_number( array(
'number' => $handling_fee,
'dots_and_commas_operation' => 'comma-to-dot'
) );
$handling_fee = (float) $handling_fee;
$line_handling_fee = $handling_fee * $quantity;
$profit -= $line_handling_fee;
Expand Down
21 changes: 16 additions & 5 deletions includes/class-alg-wc-cog-products-cost-archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Products - Cost archive.
*
* @version 2.9.3
* @version 2.9.5
* @since 2.8.2
* @author WPFactory
*/
Expand Down Expand Up @@ -65,9 +65,10 @@ function add_cost_archive_meta_box() {
* @version 2.9.3
* @since 2.8.2
*
* @param null $args
* @param $args
*
* @return array
* @throws Exception
*/
function get_product_cost_archive( $args = null ) {
$args = wp_parse_args( $args, array(
Expand Down Expand Up @@ -122,7 +123,7 @@ function get_product_cost_archive( $args = null ) {
/**
* product_add_stock_meta_box.
*
* @version 2.9.3
* @version 2.9.5
* @since 2.8.2
* @todo [next] add option to delete all/selected history
*/
Expand All @@ -133,6 +134,7 @@ function display_product_cost_archive_table( $post ) {
$product_cost_archive = $this->get_product_cost_archive( array(
'product_id' => $post->ID
) );
//$sanitize_cost
if ( empty( $product_cost_archive ) ) {
echo '<p>'.__( 'There isn\'t a cost archive for this product yet.', 'cost-of-goods-for-woocommerce' ).'</p>';
echo '<p>' . sprintf( __( 'Please, check if the option %s is enabled and then update the cost.', 'cost-of-goods-for-woocommerce' ), '<strong><a href="' . admin_url( 'admin.php?page=wc-settings&tab=alg_wc_cost_of_goods' ) . '">' . __( 'Products > Cost archive > Save cost archive', 'cost-of-goods-for-woocommerce' ) . '</a></strong>' ) . '</p>';
Expand All @@ -143,12 +145,21 @@ function display_product_cost_archive_table( $post ) {
'new_cost_value' => __( 'New cost', 'cost-of-goods-for-woocommerce' ),
);
$table_rows = array();
$dots_and_commas_operation = 'comma-to-dot';
foreach ( $product_cost_archive as $cost_info ) {
$prev_cost = alg_wc_cog_sanitize_number( array(
'number' => $cost_info['prev_cost_value'],
'dots_and_commas_operation' => $dots_and_commas_operation
) );
$new_cost_value = alg_wc_cog_sanitize_number( array(
'number' => $cost_info['new_cost_value'],
'dots_and_commas_operation' => $dots_and_commas_operation
) );
$table_rows[] = array(
'val_by_col' => array(
wp_date( get_option( 'alg_wc_cog_save_cost_archive_date_format', 'Y-m-d' ), $cost_info['update_date'] ),
alg_wc_cog_format_cost( $cost_info['prev_cost_value'] ),
alg_wc_cog_format_cost( $cost_info['new_cost_value'] )
alg_wc_cog_format_cost( $prev_cost ),
alg_wc_cog_format_cost( $new_cost_value )
)
);
}
Expand Down
Loading

0 comments on commit f830826

Please sign in to comment.