Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

php - Wordpress Woocommerce wp->insert keeps give duplicates

I use the following Platforms Woocommerce - Version 4.9.0 WordPress - Version 5.6

I'm getting wp_posts from an external source SQL server I connect fine no problem, the problem I have is when I import from the external source to WordPress I get duplicates and need help to sort it out

<?php
    require_once('../../../wp-load.php');
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
    // Connect test database
    $serverName = "sqlserver"; //c6140c606f53.sn.mynetname.netPALLADIUMSQL,1435 
      $connectionOptions = array("Database"=>"database",  
                                  "UID"=>"user", 
                                  "PWD"=>"password");  
    $conn = sqlsrv_connect($serverName, $connectionOptions);  
    // Check connection
    if($conn == false) {
      echo "failed";
      die(sqlsrv_errors());
    }
/* Begin the transaction. 
if ( sqlsrv_begin_transaction( $conn ) === false ) {
     die( print_r( sqlsrv_errors(), true ));
}*/
    // echo "Connected successfully";
    $sql = "SELECT * FROM dbo.tbl'";
    $sql_products = sqlsrv_query($conn, $sql);

    global $wpdb;
    $where = "WHERE ID = ".$row['intProductId']."";
    $wherre = "WHERE post_id = ".$row['intProductId']."";
        while($row = sqlsrv_fetch_array($sql_products, SQLSRV_FETCH_ASSOC)) {
        $wpdb->insert(''. $wpdb->prefix . 'posts', array('post_title' => $row['strDesc'], 'post_type' => 'product'));
        $product_id = $wpdb->insert_id;
        $wpdb->insert(''. $wpdb->prefix . 'post_meta', array('post_id' => $row['intProductId'], 'meta_key' => '_sku','meta_value' => $row['strPartNumber'])); 
            
        if (sqlsrv_query($conn, $sql)) {
               //echo "Name:".$row['strDesc']." - SKU: ". $row['strPartNumber']. " - Product ID: ".$row['intProductId']."<br/>";
               ///echo " - Inserted successfully
 <br/>";
         
}
// else {
//                die(sqlsrv_errors());
//            }
    }

    sqlsrv_close($conn);

    header("Location: http://website header");
    die();
    
?>

Why is duplicating the whole time?

EDITED OK SO I CHANGED THE SCRIPT A BIT TO THE FOLLOOWING

<?php
    require_once('../../../wp-load.php');
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
     // Connect Palladium database
      require_once('inc/db.php');
      
    $conn = sqlsrv_connect($serverName, $connectionOptions);  
    // Check connection
    if($conn == false) {
      echo "failed";
      die(sqlsrv_errors());
    }
    // echo "Connected successfully";
    $sql = "SELECT * FROM dbo.tblInv";
    $sql_products = sqlsrv_query($conn, $sql);

    global $wpdb;
    $woo_products = $wpdb->get_results("SELECT * FROM ". $wpdb->prefix . "posts WHERE post_type = 'product'");
    //foreach ($woo_products as $woo_product) {
        //$wpdb->delete(''. $wpdb->prefix . 'postmeta', array('post_id' => $woo_product->ID));
    //}
    $wpdb->delete(''. $wpdb->prefix . 'posts', array('post_type' => 'product'));
    while($row = sqlsrv_fetch_array($sql_products, SQLSRV_FETCH_ASSOC)) {
        // echo $row['name'];
        if (!empty ($row['intProductId'])){
        $wpdb->insert(''. $wpdb->prefix . 'posts', array('post_title' => $row['strDesc'], 'post_content' => $row['strExtendedDesc'], 'post_type' => 'product', 'ID' => $row['intProductId'] ));
        
        if ( ! add_post_meta( $row['intProductId'], '_sku', $row['strPartNumber'], true ) ) { 
   update_post_meta ( $row['intProductId'], '_sku', $row['strPartNumber']);
        }
        } elseif(!empty ($row['strPartNumber']) && empty($row['intProductId'])) {
        $wpdb->insert(''. $wpdb->prefix . 'posts', array('post_title' => $row['strDesc'], 'post_content' => $row['strExtendedDesc'], 'post_type' => 'product' ));
        $post_id = $wpdb->insert_id;
        if ( ! add_post_meta( $post_id, '_sku', $row['strPartNumber'], true ) ) { 
   update_post_meta ( $post_id, '_sku', $row['strPartNumber']);
    }
    }
}

    sqlsrv_close($conn);

    header("Location: https://localhost/wp-admin/admin.php?page=product-manage");
    die();
    
?>

this works for me, the only problem now is that it keeps adding an extra product example if there is 1000 on SQL server and I import to WordPress, WordPress stats that there are 1001 products.

I would like to keep the post meta as this is where the images, categories and SKU's is stored

P.S when I run the script it brings up the errors that there is the duplicate primary key

question from:https://stackoverflow.com/questions/65847082/wordpress-woocommerce-wp-insert-keeps-give-duplicates

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're not checking if the post already exist or not, so you're probably getting stuck in an infinite loop.

Checking for the post title should be enough

<?php
// ... while
if( ! get_page_by_title( $row['strDesc'] ) ) { 
  // ... your loop content
}; ?>

Learn more


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...