
Dưới đây là 15 chức năng nâng cao giúp trang giỏ hàng WooCommerce trong wordpress của bạn trở nên chuyên nghiệp hơn, bạn cần thực hiện chức năng nào thì có thể lấy bỏ vào funtion.php nhé
1. Hiển thị phí vận chuyển ước tính ngay trong giỏ hàng
Cho phép khách hàng xem phí vận chuyển trước khi thanh toán.
| function show_shipping_cost_in_cart() { if (WC()->cart->needs_shipping()) { $packages = WC()->shipping->get_packages(); foreach ($packages as $i => $package) { $rates = WC()->shipping->calculate_shipping_for_package($package)['rates']; if (!empty($rates)) { foreach ($rates as $rate) { echo '<p>Phí vận chuyển: ' . $rate->get_label() . ' - ' . wc_price($rate->get_cost()) . '</p>'; } } } } } add_action('woocommerce_cart_totals_before_order_total', 'show_shipping_cost_in_cart'); |
2. Hiển thị hình ảnh sản phẩm trong giỏ hàng
| function add_product_thumbnail_in_cart($product_name, $cart_item, $cart_item_key) { $thumbnail = get_the_post_thumbnail($cart_item['product_id'], 'thumbnail'); return $thumbnail . ' ' . $product_name; } add_filter('woocommerce_cart_item_name', 'add_product_thumbnail_in_cart', 10, 3); |
3. Áp dụng mã giảm giá tự động khi đơn hàng đạt mức tối thiểu
| function apply_coupon_when_reach_threshold() { if (WC()->cart->subtotal >= 1000000 && !WC()->cart->has_discount('SALE100')) { WC()->cart->apply_coupon('SALE100'); wc_add_notice('Bạn đã được áp dụng mã giảm giá SALE100!', 'success'); } } add_action('woocommerce_before_cart', 'apply_coupon_when_reach_threshold'); |
4. Thêm nút “Lưu giỏ hàng” để mua sau
| function add_save_cart_button() { echo '<button type="button" class="button" id="save-cart">Lưu giỏ hàng</button>'; ?> <script> document.getElementById("save-cart").addEventListener("click", function() { alert("Giỏ hàng đã được lưu!"); }); </script> <?php } add_action('woocommerce_cart_actions', 'add_save_cart_button'); |
5. Nhắc nhở khách hàng về sản phẩm sắp hết hàng trong giỏ hàng
| function alert_low_stock_items() { foreach (WC()->cart->get_cart() as $cart_item) { $product = wc_get_product($cart_item['product_id']); if ($product->is_in_stock() && $product->get_stock_quantity() <= 5) { wc_add_notice('Sản phẩm "' . $product->get_name() . '" chỉ còn ' . $product->get_stock_quantity() . ' cái, hãy đặt hàng sớm!', 'notice'); } } } add_action('woocommerce_check_cart_items', 'alert_low_stock_items'); |
6. Cho phép khách hàng chọn ngày giao hàng
| function add_delivery_date_field() { echo '<p><label for="delivery_date">Chọn ngày giao hàng:</label>'; echo '<input type="date" name="delivery_date" required></p>'; } add_action('woocommerce_after_order_notes', 'add_delivery_date_field'); |
7. Hiển thị số tiền cần thêm để đạt mức giảm giá tiếp theo
| function show_next_discount_threshold() { $cart_total = WC()->cart->subtotal; $threshold = 2000000; // Ngưỡng giảm giá tiếp theo if ($cart_total < $threshold) { echo '<p>Còn ' . wc_price($threshold - $cart_total) . ' nữa để nhận ưu đãi tiếp theo!</p>'; } } add_action('woocommerce_before_cart', 'show_next_discount_threshold'); |
8. Tích hợp Google Analytics để theo dõi giỏ hàng
| function track_cart_with_google_analytics() { ?> <script> window.dataLayer = window.dataLayer || []; window.dataLayer.push({ 'event': 'cart_view', 'cart_total': <?php echo WC()->cart->total; ?> }); </script> <?php } add_action('wp_footer', 'track_cart_with_google_analytics'); |
9. Tự động xóa sản phẩm hết hàng khỏi giỏ hàng
| function remove_out_of_stock_products_from_cart() { foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { if (!wc_get_product($cart_item['product_id'])->is_in_stock()) { WC()->cart->remove_cart_item($cart_item_key); } } } add_action('woocommerce_before_calculate_totals', 'remove_out_of_stock_products_from_cart'); |
10. Thêm gợi ý sản phẩm liên quan trong giỏ hàng
| function suggest_related_products_in_cart() { $cart_items = WC()->cart->get_cart(); if (!empty($cart_items)) { echo '<h3>Sản phẩm gợi ý</h3>'; echo do_shortcode('[products limit="4" columns="4" orderby="rand"]'); } } add_action('woocommerce_after_cart_table', 'suggest_related_products_in_cart'); |
11. Thêm chức năng tặng quà cho đơn hàng đạt mức tối thiểu
| function add_free_gift_to_cart() { $gift_id = 123; // ID của sản phẩm quà tặng if (WC()->cart->subtotal >= 500000 && !WC()->cart->find_product_in_cart($gift_id)) { WC()->cart->add_to_cart($gift_id); } } add_action('woocommerce_before_calculate_totals', 'add_free_gift_to_cart'); |
12. Hiển thị đồng hồ đếm ngược cho chương trình khuyến mãi trong giỏ hàng
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function show_promotion_countdown() { echo '<p>Khuyến mãi kết thúc sau: <span id="countdown"></span></p>'; ?> <script> var countDownDate = new Date("Mar 15, 2025 23:59:59").getTime(); var x = setInterval(function() { var now = new Date().getTime(); var distance = countDownDate - now; var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); document.getElementById("countdown").innerHTML = hours + "h " + minutes + "m " + seconds + "s "; }, 1000); </script> <?php } add_action('woocommerce_before_cart', 'show_promotion_countdown'); |
13. Thêm lựa chọn gói quà tặng cho đơn hàng
| function add_gift_wrap_option() { woocommerce_form_field('gift_wrap', array( 'type' => 'checkbox', 'label' => 'Gói quà (+20.000đ)', ), WC()->session->get('gift_wrap')); } add_action('woocommerce_after_cart_table', 'add_gift_wrap_option'); |
14. Đề xuất nâng cấp sản phẩm trong giỏ hàng
| function upsell_product_suggestion() { echo '<p>Bạn có muốn nâng cấp lên phiên bản cao hơn không? <a href="#">Xem ngay</a></p>'; } add_action('woocommerce_after_cart_table', 'upsell_product_suggestion'); |
15. Tích hợp chatbot hỗ trợ khách hàng ngay trong giỏ hàng
| function add_chatbot_support() { echo '<div class="chatbot"><a href="https://zalo.me/xxxxxxx">Hỗ trợ qua Zalo</a></div>'; } add_action('woocommerce_after_cart', 'add_chatbot_support'); |
Bạn có thể chọn hoặc kết hợp nhiều chức năng trên để cải thiện trang giỏ hàng của mình. Nếu cần tuỳ chỉnh sâu hơn, hãy cho mình biết nhé!