Skip to content

Instantly share code, notes, and snippets.

@devlifeX
Created April 17, 2024 15:17
Show Gist options
  • Save devlifeX/fd66978ad234fffdc52ff41d95fc032f to your computer and use it in GitHub Desktop.
Save devlifeX/fd66978ad234fffdc52ff41d95fc032f to your computer and use it in GitHub Desktop.
The Bulk Set Variations Stock to Zero plugin provides a quick and efficient solution for managing stock levels in WooCommerce. This lightweight plugin enhances the WooCommerce bulk edit functionality, allowing you to easily set the stock quantity of all variations for a selected variable product to zero in just a few clicks.
<?php
/*
Plugin Name: Bulk Edit for variation products
Author: Dariush vesal
Plugin URI: https://vesal.blog
Description: Bulk Edit for variation products
Author URI: https://vesal.blog
Text Domain: bulk-variation
Version: 1.0.0
*/
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();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment