
Chuyển YITH Brands Add-On sang brand mặc định trong WooCommerce
Trong 1 bài viết trước đó tôi đã share code để chuyển đổi brand khi sử dụng WPC Brands for WooCommerce sang brand mặc định trong WooCommerce và trong bài này là 1 plugin brand khác đó là YITH WooCommerce Brands Add-On
Này sẽ là code hướng dẫn nhanh để bạn có thể áp dụng ngay cách để chuyển YITH Brands Add-On sang brand mặc định trong WooCommerce
- Login vào hosting của bạn
- Sao lưu database trước
- Truy cập vào thư mục gốc chứa website và tạo 1 file có tên là:
migrate-brands.php
- Dán code phía dưới vào và lưu lại
- Sau đó truy cập vào website của bạn theo dạng
https://websitecuaban.com/migrate-brands.php
- Check vào ô xác nhận bạn đã sao lưu cơ sở dữ liệu
- Nhấn nút “Bắt đầu chuyển đổi” rồi ngồi rung đùi tí là xong.
<?php
/**
* Script chuyển đổi YITH Brands sang WooCommerce Brands
*
* Cách sử dụng:
* 1. Tạo file PHP này trên máy tính của bạn
* 2. Tải lên thư mục gốc WordPress của bạn
* 3. Chạy script bằng cách truy cập URL: https://phuhoangthinh.com/migrate-brands.php
* 4. Xóa file sau khi hoàn thành
*/
// Kiểm tra bảo mật
if (!defined('ABSPATH')) {
// Định nghĩa ABSPATH nếu script được chạy trực tiếp
define('ABSPATH', dirname(__FILE__) . '/');
// Tải WordPress Core
require_once(ABSPATH . 'wp-load.php');
// Kiểm tra quyền admin
if (!current_user_can('manage_options')) {
die('Bạn không có quyền thực hiện thao tác này.');
}
}
// Hàm chính để di chuyển brands
function migrate_yith_to_woo_brands() {
// Lưu log
$log = [];
$log[] = "Bắt đầu di chuyển dữ liệu...";
// Kiểm tra taxonomy nguồn và đích
$yith_taxonomy = 'yith_product_brand';
$woo_taxonomy = 'product_brand';
if (!taxonomy_exists($yith_taxonomy)) {
$log[] = "Lỗi: Taxonomy YITH ($yith_taxonomy) không tồn tại.";
return $log;
}
if (!taxonomy_exists($woo_taxonomy)) {
$log[] = "Lỗi: Taxonomy WooCommerce ($woo_taxonomy) không tồn tại.";
return $log;
}
// Lấy tất cả thương hiệu YITH
$yith_brands = get_terms([
'taxonomy' => $yith_taxonomy,
'hide_empty' => false,
]);
if (is_wp_error($yith_brands)) {
$log[] = "Lỗi: " . $yith_brands->get_error_message();
return $log;
}
if (empty($yith_brands)) {
$log[] = "Không tìm thấy thương hiệu nào từ YITH.";
return $log;
}
$log[] = "Tìm thấy " . count($yith_brands) . " thương hiệu cần chuyển đổi.";
// Di chuyển từng thương hiệu
foreach ($yith_brands as $brand) {
$log[] = "Đang xử lý thương hiệu: " . $brand->name;
// Kiểm tra xem thương hiệu đã tồn tại trong WooCommerce chưa
$existing_term = term_exists($brand->name, $woo_taxonomy);
if ($existing_term) {
$log[] = "- Thương hiệu đã tồn tại trong WooCommerce, bỏ qua.";
continue;
}
// Tạo thương hiệu mới trong WooCommerce
$new_term = wp_insert_term(
$brand->name,
$woo_taxonomy,
[
'description' => $brand->description,
'slug' => $brand->slug,
]
);
if (is_wp_error($new_term)) {
$log[] = "- Lỗi: Không thể tạo thương hiệu: " . $new_term->get_error_message();
continue;
}
$new_term_id = $new_term['term_id'];
$log[] = "- Đã tạo thương hiệu mới với ID: " . $new_term_id;
// Di chuyển metadata
$thumbnail_id = get_term_meta($brand->term_id, 'thumbnail_id', true);
if ($thumbnail_id) {
update_term_meta($new_term_id, 'thumbnail_id', $thumbnail_id);
$log[] = "- Đã chuyển hình ảnh thumbnail.";
}
// Di chuyển URL (nếu có)
$brand_url = get_term_meta($brand->term_id, 'url', true);
if ($brand_url) {
update_term_meta($new_term_id, 'url', $brand_url);
$log[] = "- Đã chuyển URL.";
}
// Các meta khác mà YITH có thể sử dụng
$meta_keys = ['banner_id', 'description', 'custom_meta'];
foreach ($meta_keys as $key) {
$value = get_term_meta($brand->term_id, $key, true);
if ($value) {
update_term_meta($new_term_id, $key, $value);
$log[] = "- Đã chuyển metadata: " . $key;
}
}
// Di chuyển sản phẩm
$products = get_posts([
'post_type' => 'product',
'numberposts' => -1,
'tax_query' => [
[
'taxonomy' => $yith_taxonomy,
'field' => 'term_id',
'terms' => $brand->term_id,
],
],
'fields' => 'ids',
]);
if (!empty($products)) {
foreach ($products as $product_id) {
wp_set_object_terms($product_id, $new_term_id, $woo_taxonomy, true);
}
$log[] = "- Đã chuyển " . count($products) . " sản phẩm sang thương hiệu mới.";
} else {
$log[] = "- Không có sản phẩm nào thuộc thương hiệu này.";
}
}
$log[] = "Hoàn tất quá trình chuyển đổi!";
return $log;
}
// Chạy khi truy cập trực tiếp
if (!defined('DOING_AJAX') && basename($_SERVER['SCRIPT_FILENAME']) == basename(__FILE__)) {
// Hiển thị trang HTML
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Chuyển đổi Thương hiệu</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #23282d;
}
.result {
background: #f8f9fa;
padding: 15px;
border: 1px solid #ddd;
margin-top: 20px;
max-height: 400px;
overflow-y: auto;
}
.result p {
margin: 5px 0;
line-height: 1.5;
}
.success {
color: green;
}
.error {
color: red;
}
</style>
</head>
<body>
<h1>Chuyển đổi từ YITH Brands sang WooCommerce Brands</h1>
<?php if (isset($_POST['migrate']) && $_POST['migrate'] === 'yes'): ?>
<div class="result">
<h3>Kết quả chuyển đổi:</h3>
<?php
$results = migrate_yith_to_woo_brands();
foreach ($results as $line) {
if (strpos($line, 'Lỗi:') !== false) {
echo '<p class="error">' . esc_html($line) . '</p>';
} elseif (strpos($line, 'Hoàn tất') !== false) {
echo '<p class="success">' . esc_html($line) . '</p>';
} else {
echo '<p>' . esc_html($line) . '</p>';
}
}
?>
</div>
<p><a href="<?php echo esc_url($_SERVER['PHP_SELF']); ?>">Quay lại</a></p>
<?php else: ?>
<p>Công cụ này sẽ chuyển đổi tất cả thương hiệu từ YITH WooCommerce Brands Add-On sang WooCommerce Brands.</p>
<p><strong>Lưu ý:</strong> Vui lòng sao lưu cơ sở dữ liệu trước khi tiếp tục.</p>
<form method="post">
<p>
<label>
<input type="checkbox" name="backup" required>
Tôi đã sao lưu cơ sở dữ liệu
</label>
</p>
<p>
<input type="hidden" name="migrate" value="yes">
<button type="submit">Bắt đầu chuyển đổi</button>
</p>
</form>
<?php endif; ?>
<p><em>Sau khi hoàn tất nên xóa file này khỏi server để đảm bảo bảo mật.</em></p>
</body>
</html>
<?php
exit;
}
Chuyển YITH Brands Add-On sang brand mặc định trong WooCommerce đơn với vài bước ở trên thôi.
Hy vọng code sẽ giúp ích được cho nhiều người.