woocommerce - 删除与已删除属性关联的变体
问题描述
当我删除一个属性并保存它时,不会删除与该属性关联的变体。我真的很想自动删除与我已删除的属性相关的变体。
我的情况:
我有 Ship from 属性的三个值,我只想保留 China 。但是当我删除美国和欧洲时,不会删除与美国和欧洲相关的其他变体。
关于如何为我想要更改的每个产品执行此操作的任何想法。非常感谢 https://ibb.co/G0cc1cL
解决方案
将此函数添加到您的functions.php 文件中。
function kotsh_delete_variations_with_no_parent() {
$variations = new WP_Query( array(
'post_type' => 'product_variation',
'posts_per_page' => -1
) );
if ( $variations->have_posts() ) {
while ( $variations->have_posts() ) {
$variations->the_post();
$parent = wp_get_post_parent_id( get_the_id() );
if ( false === get_post_status( $parent ) ) {
wp_delete_post( get_the_id(), true );
}
}
}
wp_reset_postdata();
}
add_action( 'init', 'kotsh_delete_variations_with_no_parent', 10 );
推荐阅读
- sql - 将 GROUP BY 子查询嵌入到 GROUP BY 查询中
- visual-c++ - 如何在游戏c ++中读/写内存
- html - 如何为 data-src 设置自定义 CSS
- hadoop - 当一个节点在 Hadoop Cluster 中很慢?
- python - pymysql.err.Error:已经在 heroku 上的烧瓶中关闭
- javascript - 将道具传递给
或将隐藏数据传递给路由器但不使用参数 - c++ - 为什么我的成员变量在每次循环迭代结束时消失?
- android - 一个接一个地启动相同协程的实例
- php - 如何使用另一个变量的文本和值创建一个 php 变量
- python - Py2neo - 如何通过节点匹配获取节点 ID?