Do not upload if there is a product with the same name in the database
-
I continue to use this great plugin and there may be some requirements.
When making an upload, if there is a product with the same name in the database, skip this product, how can I do this?
I found the code below but
I don’t know how to get the product name. What I want is for all products name to be checked before upload.function my_is_post_to_create( $continue_import, $data, $import_id ) { if ( $import_id == 53 ) { if ( $product = get_page_by_title( $data['product_name1'], OBJECT, 'product' ) ) { return false; } } return true; } add_filter( 'wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3 );
-
Hi @ibrahimpak,
I found the code below but I don’t know how to get the product name.
Do you mean that you don’t know how to get the product name from the import record inside your code? If so, can you send me the import element(s) that’s being imported into the “Title” field in your product import?
Hello,
Thanks for your answer.
Let’s say
<title>TITLE</title>
is the code then$product = get_page_by_title( $data['title'], OBJECT, 'product' ) )
In this way, if the product names in the file are in the database, will the uploaded not be done?
This is exactly what I want, if one of the product names in the file already exists, don’t load it.
- This reply was modified 2 years, 4 months ago by ibrahimpak.
- This reply was modified 2 years, 4 months ago by ibrahimpak.
I tried as above and it upload the previously uploaded products with the same name.
These are my import elements, for example, if in site there is already a product named “Omuz Fırfırlı Tshirt – Bordo”, how can I prevent it from uploaded?
<product> <productCode>AKDNBS2041</productCode> <main_category>Üst Giyim</main_category> <name> Omuz Fırfırlı Tshirt – Bordo </name> <description> Maken üzerinde beden : S Beden Renk : Bordo </description> <Price>59.99</Price> <quantity>240</quantity> </product>
- This reply was modified 2 years, 4 months ago by ibrahimpak.
I’m trying a code like below, but after run import I get the error, “Your server terminated the import process”.
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; }
Hi @ibrahimpak,
Since the {name[1]} element contains the title you want to search for, this should work:
function my_is_post_to_create( $continue_import, $data, $import_id ) { if ( $import_id == 53 ) { if ( $product = get_page_by_title( $data['name'], OBJECT, 'product' ) ) { return false; } } return true; } add_filter( 'wp_all_import_is_post_to_create', 'my_is_post_to_create', 10, 3 );
Hello,
Unfortunately it continues to uploaded with the same name.
This is the code I’m using, it contains both upload ids.
add_filter( 'wp_all_import_is_post_to_create', 'my_is_post_to_create_filter', 10, 3 ); function my_is_post_to_create_filter( $continue_import, $data, $import_id ) { if ( $import_id == 33 || $import_id == 34 ) { if ( $product = get_page_by_title( $data['name'], OBJECT, 'product' ) ) { return false; } } return true; }
This is the products xml file I use.
<products> <product> <productCode>AKDNBS2041</productCode> <main_category>Üst Giyim</main_category> <name> Dantel Nakış İşlemeli Pedli Bambu Kadın Büstiyer Krem Bordo </name> <description> 2 Adet Dantel Nakış İşlemeli Pedli Bambu Kadın Büstiyer Krem Bordo </description> <listPrice>102.99</listPrice> <quantity>240</quantity> </product> </products>
The code I am using is getting the product name from the install file. Does it get the product name in the database?
It’s really weird that it doesn’t work, everything seems fine but it keeps uploaded the product with the same name.
I guess the other code above just checks the names in the file.
What is actually needed is some code to check the database.
The code below is correct but I guess it only checks the custom field. How can I use this code to check the header?
function create_only_if_unique_custom_field( $continue_import, $data, $import_id ) { // Only run for import ID 1. if ($import_id == 1) { // The custom field to check. $key_to_look_up = "my_custom_field"; // The value to check where 'num' is the element name. $value_to_look_up = $data['num']; // Prepare the WP_Query arguments $args = array ( // Set the post type being imported. 'post_type' => array( 'post' ), // Check our custom field for our value. 'meta_query' => array(array( 'key' => $key_to_look_up, 'value' => $value_to_look_up, )), ); // Run the query and do not create post if custom // field value is duplicated. $query = new WP_Query( $args ); return !($query->have_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);
- This reply was modified 2 years, 3 months ago by ibrahimpak.
I edited the code as below and it is not working.
How can I get the product name field? Is “post title” correct?
add_filter('wp_all_import_is_post_to_create', 'create_only_if_unique_custom_field', 10, 3); function create_only_if_unique_custom_field( $continue_import, $data, $import_id ) { if ( $import_id == 33 || $import_id == 34 ) { $key = "post_title"; $value = $data['name']; $args = array ( 'post_type' => array( 'post' ), 'meta_query' => array(array( 'key' => $key, 'value' => $value, )), ); // Run the query and do not create post if custom field value is duplicated. $query = new WP_Query( $args ); return !($query->have_posts()); } else { // Take no action if a different import ID is running. return $continue_import; } }
After a long time, my problem was solved with a small and easy method. I leave the code here for those who need it.
Thanks to @wpallimport!
add_filter('wp_all_import_is_post_to_create', 'create_only_if_unique_custom_field', 10, 3); function create_only_if_unique_custom_field( $continue_import, $data, $import_id ) { // Only run for import ID 33 if ( $import_id == 33 ) { // 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; } }
- This reply was modified 2 years, 3 months ago by ibrahimpak.
- The topic ‘Do not upload if there is a product with the same name in the database’ is closed to new replies.