Wordpress &引用;用于变体“;以编程方式切换
有860多种不同的产品。任务是允许用户批量编辑某些属性的“用于变体”字段。例如,我们有尺寸、性别,这仍然是变体所需的,但我们也有材料,这仅是过滤器所需,因此删除此属性不是一个解决方案。完全未经测试,如果您有很多产品,这不是特别理想,因为它可能会超时,但从理论上讲,您可以遍历所有可变产品,并更新Wordpress &引用;用于变体“;以编程方式切换,wordpress,woocommerce,Wordpress,Woocommerce,有860多种不同的产品。任务是允许用户批量编辑某些属性的“用于变体”字段。例如,我们有尺寸、性别,这仍然是变体所需的,但我们也有材料,这仅是过滤器所需,因此删除此属性不是一个解决方案。完全未经测试,如果您有很多产品,这不是特别理想,因为它可能会超时,但从理论上讲,您可以遍历所有可变产品,并更新\u product\u属性元值。我强烈建议您进行数据备份和/或这是一个临时站点 function so_41978670_run_once(){ $args = array( 'p
\u product\u属性
元值。我强烈建议您进行数据备份和/或这是一个临时站点
function so_41978670_run_once(){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'post_type',
'field' => 'slug',
'terms' => 'variable',
),
),
);
$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 );
$attributes = $product->get_attributes();
$meta_values = array();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attributes meta.
$attributes[ $attribute_key ]['is_variation'] = 1;
}
}
}
update_post_meta( $product->get_id(), '_product_attributes', $attributes );
}
wp_reset_postdata();
}
}
WooCommerce 3.0 CRUD功能的潜在更新,但尚未测试:
function so_41978670_run_once() {
$args = array(
'limit' => -1,
'type' => 'variable',
);
$products = wc_get_products( $args );
if( ! empty ( $products ) ) {
foreach ( $products as $product ) {
$attributes = $product->get_attributes();
if ( $attributes ) {
foreach ( $attributes as $attribute_key => $attribute ) {
if( in_array( $attribute_key, array( 'size', 'gender' ) ) ){
// Modify the attribute settings.
$attribute->set_variation( true );
}
}
}
$product->set_attributes( $attributes );
$product->save();
}
}
}
add_action( 'admin_init', 'so_41978670_run_once' );
在“每页帖子”=>-1需要逗号,并且不起作用?好主意。谢谢你的好球。如前所述,它未经测试。如果再这样做,我可能会使用CRUD方法与attributes.CRUD进行接口。你是说从数据库mysql?是的,我尝试在init上运行此函数,但没有任何更改:(不,我的意思是使用WooCommerce 3.0中添加的产品getter和setter。我不再尝试使用
update\u post\u meta()
。我会使用wc\u get\u products()
,然后循环返回的产品对象……我会使用$product->save())
。这让我们担心元密钥和数据的格式。请检查我的编辑,但不幸的是…它仍然未经测试。