Skip to content

Commit

Permalink
v3.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo-sg-pacheco committed May 8, 2024
1 parent a23fb31 commit 76b6cec
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 138 deletions.
2 changes: 1 addition & 1 deletion 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: 3.3.9
Version: 3.4.0
Author: WPFactory
Author URI: https://wpfactory.com
Text Domain: cost-of-goods-for-woocommerce
Expand Down
2 changes: 1 addition & 1 deletion includes/class-alg-wc-cog.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class Alg_WC_Cost_of_Goods {
* @since 1.0.0
* @var string
*/
public $version = '3.3.9';
public $version = '3.4.0';

/**
* @since 1.0.0
Expand Down
152 changes: 106 additions & 46 deletions includes/tools/class-alg-wc-cog-bulk-edit-tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Cost of Goods for WooCommerce - Bulk Edit Tool Class.
*
* @version 3.3.9
* @version 3.4.0
* @since 1.2.0
* @author WPFactory
*/
Expand Down Expand Up @@ -216,6 +216,31 @@ function handle_costs_filter_query( $query, $costs_filter ) {
return $query;
}

/**
* handle_stock_status_filter_query.
*
* @version 3.4.0
* @since 3.4.0
*
* @param $query
* @param $stock_status
*
* @return array
*/
function handle_stock_status_filter_query( $query, $stock_status = '' ) {
if ( ! empty( $stock_status ) ) {
$query['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => '_stock_status',
'value' => $stock_status
),
);
}

return $query;
}

