Wordpress Woocommerce wp->insert Keep Give Duplicates



我使用以下平台Woocommerce - 4.9.0版本WordPress - Version 5.6

我从外部源SQL服务器获得wp_posts,我连接得很好没有问题,我遇到的问题是,当我从外部源导入WordPress时,我得到重复,需要帮助来整理

<?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 successfullyn <br/>";

}
// else {
//                die(sqlsrv_errors());
//            }
}
sqlsrv_close($conn);
header("Location: http://website header");
die();

?>

为什么一直在复制?

编辑我稍微修改了一下脚本,如下

<?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();

?>

这对我来说是有效的,现在唯一的问题是,如果SQL服务器上有1000个产品,并且我导入WordPress, WordPress统计有1001个产品,它会不断添加额外的产品示例。

我想保留post元数据,因为这是存储图像,类别和SKU的地方

p。当我运行这个脚本时,它会提示有重复的主键

你没有检查帖子是否已经存在,所以你可能会陷入无限循环。

检查文章标题应该足够了

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

了解更多
  • https://wordpress.stackexchange.com/a/112650/190376

最新更新