我創建了wordpress插件,添加了csv自定義文件中的新產品和類別。它工作正常,在time_limit(60s)我的腳本添加4500產品'wp_posts'和約90k行'wp_postmeta'。Woocommerce插件 - 更新股票
現在,當我嘗試從csv更新(UPDATE或DELETE + INSERT)庫存這個產品後,約1k行腳本停止(超時)。
你知道有更好的方法(比$ wpdb-> query和php循環更好)來更新這個股票嗎?
if (isset($_POST['wool_stock_import_form_submitted'])) {
$hidden_field = esc_html($_POST['woostol_stock_import_form_submitted']);
if ($hidden_field == 'Y') {
// Prepare csv file
$addlist = $_FILES['addlist'];
$addlist_tmp_name = $_FILES['addlist']['tmp_name'];
$addlist_name = $_FILES['addlist']['name'];
$addlist_size = $_FILES['addlist']['size'];
$addlist_type = $_FILES['addlist']['type'];
$addlist_error = $_FILES['addlist']['error'];
$addlist_array = array();
}
if ($_POST['submit']){
$row = 1;
if (($handle_addlist = fopen($addlist_tmp_name, "r")) !== FALSE) {
while (($data = fgetcsv($handle_addlist, 1000, ";")) !== FALSE) {
$row++;
$addlist_array_tmp = array();
for ($i=0; $i < 99; $i++) {
if(!empty($data[$i])){
array_push($addlist_array_tmp, $data[$i]);
}
}
array_push($addlist_array, $addlist_array_tmp);
}
fclose($handle_addlist);
}
}
foreach ($addlist_array as $stock) {
$results = $wpdb->get_var($wpdb->prepare(
"
SELECT post_id
FROM wp_postmeta
WHERE meta_value = %s
",
$stock[0]
));
/* $wpdb->update(
'wp_postmeta',
array(
'meta_value' => $stock[2]
),
array(
'post_id' => $results,
'meta_key' => '_stock'
),
array(
'%s',
),
array('%d','%s')
); */
if (!empty($results)) {
$wpdb->query(
$wpdb->prepare(
"
DELETE FROM wp_postmeta
WHERE post_id = %d
AND meta_key = %s
",
$results, '_stock'
)
);
$wpdb->insert(
'wp_postmeta',
array(
'post_id' => $results,
'meta_key' => '_stock'
),
array(
'%d',
'%s'
)
);
$wpdb->show_errors();
echo 'blabla ' . $stock[0] . ' OK ' . date('Y-m-d H:i:s') . '<br>';
} else {
echo 'blabla ' . $stock[0] . ' ' . date('Y-m-d H:i:s') . '<br>';
}
}
}