-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk-variation.php
80 lines (59 loc) · 1.93 KB
/
bulk-variation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* @version 1.0.0
* @since 1.0.0
* @author Mahdi Aghtaee <[email protected]>
* @Text Domain: bulk-variation
*/
if (!defined('ABSPATH')) {
exit;
}
class BulkVariation {
public function __construct() {
$this->hooks();
}
public function hooks() {
add_filter('bulk_actions-edit-product', [$this, 'add_custom_bulk_action']);
add_action('admin_action_all_variation_zero_stock', [$this, 'handle_zero_stock_bulk_action']);
}
function add_custom_bulk_action($bulk_actions) {
$bulk_actions['all_variation_zero_stock'] = __('Make All Variation Zero Stock', 'bulk-variation');
return $bulk_actions;
}
function handle_zero_stock_bulk_action() {
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
if ('all_variation_zero_stock' === $action) {
$product_ids = isset($_REQUEST['post']) ? $_REQUEST['post'] : array();
foreach ($product_ids as $product_id) {
$this->set_variation_quantity($product_id, 0);
$var_ids = $this->get_variation_post_ids($product_id);
foreach ($var_ids as $key => $v_id) {
$this->set_variation_quantity($v_id, 0);
}
}
}
}
function set_variation_quantity($variation_id, $quantity = 0) {
update_post_meta($variation_id, '_stock', $quantity);
update_post_meta($variation_id, '_manage_stock', 'yes');
update_post_meta($variation_id, '_stock_status', 'outofstock');
}
function get_variation_post_ids($parent_id) {
$args = array(
'post_parent' => $parent_id,
'post_type' => 'product_variation',
'numberposts' => -1,
'post_status' => 'any'
);
$variations = get_children($args);
// Extract post IDs from the variation objects
$variation_ids = array();
foreach ($variations as $variation) {
$variation_ids[] = $variation->ID;
}
return $variation_ids;
}
}
add_action('admin_init', function () {
new BulkVariation();
});