/**
* filter_products_query_by_costs.
*
Expand Down Expand Up @@ -306,72 +331,77 @@ function init_bkg_process() {
/**
* get_products_to_be_updated.
*
* @version 3.3.0
* @version 3.4.0
* @since 3.3.0
*
* @param $args
*
* @return array|stdClass
*/
function get_products_to_be_updated( $args ) {
$args = wp_parse_args( $args, array(
$args = wp_parse_args( $args, array(
'product_category' => array(),
'product_tag' => array(),
'cost_filter' => '',
'update_method' => '',
'stock_status' => ''
) );
$product_categories = $args['product_category'];
$update_method = $args['update_method'];
$product_tags = $args['product_tag'];
$cost_filter = $args['cost_filter'];
$update_method = $args['update_method'];
$product_tags = $args['product_tag'];
$cost_filter = $args['cost_filter'];
$stock_status_filter = $args['stock_status'];

// Product args.
$products_args = array(
$products_args = array(
'type' => array_merge( array_keys( wc_get_product_types() ) ),
'status' => array( 'publish' ),
'limit' => '-1',
'return' => 'ids',
);
$products_args = $this->handle_category_filter_wc_get_products_args( $product_categories, $products_args );
$products_args = $this->handle_tags_filter_wc_get_products_args( $product_tags, $products_args );
$products_args = $this->handle_category_filter_wc_get_products_args( $product_categories, $products_args );
$products_args = $this->handle_tags_filter_wc_get_products_args( $product_tags, $products_args );
$products_from_taxes_args = $products_args;
if ( ! empty( $cost_filter ) ) {
$products_args = $this->handle_costs_filter_wc_get_products_args( $cost_filter, $products_args );
}
$products_args['stock_status'] = $stock_status_filter;

// Child products query args.
$child_products_query_args = array(
'post_type' => array( 'product_variation' ),
'posts_per_page' => - 1,
'fields' => 'ids',
'no_found_rows' => true,

);
if ( ! empty( $cost_filter ) ) {
$child_products_query_args = $this->handle_costs_filter_query( $child_products_query_args, $cost_filter );
}
if ( ! empty( $stock_status_filter ) ) {
$child_products_query_args = $this->handle_stock_status_filter_query( $child_products_query_args, $stock_status_filter );
}

// Get products.
$products_from_taxes = empty( $product_tags ) && empty( $product_categories ) ? array() : wc_get_products( $products_from_taxes_args );
$products = wc_get_products( $products_args );

switch ( $update_method ) {
case 'set_variation_costs_from_parents':
$products = wc_get_products( $products_args );
if ( ! empty( $products ) ) {
$child_products_query_args['post_parent__in'] = $products;
$child_products_query_args['post_parent__not_in'] = array( 0 );
$child_products = new WP_Query( $child_products_query_args );
if ( $child_products->have_posts() ) {
$products = $child_products->posts;
} else {
$products = array();
}
$child_products_query_args['post_parent__in'] = $products_from_taxes;
$child_products_query_args['post_parent__not_in'] = array( 0 );
$child_products = new WP_Query( $child_products_query_args );
if ( $child_products->have_posts() ) {
$products = $child_products->posts;
} else {
$products = array();
}
break;
default:
$products = wc_get_products( $products_args );
if ( ! empty( $products ) ) {
$child_products_query_args['post_parent__in'] = $products;
$child_products = new WP_Query( $child_products_query_args );
if ( $child_products->have_posts() ) {
$products = array_merge( $products, $child_products->posts );
}
$child_products_query_args['post_parent__in'] = $products_from_taxes;
$child_products = new WP_Query( $child_products_query_args );
if ( $child_products->have_posts() ) {
$products = array_merge( $products, $child_products->posts );
}
break;
}
Expand All @@ -382,7 +412,7 @@ function get_products_to_be_updated( $args ) {
/**
* update_costs.
*
* @version 3.3.0
* @version 3.4.0
* @since 3.3.0
*
* @return void
Expand All @@ -394,7 +424,8 @@ function update_costs() {
'costs_filter' => '',
'_nonce_costs_automatically_val' => '',
'product_category' => array(),
'product_tag' => array()
'product_tag' => array(),
'product_stock_status' => '',
) );

if (
Expand All @@ -406,17 +437,19 @@ function update_costs() {
}

// Sanitize args.
$cost_edit_value = floatval( $args['cost_edit_value'] );
$product_categories = empty( $args['product_category'] ) ? array() : array_map( 'intval', $args['product_category'] );
$product_tags = empty( $args['product_tag'] ) ? array() : array_map( 'intval', $args['product_tag'] );
$cost_filter = in_array( $filter = sanitize_text_field( $args['costs_filter'] ), wp_list_pluck( $this->get_cost_filter_options(), 'id' ) ) ? $filter : '';
$cost_edit_value = floatval( $args['cost_edit_value'] );
$product_categories = empty( $args['product_category'] ) ? array() : array_map( 'intval', $args['product_category'] );
$product_tags = empty( $args['product_tag'] ) ? array() : array_map( 'intval', $args['product_tag'] );
$cost_filter = in_array( $filter = sanitize_text_field( $args['costs_filter'] ), wp_list_pluck( $this->get_cost_filter_options(), 'id' ) ) ? $filter : '';
$stock_status_filter = sanitize_text_field( $args['product_stock_status'] );

// Get products to be updated.
$products = $this->get_products_to_be_updated( array(
'product_category' => $product_categories,
'product_tag' => $product_tags,
'cost_filter' => $cost_filter,
'update_method' => $bulk_edit_costs_method,
'product_category' => $product_categories,
'product_tag' => $product_tags,
'cost_filter' => $cost_filter,
'update_method' => $bulk_edit_costs_method,
'stock_status' => $stock_status_filter
) );

// Check if background process is needed.
Expand Down Expand Up @@ -482,7 +515,7 @@ function update_costs() {
/**
* update_prices.
*
* @version 3.3.9
* @version 3.4.0
* @since 3.3.0
*
* @return void
Expand All @@ -495,7 +528,8 @@ function update_prices() {
'price_rounding' => '',
'_nonce_prices_automatically_val' => '',
'product_category' => array(),
'product_tag' => array()
'product_tag' => array(),
'product_stock_status' => '',
) );

if (
Expand All @@ -507,17 +541,19 @@ function update_prices() {
}

// Sanitize args.
$price_edit_value = floatval( $args['price_edit_value'] );
$product_categories = empty( $args['product_category'] ) ? array() : array_map( 'intval', $args['product_category'] );
$product_tags = empty( $args['product_tag'] ) ? array() : array_map( 'intval', $args['product_tag'] );
$price_type = sanitize_text_field( $args['price_type'] );
$price_rounding = sanitize_text_field( $args['price_rounding'] );
$price_edit_value = floatval( $args['price_edit_value'] );
$product_categories = empty( $args['product_category'] ) ? array() : array_map( 'intval', $args['product_category'] );
$product_tags = empty( $args['product_tag'] ) ? array() : array_map( 'intval', $args['product_tag'] );
$price_type = sanitize_text_field( $args['price_type'] );
$price_rounding = sanitize_text_field( $args['price_rounding'] );
$stock_status_filter = sanitize_text_field( $args['product_stock_status'] );

// Get products to be updated.
$products = $this->get_products_to_be_updated( array(
'product_category' => $product_categories,
'product_tag' => $product_tags,
'update_method' => $bulk_edit_prices_method,
'stock_status' => $stock_status_filter
) );

// Check if background process is needed.
Expand Down Expand Up @@ -777,7 +813,7 @@ function get_cost_filter_options() {
return array(
array(
'id' => 'ignore_costs',
'label' => __( 'Update products regardless of their current cost', 'cost-of-goods-for-woocommerce' ),
'label' => __( 'Ignore current cost', 'cost-of-goods-for-woocommerce' ),
'desc' => __( '', 'cost-of-goods-for-woocommerce' ),
),
array(
Expand All @@ -796,13 +832,14 @@ function get_cost_filter_options() {
/**
* display_bulk_edit_prices.
*
* @version 3.3.0
* @version 3.4.0
* @since 2.6.1
*/
function display_bulk_edit_prices() {
$disabled = apply_filters( 'alg_wc_cog_settings', 'disabled' );
$blocked_text = apply_filters( 'alg_wc_cog_settings', alg_wc_cog_get_blocked_options_message() );
$methods = $this->get_bulk_edit_prices_methods();
$stock_statuses = wc_get_product_stock_status_options();
?>
<table class="form-table bulk-edit-auto" role="presentation">
<tr>
Expand Down Expand Up @@ -908,21 +945,33 @@ function display_bulk_edit_prices() {
</p>
</td>
</tr>
<tr>
<th scope="row"><label for="product-stock-status"><?php esc_html_e( 'Filter by stock status', 'cost-of-goods-for-woocommerce' ); ?></label></th>
<td>
<select class="wc-enhanced-select" id="product-stock-status" name="product_stock_status">
<option value=""><?php echo esc_html__( 'Ignore stock status', 'woocommerce' ) ?></option>
<?php foreach ( $stock_statuses as $status => $label ) : ?>
<option value="<?php echo esc_attr( $status ) ?> "><?php echo esc_html( $label ) ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
<?php
}

/**
* Display content for Bulk edit costs automatically.
*
* @version 2.9.5
* @version 3.4.0
* @since 2.5.1
*/
function display_bulk_edit_costs_automatically() {
$disabled = apply_filters( 'alg_wc_cog_settings', 'disabled' );
$blocked_text = apply_filters( 'alg_wc_cog_settings', alg_wc_cog_get_blocked_options_message() );
$methods = $this->get_bulk_edit_costs_methods();
$cost_filter_options = $this->get_cost_filter_options();
$stock_statuses = wc_get_product_stock_status_options();
?>
<table class="form-table bulk-edit-auto" role="presentation">
<tr>
Expand Down Expand Up @@ -1042,6 +1091,17 @@ function display_bulk_edit_costs_automatically() {
</p>
</td>
</tr>
<tr>
<th scope="row"><label for="product-stock-status"><?php esc_html_e( 'Filter by stock status', 'cost-of-goods-for-woocommerce' ); ?></label></th>
<td>
<select class="wc-enhanced-select" id="product-stock-status" name="product_stock_status">
<option value=""><?php echo esc_html__( 'Ignore stock status', 'woocommerce' ) ?></option>
<?php foreach ( $stock_statuses as $status => $label ) : ?>
<option value="<?php echo esc_attr( $status ) ?> "><?php echo esc_html( $label ) ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
<?php
}
Expand Down
Loading

0 comments on commit 76b6cec

Please sign in to comment.