Last active
April 1, 2025 12:46
-
-
Save webantam43/ffbbcb021ca8654a675105059e1ebf60 to your computer and use it in GitHub Desktop.
Code tự động cập nhật giá khi tăng giảm số lượng ở trang sản phẩm Woocommerce
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
| /** | |
| * Tự động cập nhật giá khi tăng giảm số lượng ở trang sản phẩm Woocommerce webantam.com | |
| */ | |
| function add_quantity_price_update_script_pod_wat() { | |
| if (is_product()) { | |
| ?> | |
| <script type="text/javascript"> | |
| jQuery(document).ready(function($) { | |
| var originalPrice_pod_wat = 0; | |
| var pricePerUnit_pod_wat = 0; | |
| var priceUpdated_pod_wat = false; | |
| // Lưu trữ giá gốc khi trang tải | |
| function storeOriginalPrice_pod_wat() { | |
| var $priceElement = $('.summary .price .woocommerce-Price-amount, .entry-summary .price .amount').first(); | |
| if ($priceElement.length === 0) { | |
| $priceElement = $('[itemprop="price"], .price ins .amount, .summary .price'); | |
| } | |
| // Nếu đã lưu giá gốc rồi thì không lưu lại nữa | |
| if (originalPrice_pod_wat > 0) return; | |
| var priceHTML = $priceElement.html(); | |
| var priceText = priceHTML.replace(/<[^>]*>/g, ''); | |
| var matches = priceText.match(/[0-9.,]+/); | |
| if (matches && matches.length > 0) { | |
| pricePerUnit_pod_wat = parseFloat(matches[0].replace(/,/g, '.')); | |
| originalPrice_pod_wat = pricePerUnit_pod_wat; | |
| console.log("Đã lưu giá gốc: " + originalPrice_pod_wat); | |
| } | |
| } | |
| // Cập nhật hiển thị giá | |
| function updateDisplayPrice_pod_wat() { | |
| var $quantityInput = $('input.qty'); | |
| var quantity = parseInt($quantityInput.val()) || 1; | |
| if (quantity < 1) { | |
| quantity = 1; | |
| $quantityInput.val(1); | |
| } | |
| var $priceElement = $('.summary .price .woocommerce-Price-amount, .entry-summary .price .amount').first(); | |
| if ($priceElement.length === 0) { | |
| $priceElement = $('[itemprop="price"], .price ins .amount, .summary .price'); | |
| } | |
| var totalPrice = pricePerUnit_pod_wat * quantity; | |
| var priceHTML = $priceElement.html(); | |
| var updatedHTML = priceHTML.replace(/([0-9.,]+)/, totalPrice.toFixed(2)); | |
| $priceElement.html(updatedHTML); | |
| priceUpdated_pod_wat = true; | |
| console.log("Hiển thị giá cập nhật: " + totalPrice.toFixed(2) + " (Số lượng: " + quantity + ")"); | |
| } | |
| // Đặt lại giá gốc trước khi thêm vào giỏ hàng | |
| function resetPriceBeforeAddToCart_pod_wat() { | |
| $('form.cart').on('submit', function() { | |
| if (priceUpdated_pod_wat) { | |
| // Tạo input ẩn để gửi giá gốc | |
| if ($('input[name="original_price_pod_wat"]').length === 0) { | |
| $(this).append('<input type="hidden" name="original_price_pod_wat" value="' + pricePerUnit_pod_wat + '">'); | |
| } | |
| console.log("Đã đặt lại giá gốc trước khi thêm vào giỏ hàng: " + pricePerUnit_pod_wat); | |
| } | |
| }); | |
| // Xử lý nút AJAX add to cart | |
| $(document).on('click', '.ajax_add_to_cart', function() { | |
| if (priceUpdated_pod_wat) { | |
| var $form = $(this).closest('form'); | |
| if ($form.length && $('input[name="original_price_pod_wat"]').length === 0) { | |
| $form.append('<input type="hidden" name="original_price_pod_wat" value="' + pricePerUnit_pod_wat + '">'); | |
| } | |
| console.log("Đã đặt lại giá gốc cho AJAX add to cart: " + pricePerUnit_pod_wat); | |
| } | |
| }); | |
| } | |
| // Xử lý sự kiện khi thay đổi số lượng | |
| function handleQuantityChanges_pod_wat() { | |
| var $quantityInput = $('input.qty'); | |
| $quantityInput.on('change input', function() { | |
| updateDisplayPrice_pod_wat(); | |
| }); | |
| $('.plus, .minus, .quantity-up, .quantity-down').on('click', function() { | |
| setTimeout(updateDisplayPrice_pod_wat, 100); | |
| }); | |
| updateDisplayPrice_pod_wat(); | |
| } | |
| function init_pod_wat() { | |
| storeOriginalPrice_pod_wat(); | |
| handleQuantityChanges_pod_wat(); | |
| resetPriceBeforeAddToCart_pod_wat(); | |
| } | |
| init_pod_wat(); | |
| setTimeout(init_pod_wat, 500); | |
| }); | |
| </script> | |
| <?php | |
| } | |
| } | |
| add_action('wp_footer', 'add_quantity_price_update_script_pod_wat', 99); | |
| function use_original_price_for_cart_pod_wat($cart_item_data, $product_id) { | |
| if (isset($_POST['original_price_pod_wat']) && is_numeric($_POST['original_price_pod_wat'])) { | |
| $cart_item_data['original_price_pod_wat'] = floatval($_POST['original_price_pod_wat']); | |
| } | |
| return $cart_item_data; | |
| } | |
| add_filter('woocommerce_add_cart_item_data', 'use_original_price_for_cart_pod_wat', 10, 2); | |
| function apply_original_price_to_cart_item_pod_wat($cart_object) { | |
| if (is_admin() && !defined('DOING_AJAX')) return; | |
| foreach ($cart_object->get_cart() as $cart_item_key => $cart_item) { | |
| if (isset($cart_item['original_price_pod_wat'])) { | |
| $product = $cart_item['data']; | |
| $original_price = floatval($cart_item['original_price_pod_wat']); | |
| $product->set_price($original_price); | |
| } | |
| } | |
| } | |
| add_action('woocommerce_before_calculate_totals', 'apply_original_price_to_cart_item_pod_wat', 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment