在 WooCommerce 中,我有 100 个可变产品,其变体使用 custom attribute 来表示变体“体积”,值“50ml”。我想将值从“50ml”更改/更新为“60ml”。
这是我到目前为止的代码:
add_action('woocommerce_before_variations_form', 'p4wp_default_variation_value');
function p4wp_default_variation_value() {
global $product;
// Check if the product is a variable product
if ($product->is_type('variable')) {
$product_attributes = $product->get_attributes();
$attribute_value_to_find = '60ml';
// Get the variations with the specified attribute value
$variations = wc_get_products(array(
'type' => 'variation',
'status' => 'publish',
'limit' => -1,
'parent' => $product->get_id(),
'meta_query' => array(
array(
'key' => 'attribute_volume',
'value' => $attribute_value_to_find,
'compare' => '=',
),
),
));
// Loop through the variations and get their IDs
$variation_ids = array();
foreach ($variations as $variation) {
$variation_ids[] = $variation->get_id();
}
echo '<pre>';
print_r($variation_ids);
echo '</pre>';
// Check if there are attributes
if (count($product_attributes)) {
$product_attributes = get_post_meta($product->get_id(), '_product_attributes', true);
// Check if the 'volume' attribute exists
if (isset($product_attributes['volume'])) {
// Update the 'value' for '50ml' to '60ml'
$product_attributes['volume']['value'] = str_replace('60ml', '50ml', $product_attributes['volume']['value']);
// Update the '_product_attributes' meta field
update_post_meta($product->get_id(), '_product_attributes', $product_attributes);
//update_post_meta($variation_ids[0], 'attribute_volume', '60ml');
}
}
}
//Update post meta for post ID 3631
update_post_meta($variation_ids[0], 'attribute_volume', '50ml');
}
但是代码根本不起作用。我做错了什么?如何将属性值从“50ml”更改/更新为“60ml”以保持所有产品变体功能正常?
如有任何帮助,我们将不胜感激。
首先,始终进行数据库备份 (强制)。
以下未经测试代码应针对您的所有产品变体更新“体积”变体属性值,从“50ml”更新为“60ml”,以保持所有产品变体功能正常。
该代码是轻量级的,因为它对所有匹配产品上的数据库进行一到两次直接 SQL 更新查询,而不需要 PHP 参与多个读取/更新数据库查询的繁重流程。
该代码仅对管理员用户角色有效(请参阅最后的用法)。
该代码适用于全局产品属性和自定义产品属性。 请注意,全局产品属性分类始终以“pa_”(+属性段)开头。
add_action('init', 'update_variation_attribute_volume_value');
function update_variation_attribute_volume_value() {
global $wpdb, $current_user;
// Restricted to administrator user role
if ( ! in_array('administrator', $current_user->roles) ) return;
$attribute = 'volume'; // Here define the attribute taxonomy (start with "pa_") or the custom attribute (slug)
$name_from = '50ml'; // Here define the existing attribute term name or custom attribute name value
$slug_from = '50ml'; // Here define the existing attribute term slug or custom attribute name value
$name_to = '60ml'; // Here define the replacement attribute term name or custom attribute name value
$slug_to = '60ml'; // Here define the replacement attribute term slug or custom attribute name value
// Lightweight bulk update all product variations related variation attribute, and sync variable product title
$query = $wpdb->query( "
UPDATE {$wpdb->prefix}postmeta pm
INNER JOIN {$wpdb->prefix}posts p ON pm.post_id = p.ID
SET pm.meta_value = '{$slug_to}',
p.post_title = REPLACE(p.post_title, '{$name_from}', '{$name_to}'),
p.post_excerpt = REPLACE(p.post_excerpt, '{$name_from}', '{$name_to}')
WHERE pm.meta_key = 'attribute_{$attribute}'
AND pm.meta_value = '{$slug_from}'
AND p.post_type = 'product_variation'
" );
error_log( "Query (product variation)): \n" . print_r($query, true) ); // DEBUG
// For global attribute (taxonomy): Update the product attribute term
if ( taxonomy_exists($attribute) && term_exists($slug_from, $attribute) ) {
$term = get_term_by('slug',$slug_from, $attribute );
$update = wp_update_term( $term->term_id, $attribute, array('name' => $slug_to, 'slug' => $slug_to) ); // Update term
error_log( "Taxonomy term update: \n" . print_r($update, true) ); // DEBUG
}
// For custom attribute: Lightweight bulk update the variable product attribute
else {
$query2 = $wpdb->query( "
UPDATE {$wpdb->prefix}postmeta pm
INNER JOIN {$wpdb->prefix}posts p ON pm.post_id = p.ID
SET pm.meta_value = REPLACE(pm.meta_value, '{$slug_from}', '{$slug_to}')
WHERE pm.meta_key = '_product_attributes'
AND pm.meta_value LIKE '%{$slug_from}%'
AND p.post_type = 'product'
" );
error_log( "Query two (variable product)): \n" . print_r($query2, true) ); // DEBUG
}
}
代码位于子主题的functions.php 文件中(或插件中)。应该可以。
用法:浏览网站上的任何页面(前端或后端),然后删除代码。