Created
April 5, 2025 11:50
-
-
Save webantam43/d3707e7d3c13f587137b62c8ee73df75 to your computer and use it in GitHub Desktop.
Hook thay đổi layout plugin review WAT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Gỡ bỏ action hiển thị số lượng đã bán của plugin | |
| function remove_original_sold_quantity_display_review_woo_wat() { | |
| // Xóa cả class và function (phải thực hiện sau khi class đã được khởi tạo) | |
| global $wp_filter; | |
| // Xóa hook hiển thị ở trang chi tiết sản phẩm | |
| if (isset($wp_filter['woocommerce_single_product_summary'])) { | |
| foreach ($wp_filter['woocommerce_single_product_summary']->callbacks as $priority => $callbacks) { | |
| foreach ($callbacks as $key => $callback) { | |
| if (is_array($callback['function']) && $callback['function'][1] === 'display_sold_quantity_review_woo_wat') { | |
| remove_action('woocommerce_single_product_summary', $callback['function'], $priority); | |
| } | |
| } | |
| } | |
| } | |
| // Xóa hook hiển thị ở trang danh mục sản phẩm | |
| if (isset($wp_filter['woocommerce_after_shop_loop_item_title'])) { | |
| foreach ($wp_filter['woocommerce_after_shop_loop_item_title']->callbacks as $priority => $callbacks) { | |
| foreach ($callbacks as $key => $callback) { | |
| if (is_array($callback['function']) && $callback['function'][1] === 'display_sold_quantity_review_woo_wat') { | |
| remove_action('woocommerce_after_shop_loop_item_title', $callback['function'], $priority); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // Thực hiện sau khi tất cả plugins đã được load | |
| add_action('wp_loaded', 'remove_original_sold_quantity_display_review_woo_wat', 20); | |
| // Hàm lấy tổng số lượng đã bán (giống y hệt plugin) | |
| function get_custom_total_sold_quantity_review_woo_wat($product_id) { | |
| $options = get_option('review_woo_wat_sold_quantity_options_review_woo_wat'); | |
| $is_auto_update = isset($options['auto_update']) ? $options['auto_update'] : 1; | |
| // Nếu tắt chế độ tự động, ưu tiên số lượng nhập thủ công | |
| if (!$is_auto_update) { | |
| $manual_sold_quantity = get_post_meta($product_id, 'manual_sold_quantity_review_woo_wat', true); | |
| // Nếu có số lượng nhập thủ công, trả về số lượng đó | |
| if ($manual_sold_quantity !== '') { | |
| return intval($manual_sold_quantity); | |
| } | |
| } | |
| // Lấy số lượng đã bán tự động từ meta (đã được plugin cập nhật) | |
| $total_sold = get_post_meta($product_id, 'total_sold_quantity_review_woo_wat', true); | |
| // Nếu không có trong meta, tính toán từ đơn hàng | |
| if (!$total_sold) { | |
| // Sử dụng phương pháp mới với API của WooCommerce | |
| $args = [ | |
| 'status' => 'completed', | |
| 'type' => 'shop_order', | |
| 'limit' => -1 | |
| ]; | |
| $orders = wc_get_orders($args); | |
| $total_sold = 0; | |
| foreach ($orders as $order) { | |
| foreach ($order->get_items() as $item) { | |
| if ($item->get_product_id() == $product_id) { | |
| $total_sold += $item->get_quantity(); | |
| } | |
| } | |
| } | |
| } | |
| return intval($total_sold); | |
| } | |
| // Thêm hàm hiển thị mới | |
| function custom_display_sold_quantity_review_woo_wat() { | |
| global $product; | |
| if ($product) { | |
| // Sử dụng hàm lấy số lượng đã bán tương tự như plugin | |
| $sold_quantity = get_custom_total_sold_quantity_review_woo_wat($product->get_id()); | |
| // Chỉ hiển thị khi số lượng đã bán từ 1 trở lên | |
| if ($sold_quantity >= 1) { | |
| // Kiểm tra nếu đang ở trang danh mục sản phẩm | |
| if (is_shop() || is_product_category() || is_product_tag() || (function_exists('is_product_taxonomy') && is_product_taxonomy())) { | |
| // Hiển thị đơn giản cho trang danh mục | |
| echo '<div class="daban_sold_quantity_review_cmt_woo_wat" style="font-size: 0.9em;">' . | |
| sprintf('Đã bán %s', | |
| number_format_i18n($sold_quantity)) . | |
| '</div>'; | |
| } else { | |
| // Hiển thị đầy đủ cho trang chi tiết sản phẩm | |
| // Xác định mức độ hot dựa trên số lượng đã bán | |
| $sold_percentage = min(100, max(20, round($sold_quantity / 5) * 5)); // Đây chỉ là công thức mẫu | |
| // HTML với thanh tiến trình có chữ bên trong | |
| echo '<div class="daban_sold_quantity_review_cmt_woo_wat"> | |
| <div class="sold-progress-bar-review-woo-wat-container"> | |
| <div class="sold-progress-bar-review-woo-wat" style="width: ' . $sold_percentage . '%;"> | |
| <span class="sold-text-review-woo-wat">Đã bán ' . number_format_i18n($sold_quantity) . '</span> | |
| </div> | |
| </div> | |
| </div>'; | |
| } | |
| } | |
| } | |
| } | |
| // Thêm CSS vào header | |
| function add_sold_quantity_custom_css_review_woo_wat() { | |
| ?> | |
| <style> | |
| .single-product .daban_sold_quantity_review_cmt_woo_wat { | |
| position: relative; | |
| } | |
| .single-product .sold-progress-bar-review-woo-wat-container { | |
| height: 20px; | |
| background-color: #f0f0f0; | |
| border-radius: 12px; | |
| position: relative; | |
| overflow: hidden; | |
| } | |
| .single-product .sold-progress-bar-review-woo-wat { | |
| height: 100%; | |
| background: linear-gradient(90deg, #ff7a00, #ff5252); | |
| border-radius: 12px; | |
| position: relative; | |
| min-width: 120px; | |
| transition: width 1s ease; | |
| } | |
| .single-product .sold-text-review-woo-wat { | |
| position: absolute; | |
| left: 12px; | |
| top: 50%; | |
| transform: translateY(-50%); | |
| color: white; | |
| font-weight: 600; | |
| font-size: 13px; | |
| white-space: nowrap; | |
| text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2); | |
| } | |
| </style> | |
| <?php | |
| } | |
| add_action('wp_head', 'add_sold_quantity_custom_css_review_woo_wat'); | |
| // Thêm lại action với hàm mới | |
| add_action('woocommerce_single_product_summary', 'custom_display_sold_quantity_review_woo_wat', 8); | |
| add_action('woocommerce_after_shop_loop_item_title', 'custom_display_sold_quantity_review_woo_wat', 11); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment