yith-brand-migrator/migrate-script.php
2025-10-21 14:48:11 +08:00

68 lines
No EOL
2.4 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
define('WP_USE_THEMES', false);
require_once('/Users/feibisi-studio/Local Sites/mall/app/public/wp-load.php');
echo "=== YITH 品牌迁移脚本 ===\n\n";
global $wpdb;
echo "📊 迁移前数据统计:\n";
$yith_brands_before = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->terms} t
INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
WHERE tt.taxonomy = 'yith_product_brand'");
$native_brands_before = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->terms} t
INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
WHERE tt.taxonomy = 'product_brand'");
echo " YITH 品牌数量: " . ($yith_brands_before ?: 0) . "\n";
echo " 原生品牌数量: " . ($native_brands_before ?: 0) . "\n\n";
if ($yith_brands_before > 0) {
echo "🔄 开始迁移 YITH 品牌到原生品牌...\n";
if ($native_brands_before > 0) {
echo "⚠️ 警告:已存在 " . $native_brands_before . " 个原生品牌\n";
}
$wpdb->query('START TRANSACTION');
try {
$result = $wpdb->update(
$wpdb->term_taxonomy,
array('taxonomy' => 'product_brand'),
array('taxonomy' => 'yith_product_brand')
);
if ($result !== false) {
echo "✅ 成功迁移 " . $result . " 个品牌\n";
$wpdb->query('COMMIT');
wp_cache_flush();
if (function_exists('wc_delete_product_transients')) {
wc_delete_product_transients();
}
echo "✅ 缓存已清理\n";
$yith_brands_after = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'yith_product_brand'");
$native_brands_after = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'product_brand'");
echo "\n📊 迁移后数据统计:\n";
echo " YITH 品牌数量: " . ($yith_brands_after ?: 0) . "\n";
echo " 原生品牌数量: " . ($native_brands_after ?: 0) . "\n";
} else {
$wpdb->query('ROLLBACK');
echo "❌ 迁移失败\n";
}
} catch (Exception $e) {
$wpdb->query('ROLLBACK');
echo "❌ 迁移出错: " . $e->getMessage() . "\n";
}
} else {
echo " 未检测到 YITH 品牌数据,无需迁移\n";
}
echo "\n=== 迁移完成 ===\n";