我正在寻找一种方法,该方法是通过过滤变体标题中的短语来删除产品变体。
我使用此帖子过滤器:
case 'trash_unwanted_products':
$args = array(
'post_type' => array('product','product_variation'),
'post_status' => array('publish','draft'),
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'fields' => 'ids',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'asc',
);
break;
这是我设置的帖子垃圾回收功能:
private function trash_unwanted_products($product) {
if (!$product->exists()) {
return false;
}
$productName = $product->get_name();
$productName_slug = sanitize_title($productName);
$productName_dash = sanitize_title_with_dashes($productName);
$productSKU = $product->get_sku();
if ($product->is_type( 'variable' )) {
$variation = new WC_Product_Variation($variation_id);
$title_slug = current($variation->get_variation_attributes());
$results = $wpdb->get_results("SELECT * FROM wp_terms WHERE slug = '{$title_slug}'", ARRAY_A);
$variationName = $results[0]['name'];
$variationName_slug = sanitize_title($variationName);
$variationName_dash = sanitize_title_with_dashes($variationName);
}
if (strpos($productName_slug,'ubrus')!==false && strpos($productName_slug,'vanocni')!==false) {
$this->trash_product($product);
}
if ($product->is_type( 'variable' ) && strpos($variationName_slug,'latka')!==false && strpos($variationName_dash,'cena-za-1-bezny-metr')!==false) {
$this->wp_delete_post($variation->get_id(), true);
}
}
private function trash_product($product) {
wp_delete_post($product->get_id(), true);
}
但是,我不确定该脚本是否会遍历每个变量产品中的所有变体,因此是否可以删除目标产品变体。
有什么建议吗?
答案 0 :(得分:0)
我已经开发了自己的变体过滤器,但还没有机会对其进行测试。
case 'trash_unwanted_variations':
$args = array(
'post_type' => 'product_variation',
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'fields' => 'ids',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'asc',
's' => get_search_query(),
'title' => array(
array(
'value' => sanitize_title_with_dashes('cena-za-1-bezny-metr'),
'compare' => 'IN',
)
),
);
break;
这也许可以解决问题? (如果要工作):D ..