admin管理员组

文章数量:1125361

Hey guys I have a Woo store where pretty much all my SKUs are wrong. I am trying to create a plugin to update all the SKU's in bulk but it is not working as should. Here is the code snippet:

<?php

 if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}

/**
* Add a submenu page under WooCommerce menu
*/
function custom_sku_update_menu() {
add_submenu_page(
    'woocommerce',
    'Update Product SKUs',
    'Update SKUs',
    'manage_options',
    'custom-sku-update',
    'custom_sku_update_page'
);
}

add_action('admin_menu', 'custom_sku_update_menu');

/**
* Callback function for the submenu page
*/
function custom_sku_update_page() {
?>
<div class="wrap">
    <h1>Update Product SKUs</h1>
    <form method="post">
        <p><strong>Note:</strong> This will update product SKUs in bulk based on preset criteria.</p>
        <p>Example 1: If SKU is 1234, it will become 000001234</p>
        <p>Example 2: If SKU is 11-345, it will become 110000345</p>
        <p>Example 3: If SKU is 12345, it will become 000012345</p>
        <input type="submit" name="update_skus" class="button button-primary" value="Update SKUs">
    </form>
</div>
<?php
}

/**
* Function to update product SKUs based on preset criteria
*/
function update_product_skus() {
// Get all products
$args = array(
    'post_type' => 'product',
    'posts_per_page' => -1,
);
$products = new WP_Query($args);

if ($products->have_posts()) {
    while ($products->have_posts()) {
        $products->the_post();
        $product_id = get_the_ID();
        $product = wc_get_product($product_id);

        // Get current SKU
        $sku = $product->get_sku();

        // Update SKU based on criteria
        $new_sku = $sku;

        // Example 1: If SKU is 1234, it will become 000001234
        if (preg_match('/^\d{4}$/', $sku)) {
            $new_sku = sprintf('%010d', $sku);
        }

        // Example 2: If SKU is 11-345, it will become 110000345
        if (preg_match('/^(\d+)-(\d+)$/', $sku, $matches)) {
            $new_sku = $matches[1] . sprintf('%04d', $matches[2]);
        }

        // Example 3: If SKU is 12345, it will become 000012345
        if (preg_match('/^\d{5}$/', $sku)) {
            $new_sku = sprintf('%09d', $sku);
        }

        // Only update SKU if it has changed
        if ($new_sku !== $sku) {
            // Update SKU
            $product->set_sku($new_sku);
            $product->save();
        }
    }
}

wp_reset_postdata();
}

/**
* Hook to run the SKU update when the form is submitted
*/
add_action('admin_init', 'custom_sku_update_process');

function custom_sku_update_process() {
if (isset($_POST['update_skus'])) {
    update_product_skus();
    echo '<div class="updated"><p>Product SKUs updated successfully!</p></div>';
 }
}

The problem is that it is not adding the correct zeros depending on the case. For example it is adding 3 zeros instead of 4 for the Example 2. Or for Example 1 it is adding 4 zeros in front instead of 5 zeros. Can someone help me out is it something with my regular expressions?

本文标签: phpHow to change product SKU39s in bulk with a plugin