/** * 1. Hàm lấy giá từ Envato API (giữ nguyên logic cache) */ function fetch_envato_price($item_id) { $api_token = 'KV16D3KIjNVOekZTA3SHl8lDs463eAt0'; // Thay Token của bạn $transient_key = 'tf_price_auto_' . $item_id; $cached_price = get_transient($transient_key); if ($cached_price !== false) return $cached_price; $response = wp_remote_get("https://api.envato.com/v3/market/catalog/item?id=$item_id", array( 'headers' => array('Authorization' => "Bearer $api_token") )); if (is_wp_error($response)) return false; $data = json_decode(wp_remote_retrieve_body($response), true); if (isset($data['price_cents'])) { $price = $data['price_cents'] / 100; set_transient($transient_key, $price, 12 * HOUR_IN_SECONDS); return $price; } return false; } /** * 2. Hàm trích xuất ID từ mô tả ngắn (Short Description) */ function get_tf_id_from_description($product) { // Lấy nội dung mô tả ngắn $short_desc = $product->get_short_description(); if (empty($short_desc)) return false; // Sử dụng Regex để tìm con số cuối cùng trong link themeforest.net // Tìm mẫu: themeforest.net/item/.../数字 if (preg_match('/themeforest\.net\/item\/[^\/]+\/(\d+)/', $short_desc, $matches)) { return $matches[1]; // Trả về ID (ví dụ: 53522229) } return false; } /** * 3. Ghi đè Regular Price và Sale Price (giảm 80%) */ add_filter('woocommerce_product_get_regular_price', 'auto_sync_tf_regular_price', 999, 2); add_filter('woocommerce_product_get_price', 'auto_sync_tf_sale_price', 999, 2); add_filter('woocommerce_product_get_sale_price', 'auto_sync_tf_sale_price', 999, 2); function auto_sync_tf_regular_price($price, $product) { $tf_id = get_tf_id_from_description($product); if ($tf_id) { $api_price = fetch_envato_price($tf_id); if ($api_price) return (string)$api_price; } return $price; } function auto_sync_tf_sale_price($price, $product) { $tf_id = get_tf_id_from_description($product); if ($tf_id) { $api_price = fetch_envato_price($tf_id); if ($api_price) return (string)($api_price * 0.2); // Giảm 80% } return $price; } // Bật badge Sale cho sản phẩm có link TF add_filter('woocommerce_product_is_on_sale', function($is_on_sale, $product) { if (get_tf_id_from_description($product)) return true; return $is_on_sale; }, 999, 2); LuxeNest - Furniture & Interior WooCommerce WordPress Theme - goodthemewp.com

LuxeNest – Furniture & Interior WooCommerce WordPress Theme

Original price was: $59.00.Current price is: $11.80.