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
版权声明:本文标题:php - How to change product SKU's in bulk with a plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736629862a1945753.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论