diff --git a/cost-of-goods-for-woocommerce.php b/cost-of-goods-for-woocommerce.php index 15ae9b8..58fe5e0 100644 --- a/cost-of-goods-for-woocommerce.php +++ b/cost-of-goods-for-woocommerce.php @@ -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 @@ -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 diff --git a/includes/alg-wc-cog-functions.php b/includes/alg-wc-cog-functions.php index c025fac..51f99ad 100644 --- a/includes/alg-wc-cog-functions.php +++ b/includes/alg-wc-cog-functions.php @@ -2,7 +2,7 @@ /** * Cost of Goods for WooCommerce - Functions. * - * @version 2.8.7 + * @version 2.9.5 * @since 1.4.0 * @author WPFactory */ @@ -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. diff --git a/includes/background-process/class-alg-wc-cog-update-cost-bkg-process.php b/includes/background-process/class-alg-wc-cog-update-cost-bkg-process.php index f9f9d5a..8224299 100644 --- a/includes/background-process/class-alg-wc-cog-update-cost-bkg-process.php +++ b/includes/background-process/class-alg-wc-cog-update-cost-bkg-process.php @@ -1,8 +1,8 @@ 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; diff --git a/includes/background-process/class-alg-wc-cog-update-variation-costs-bkg-process.php b/includes/background-process/class-alg-wc-cog-update-variation-costs-bkg-process.php new file mode 100644 index 0000000..9367e83 --- /dev/null +++ b/includes/background-process/class-alg-wc-cog-update-variation-costs-bkg-process.php @@ -0,0 +1,48 @@ +core->products->update_variation_cost_from_parent( $item ); + return false; + } + } +endif; \ No newline at end of file diff --git a/includes/class-alg-wc-cog-orders.php b/includes/class-alg-wc-cog-orders.php index 74b25bd..8485538 100644 --- a/includes/class-alg-wc-cog-orders.php +++ b/includes/class-alg-wc-cog-orders.php @@ -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 */ @@ -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`) @@ -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']; @@ -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; diff --git a/includes/class-alg-wc-cog-products-cost-archive.php b/includes/class-alg-wc-cog-products-cost-archive.php index e78aed7..538e6a4 100644 --- a/includes/class-alg-wc-cog-products-cost-archive.php +++ b/includes/class-alg-wc-cog-products-cost-archive.php @@ -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 */ @@ -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( @@ -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 */ @@ -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 '
'.__( 'There isn\'t a cost archive for this product yet.', 'cost-of-goods-for-woocommerce' ).'
'; echo '' . sprintf( __( 'Please, check if the option %s is enabled and then update the cost.', 'cost-of-goods-for-woocommerce' ), '' . __( 'Products > Cost archive > Save cost archive', 'cost-of-goods-for-woocommerce' ) . '' ) . '
'; @@ -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 ) ) ); } diff --git a/includes/class-alg-wc-cog-products.php b/includes/class-alg-wc-cog-products.php index 6fa2774..ca81ea6 100644 --- a/includes/class-alg-wc-cog-products.php +++ b/includes/class-alg-wc-cog-products.php @@ -2,7 +2,7 @@ /** * Cost of Goods for WooCommerce - Products Class. * - * @version 2.9.4 + * @version 2.9.5 * @since 2.1.0 * @author WPFactory */ @@ -179,7 +179,7 @@ function handle_product_columns_style() { * * @see https://stackoverflow.com/a/4325608/1193038 * - * @version 2.3.5 + * @version 2.9.5 * @since 2.3.5 * * @param $value @@ -188,8 +188,12 @@ function handle_product_columns_style() { */ function sanitize_cog_meta( $value ) { if ( 'yes' === get_option( 'alg_wc_cog_products_sanitize_cog_meta', 'no' ) ) { - $value = str_replace( ',', '.', $value ); + $value = alg_wc_cog_sanitize_number( array( + 'number' => $value, + 'dots_and_commas_operation' => 'comma-to-dot' + ) ); } + return $value; } @@ -345,7 +349,7 @@ function render_product_columns( $column, $product_id ) { /** * get_product_cost. * - * @version 2.8.5 + * @version 2.9.5 * @since 1.0.0 */ function get_product_cost( $product_id, $args = null ) { @@ -365,11 +369,10 @@ function get_product_cost( $product_id, $args = null ) { ) { $cost = get_post_meta( $parent_id, '_alg_wc_cog_cost', true ); } - if ( 'comma-to-dot' === $dots_and_commas_operation ) { - $cost = str_replace( ',', '.', $cost ); - } elseif ( 'dot-to-comma' === $dots_and_commas_operation ) { - $cost = str_replace( '.', ',', (string) $cost ); - } + $cost = alg_wc_cog_sanitize_number( array( + 'number' => $cost, + 'dots_and_commas_operation' => $dots_and_commas_operation + ) ); $cost = $convert_to_number ? (float) $cost : $cost; return apply_filters( 'alg_wc_cog_get_product_cost', $cost, $product_id, isset( $parent_id ) ? $parent_id : null, $args ); } @@ -589,10 +592,32 @@ function update_product_price_by_profit( $args = array() ) { return true; } + /** + * update_variation_cost_from_parent. + * + * @version 2.9.5 + * @since 2.9.5 + * + * @param $args + * + * @return void + */ + function update_variation_cost_from_parent( $args = null ) { + $args = wp_parse_args( $args, array( + 'product_id' => '', + ) ); + $product_id = $args['product_id']; + $product = wc_get_product( $product_id ); + $parent_id = $product->get_parent_id(); + update_post_meta( $product_id, '_alg_wc_cog_cost', $this->get_product_cost( $parent_id, array( + 'check_parent_cost' => false + ) ) ); + } + /** * update_product_price. * - * @version 2.6.3 + * @version 2.9.5 * @since 2.5.1 * * @param null $args @@ -602,6 +627,7 @@ function update_product_price_by_profit( $args = array() ) { function update_product_cost_by_percentage( $args = null ) { $args = wp_parse_args( $args, array( 'product_id' => '', + 'costs_filter' => 'ignore_costs', // ignore_costs, products_without_costs, products_with_costs 'percentage' => 100, 'update_type' => 'costs_price', // costs_profit | costs_price 'update_variations' => true @@ -611,10 +637,13 @@ function update_product_cost_by_percentage( $args = null ) { $product = wc_get_product( $product_id ); $update_variations = $args['update_variations']; $update_type = $args['update_type']; + $costs_filter = $args['costs_filter']; if ( ! is_a( $product, 'WC_Product' ) ) { return false; } - update_post_meta( $product->get_id(), '_alg_wc_cog_cost', $this->calculate_product_cost_by_percentage( $product->get_price(), $percentage, $update_type ) ); + if ( $this->can_update_product_cost_based_on_costs_filter( $product_id, $costs_filter ) ) { + update_post_meta( $product->get_id(), '_alg_wc_cog_cost', $this->calculate_product_cost_by_percentage( $product->get_price(), $percentage, $update_type ) ); + } if ( $update_variations && $product->is_type( 'variable' ) && $product instanceof WC_Product_Variable @@ -625,12 +654,38 @@ function update_product_cost_by_percentage( $args = null ) { if ( empty( $variation_id ) || empty( $display_price ) ) { continue; } - update_post_meta( $variation_id, '_alg_wc_cog_cost', $this->calculate_product_cost_by_percentage( $display_price, $percentage, $update_type ) ); + if ( $this->can_update_product_cost_based_on_costs_filter( $variation_id, $costs_filter ) ) { + update_post_meta( $variation_id, '_alg_wc_cog_cost', $this->calculate_product_cost_by_percentage( $display_price, $percentage, $update_type ) ); + } } } return true; } + /** + * can_update_product_cost. + * + * @version 2.9.5 + * @since 2.9.5 + * + * @param $product_id + * @param $costs_filter + * + * @return bool + */ + function can_update_product_cost_based_on_costs_filter( $product_id, $costs_filter ): bool { + $can_update = false; + if ( + ( 'products_without_costs' === $costs_filter && empty( $this->get_product_cost( $product_id, array( 'check_parent_cost' => false ) ) ) ) || + ( 'products_with_costs' === $costs_filter && ! empty( $this->get_product_cost( $product_id, array( 'check_parent_cost' => false ) ) ) ) || + ( 'ignore_costs' === $costs_filter ) + ) { + $can_update = true; + } + + return $can_update; + } + /** * calculate_product_cost. * diff --git a/includes/settings/class-alg-wc-cog-settings-compatibility.php b/includes/settings/class-alg-wc-cog-settings-compatibility.php index 4c1b830..c8c03bc 100644 --- a/includes/settings/class-alg-wc-cog-settings-compatibility.php +++ b/includes/settings/class-alg-wc-cog-settings-compatibility.php @@ -91,9 +91,9 @@ function get_settings() { ); $curcy_multicurrency_opts = array( array( - 'title' => __( 'CURCY – Multi Currency for WooCommerce', 'cost-of-goods-for-woocommerce' ), + 'title' => __( 'CURCY - Multi Currency for WooCommerce', 'cost-of-goods-for-woocommerce' ), 'type' => 'title', - 'desc' => sprintf( __( 'Compatibility with %s plugin.', 'cost-of-goods-for-woocommerce' ), '' . __( 'CURCY – Multi Currency for WooCommerce', 'cost-of-goods-for-woocommerce' ) . '' ), + 'desc' => sprintf( __( 'Compatibility with %s plugin.', 'cost-of-goods-for-woocommerce' ), '' . __( 'CURCY - Multi Currency for WooCommerce', 'cost-of-goods-for-woocommerce' ) . '' ), 'id' => 'alg_wc_cog_compatibility_curcy_options', ), array( diff --git a/includes/tools/class-alg-wc-cog-bulk-edit-tool.php b/includes/tools/class-alg-wc-cog-bulk-edit-tool.php index 7b1b99f..5b360ee 100644 --- a/includes/tools/class-alg-wc-cog-bulk-edit-tool.php +++ b/includes/tools/class-alg-wc-cog-bulk-edit-tool.php @@ -2,7 +2,7 @@ /** * Cost of Goods for WooCommerce - Bulk Edit Tool Class. * - * @version 2.8.8 + * @version 2.9.5 * @since 1.2.0 * @author WPFactory */ @@ -24,21 +24,40 @@ class Alg_WC_Cost_of_Goods_Bulk_Edit_Tool { /** * Tool prices page's slug. + * + * @since 2.9.5 * * @var string */ private $page_slug_prices = 'bulk-edit-prices'; /** + * Update costs bkg process. + * + * @since 2.9.5 + * * @var Alg_WC_Cost_of_Goods_Update_Cost_Bkg_Process */ public $update_cost_bkg_process; /** + * Update prices bkg process. + * + * @since 2.9.5 + * * @var Alg_WC_Cost_of_Goods_Update_Price_Bkg_Process */ public $update_price_bkg_process; + /** + * Update variation costs bkg process. + * + * @since 2.9.5 + * + * @var Alg_WC_Cost_of_Goods_Update_Variation_Costs_Bkg_Process + */ + public $update_variation_costs_bkg_process; + /** * Constructor. * @@ -118,20 +137,22 @@ function remove_query_args() { /** * init_bkg_process. * - * @version 2.6.3 + * @version 2.9.5 * @since 2.5.1 */ function init_bkg_process() { require_once( alg_wc_cog()->plugin_path() . '/includes/background-process/class-alg-wc-cog-update-cost-bkg-process.php' ); require_once( alg_wc_cog()->plugin_path() . '/includes/background-process/class-alg-wc-cog-update-price-bkg-process.php' ); - $this->update_cost_bkg_process = new Alg_WC_Cost_of_Goods_Update_Cost_Bkg_Process(); - $this->update_price_bkg_process = new Alg_WC_Cost_of_Goods_Update_Price_Bkg_Process(); + require_once( alg_wc_cog()->plugin_path() . '/includes/background-process/class-alg-wc-cog-update-variation-costs-bkg-process.php' ); + $this->update_cost_bkg_process = new Alg_WC_Cost_of_Goods_Update_Cost_Bkg_Process(); + $this->update_price_bkg_process = new Alg_WC_Cost_of_Goods_Update_Price_Bkg_Process(); + $this->update_variation_costs_bkg_process = new Alg_WC_Cost_of_Goods_Update_Variation_Costs_Bkg_Process(); } /** * Update costs on Ajax for bulk edit tools. * - * @version 2.7.9 + * @version 2.9.5 * @since 2.5.1 */ function ajax_update_product_data() { @@ -144,16 +165,19 @@ function ajax_update_product_data() { if ( isset( $form_data["_nonce_{$update_type}_val"] ) && ! wp_verify_nonce( $form_data["_nonce_{$update_type}_val"], "_nonce_{$update_type}_action" ) ) { wp_send_json_error( esc_html__( 'Something went wrong! Please try again.', 'cost-of-goods-for-woocommerce' ) ); } - $percentage = isset( $form_data['percentage'] ) ? (float) sanitize_text_field( $form_data['percentage'] ) : ''; - $absolute_profit = isset( $form_data['absolute_profit'] ) ? (float) sanitize_text_field( $form_data['absolute_profit'] ) : ''; - $affected_field = isset( $form_data['affected_field'] ) ? $form_data['affected_field'] : 'regular_price'; - $rounding = isset( $form_data['rounding'] ) ? $form_data['rounding'] : ''; + $percentage = isset( $form_data['percentage'] ) ? (float) sanitize_text_field( $form_data['percentage'] ) : ''; + $absolute_profit = isset( $form_data['absolute_profit'] ) ? (float) sanitize_text_field( $form_data['absolute_profit'] ) : ''; + $affected_field = isset( $form_data['affected_field'] ) ? $form_data['affected_field'] : 'regular_price'; + $rounding = isset( $form_data['rounding'] ) ? $form_data['rounding'] : ''; + $costs_filter = sanitize_text_field( isset( $form_data['costs_filter'] ) ? $form_data['costs_filter'] : 'ignore_costs' ); + $update_variation_costs = filter_var( isset( $form_data['update_variation_costs'] ) ? $form_data['update_variation_costs'] : false, FILTER_VALIDATE_BOOLEAN ); + $empty_variation_costs_required = filter_var( isset( $form_data['empty_variation_costs_required'] ) ? $form_data['empty_variation_costs_required'] : false, FILTER_VALIDATE_BOOLEAN ); // Requirements. if ( empty( $update_type ) ) { wp_send_json_error( esc_html__( 'Some error has occurred. Please, try again.', 'cost-of-goods-for-woocommerce' ) ); } if ( 'update_costs' === $tool_type ) { - if ( empty( $percentage ) ) { + if ( isset( $form_data['percentage'] ) && empty( $percentage ) ) { wp_send_json_error( esc_html__( 'Invalid percentage.', 'cost-of-goods-for-woocommerce' ) ); } } else { @@ -171,6 +195,28 @@ function ajax_update_product_data() { 'fields' => 'ids', 'tax_query' => array() ); + if ( $update_variation_costs ) { + $query_args['post_type'] = 'product_variation'; + if ( $empty_variation_costs_required ) { + $query_args['meta_query'] = array( + 'relation' => 'OR', + array( + 'key' => '_alg_wc_cog_cost', + 'value' => 0, + 'compare' => '==', + ), + array( + 'key' => '_alg_wc_cog_cost', + 'value' => '', + 'compare' => '==', + ), + array( + 'key' => '_alg_wc_cog_cost', + 'compare' => 'NOT EXISTS', + ), + ); + } + } // Product Category. $product_category = isset( $form_data['product_category'] ) ? $form_data['product_category'] : ''; $product_category = is_array( $product_category ) ? $product_category : array(); @@ -197,101 +243,103 @@ function ajax_update_product_data() { } $posts = get_posts( $query_args ); $bkg_process_min_amount = get_option( 'alg_wc_cog_bkg_process_min_amount', 100 ); - $args = array( - 'products' => $posts, - 'bkg_process' => count( $posts ) >= $bkg_process_min_amount, - 'options' => array( - 'percentage' => $percentage, - 'absolute_profit' => $absolute_profit, - 'update_type' => $update_type, - 'affected_field' => $affected_field, - 'rounding' => $rounding, - ), - ); + $args = array( + 'products' => $posts, + 'bkg_process' => count( $posts ) >= $bkg_process_min_amount, + 'options' => array( + 'costs_filter' => $costs_filter, + 'percentage' => $percentage, + 'absolute_profit' => $absolute_profit, + 'update_type' => $update_type, + 'affected_field' => $affected_field, + 'rounding' => $rounding, + ), + ); // For bulk update - costs if( 'update_costs' === $tool_type ) { - wp_send_json_success( $this->bulk_update_costs( $args ) ); + if ( ! $update_variation_costs ) { + $bkg_process_progress_msg = __( 'Product costs are being updated via background processing.', 'cost-of-goods-for-woocommerce' ); + $bkg_process_progress_msg .= 'yes' === get_option( 'alg_wc_cog_bkg_process_send_email', 'yes' ) ? ' ' . sprintf( __( 'An email is going to be sent to %s when the task is completed.', 'cost-of-goods-for-woocommerce' ), get_option( 'alg_wc_cog_bkg_process_email_to', get_option( 'admin_email' ) ) ) : ''; + wp_send_json_success( $this->bulk_update_products( $args, array( + 'bkg_process_obj' => $this->update_cost_bkg_process, + 'success_msg' => __( 'Successfully updated product costs.', 'cost-of-goods-for-woocommerce' ), + 'bkg_process_progress_msg' => $bkg_process_progress_msg, + 'no_bkg_process_function' => 'update_product_cost_by_percentage', + ) ) ); + } else { + $bkg_process_progress_msg = __( 'Variation costs are being updated via background processing.', 'cost-of-goods-for-woocommerce' ); + $bkg_process_progress_msg .= 'yes' === get_option( 'alg_wc_cog_bkg_process_send_email', 'yes' ) ? ' ' . sprintf( __( 'An email is going to be sent to %s when the task is completed.', 'cost-of-goods-for-woocommerce' ), get_option( 'alg_wc_cog_bkg_process_email_to', get_option( 'admin_email' ) ) ) : ''; + wp_send_json_success( $this->bulk_update_products( $args, array( + 'bkg_process_obj' => $this->update_variation_costs_bkg_process, + 'success_msg' => __( 'Successfully updated variation costs.', 'cost-of-goods-for-woocommerce' ), + 'bkg_process_progress_msg' => $bkg_process_progress_msg, + 'no_bkg_process_function' => 'update_variation_cost_from_parent', + ) ) ); + } } // For bulk update - prices if( 'update_prices' === $tool_type ) { - wp_send_json_success( $this->bulk_update_prices( $args ) ); + $bkg_process_progress_msg = __( 'Product prices are being updated via background processing.', 'cost-of-goods-for-woocommerce' ); + $bkg_process_progress_msg .= 'yes' === get_option( 'alg_wc_cog_bkg_process_send_email', 'yes' ) ? ' ' . sprintf( __( 'An email is going to be sent to %s when the task is completed.', 'cost-of-goods-for-woocommerce' ), get_option( 'alg_wc_cog_bkg_process_email_to', get_option( 'admin_email' ) ) ) : ''; + wp_send_json_success( $this->bulk_update_products( $args, array( + 'bkg_process_obj' => $this->update_price_bkg_process, + 'success_msg' => __( 'Successfully updated product prices.', 'cost-of-goods-for-woocommerce' ), + 'bkg_process_progress_msg' => $bkg_process_progress_msg, + 'no_bkg_process_function' => 'update_product_cost_by_percentage', + ) ) ); } } /** - * Bulk update product prices - * - * @version 2.7.9 - * @since 2.6.1 - * - * @param array $args + * bulk_update_products. + * + * @version 2.9.5 + * @since 2.9.5 + * + * @param $update_args + * @param $function_args * * @return string */ - function bulk_update_prices( $args = array() ) { - $args = wp_parse_args( $args, array( + function bulk_update_products( $update_args = array(), $function_args = array() ) { + $update_args = wp_parse_args( $update_args, array( 'products' => array(), 'bkg_process' => false, 'options' => array(), ) ); - $products = $args['products']; - $bkg_process = $args['bkg_process']; - $options = $args['options']; - $message = __( 'Successfully updated product prices.', 'cost-of-goods-for-woocommerce' ); - if ( $bkg_process ) { - $message = __( 'Product prices are being updated via background processing.', 'cost-of-goods-for-woocommerce' ); - $message .= 'yes' === get_option( 'alg_wc_cog_bkg_process_send_email', 'yes' ) ? ' ' . sprintf( __( 'An email is going to be sent to %s when the task is completed.', 'cost-of-goods-for-woocommerce' ), get_option( 'alg_wc_cog_bkg_process_email_to', get_option( 'admin_email' ) ) ) : ''; - $this->update_price_bkg_process->cancel_process(); - } - foreach ( $products as $product_id ) { - $_options = wp_parse_args( $options, array( 'product_id' => $product_id ) ); - if ( $bkg_process ) { - $this->update_price_bkg_process->push_to_queue( $_options ); - } else { - alg_wc_cog()->core->products->update_product_price_by_profit( $_options ); - } - } - if ( $bkg_process ) { - $this->update_price_bkg_process->save()->dispatch(); - } - return esc_html( $message ); - } - - /** - * Bulk update product costs - * - * @version 2.6.1 - * @since 2.6.1 - * - * @param array $args - * - * @return string - */ - function bulk_update_costs( $args = array() ) { - $products = isset( $args['products'] ) ? (array) $args['products'] : array(); - $bkg_process = isset( $args['bkg_process'] ) && $args['bkg_process']; - $options = isset( $args['options'] ) ? (array) $args['options'] : array(); - $message = __( 'Successfully updated product costs.', 'cost-of-goods-for-woocommerce' ); - + $products = $update_args['products']; + $bkg_process = $update_args['bkg_process']; + $options = $update_args['options']; + $function_args = wp_parse_args( $function_args, array( + 'bkg_process_obj' => null, + 'success_msg' => __( 'Successfully updated product prices.', 'cost-of-goods-for-woocommerce' ), + 'bkg_process_progress_msg' => __( 'Product prices are being updated via background processing.', 'cost-of-goods-for-woocommerce' ), + 'no_bkg_process_function' => 'update_product_price_by_profit', + 'no_bkg_process_obj' => alg_wc_cog()->core->products + ) ); + $message = $function_args['success_msg']; + $bkg_process_obj = $function_args['bkg_process_obj']; + $no_bkg_process_function = $function_args['no_bkg_process_function']; + $no_bkg_process_obj = $function_args['no_bkg_process_obj']; if ( $bkg_process ) { - $message = __( 'Product costs are being updated via background processing.', 'cost-of-goods-for-woocommerce' ); - $message .= 'yes' === get_option( 'alg_wc_cog_bkg_process_send_email', 'yes' ) ? ' ' . sprintf( __( 'An email is going to be sent to %s when the task is completed.', 'cost-of-goods-for-woocommerce' ), get_option( 'alg_wc_cog_bkg_process_email_to', get_option( 'admin_email' ) ) ) : ''; - $this->update_cost_bkg_process->cancel_process(); + $message = $function_args['bkg_process_progress_msg']; + call_user_func( array( $bkg_process_obj, "cancel_process" ) ); } - foreach ( $products as $product_id ) { $_options = wp_parse_args( $options, array( 'product_id' => $product_id ) ); if ( $bkg_process ) { - $this->update_cost_bkg_process->push_to_queue( $_options ); + call_user_func_array( array( $bkg_process_obj, "push_to_queue" ), array( $_options ) ); } else { - alg_wc_cog()->core->products->update_product_cost_by_percentage( $_options ); + call_user_func_array( array( $no_bkg_process_obj, $no_bkg_process_function ), array( $_options ) ); } } - if ( $bkg_process ) { - $this->update_cost_bkg_process->save()->dispatch(); + call_user_func( array( $bkg_process_obj, "save" ) ); + call_user_func( array( $bkg_process_obj, "dispatch" ) ); } - + if ( empty( $products ) ) { + $message = __( 'It is not necessary to update any products.', 'cost-of-goods-for-woocommerce' ); + } return esc_html( $message ); } @@ -375,10 +423,40 @@ function display_bulk_edit_prices_profit() { + + ++ | +
+
+ + + + |
+
---|
+ +
++ +
+