php WooCommerce/Wp全部导入如果要上传的产品名称已经存在于数据库中,则跳过安装

roejwanj  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(81)

我希望它跳过产品上传,如果产品名称已经存在于数据库中。我有这样一个代码,但在这种方式上传不启动,并给出一个错误。

add_filter( 'wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3 );
  function my_is_post_to_create( $continue_import, $data, $import_id ) {
     global $product;
     $product = wc_get_product(get_the_ID());
     $current_title = $product->get_name();
     $new_title = $data['name'];
     if ( $import_id == 34 ) {
        if ( $new_title == $current_title ) {
            return false;
        }
    }
    return true;
}
xytpbqjk

xytpbqjk1#

我想这应该行得通:

function create_only_if_unique_custom_field( $continue_import, $data, $import_id ) { 
    // Only run for import ID 34
    if ( $import_id == 34 ) {
    
    // Xml file column product title
    $value = $data['name'];
        
    // Get check on wpdb product title
    $posts = get_posts([
    'post_type'  => 'product',
    'title' => $value,  
  ]);
    return !$posts;

    } else {
       // Take no action if a different import ID is running.
       return $continue_import;
    }
}
add_filter('wp_all_import_is_post_to_create','create_only_if_unique_custom_field', 10, 3);

相关问题