Woocommerce SKU 生成器:为所有变体生成与父 sku 相同的 sku
Woocommerce SKU Generator: Generate a sku for all variations same as parent sku
我需要一个简单的功能,允许我添加或替换空 sku 代码,以便在我发布的产品中使用产品的父 sku 进行变体。
有没有办法获得这个作品?
已更新:要用父 SKU (可变产品 SKU) 替换已发布产品变体的空 SKU 代码,请尝试此功能仅适用于管理员用户角色。
Do a database backup before runing this
要运行此功能,浏览您的店铺页面:
- 每次更新2500个空变体SKU。
- 一条消息将显示更新版本的数量。
- 每次您都需要重新加载页面以处理 2000 个变体 SKU。,
当所有变体 SKU 都将被更新时,一条消息会让您知道工作已完成。
代码:
add_action('woocommerce_before_main_content', 'update_variations_sku', 100 );
function update_variations_sku(){
global $wpdb;
$results = $results2 = array();
$limit = 2500; // Number of variations to process
// The SQL query (get the variations with empty sku and their parent product ID)
$query = $wpdb->get_results( "
SELECT p.ID, p.post_parent
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product_variation'
AND p.post_status LIKE 'publish'
AND pm.meta_key LIKE '_sku'
LIMIT $limit
" );
if( count($query) == 0 ){
echo "<p><pre>The job is finished, you can remove the code</pre></p>";
return; // exit
}
// Loop through variation Ids and get the parent variable product ID
foreach( $query as $value )
$results[$value->post_parent][] = $value->ID;
// Loop through variation Ids and set the parent sku in related empty variations skus
foreach($results as $parent_id => $variation_ids){
$parent_sku = get_post_meta( $parent_id, '_sku', true );
$count = 0;
foreach($variation_ids as $variation_id){
$count++;
update_post_meta( $variation_id, '_sku', $parent_sku.'-'.$count );
}
}
if( count($query) < $limit ){
echo "<p><pre>The job is finished, you can remove the code <br>$count variations SKUs have been updated</pre></p>";
} else {
echo "<p><pre>$count variations SKUs have been updated (continue reloading the page again)</pre></p>";
}
}
代码进入您活跃的子主题(或主题)的 function.php 文件。
已测试并有效。
我需要一个简单的功能,允许我添加或替换空 sku 代码,以便在我发布的产品中使用产品的父 sku 进行变体。
有没有办法获得这个作品?
已更新:要用父 SKU (可变产品 SKU) 替换已发布产品变体的空 SKU 代码,请尝试此功能仅适用于管理员用户角色。
Do a database backup before runing this
要运行此功能,浏览您的店铺页面:
- 每次更新2500个空变体SKU。
- 一条消息将显示更新版本的数量。
- 每次您都需要重新加载页面以处理 2000 个变体 SKU。,
当所有变体 SKU 都将被更新时,一条消息会让您知道工作已完成。
代码:
add_action('woocommerce_before_main_content', 'update_variations_sku', 100 );
function update_variations_sku(){
global $wpdb;
$results = $results2 = array();
$limit = 2500; // Number of variations to process
// The SQL query (get the variations with empty sku and their parent product ID)
$query = $wpdb->get_results( "
SELECT p.ID, p.post_parent
FROM {$wpdb->prefix}posts as p
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
WHERE p.post_type LIKE 'product_variation'
AND p.post_status LIKE 'publish'
AND pm.meta_key LIKE '_sku'
LIMIT $limit
" );
if( count($query) == 0 ){
echo "<p><pre>The job is finished, you can remove the code</pre></p>";
return; // exit
}
// Loop through variation Ids and get the parent variable product ID
foreach( $query as $value )
$results[$value->post_parent][] = $value->ID;
// Loop through variation Ids and set the parent sku in related empty variations skus
foreach($results as $parent_id => $variation_ids){
$parent_sku = get_post_meta( $parent_id, '_sku', true );
$count = 0;
foreach($variation_ids as $variation_id){
$count++;
update_post_meta( $variation_id, '_sku', $parent_sku.'-'.$count );
}
}
if( count($query) < $limit ){
echo "<p><pre>The job is finished, you can remove the code <br>$count variations SKUs have been updated</pre></p>";
} else {
echo "<p><pre>$count variations SKUs have been updated (continue reloading the page again)</pre></p>";
}
}
代码进入您活跃的子主题(或主题)的 function.php 文件。
已测试并有效。