Skip to main content

[Thủ Thuật WordPress] Thêm nút Mua Ngay vào sản phẩm woocommerce wordpress

Thêm nút Mua Ngay vào sản phẩm woocommerce wordpress, sau đây sẽ hướng dẫn thêm nút mua ngay vào sản phẩm woocommerce.

Nút mua ngay sẽ hiện ở trang chi tiết sản phẩm, nút mua ngay sản phẩm có chức năng khi bạn click vào sẽ tự động chuyển đến trang thanh toán, không cần qua giỏ hàng nữa.

Nút mua ngay sản phẩm thật tiện phải không ạ. Ưu điểm:

+ Bạn không cần cài thêm plugin đỡ nặng web.

+ Không cần biết code, chỉ cần theme mã mua ngay vào function.php

+ Code nhẹ, đảm bảo tốc độ nhanh.

+ Dùng cho cả sản phẩm đơn giản và biến thể.

Cách làm rất đơn giản là thêm đoạn code guói vào file functions.php trong theme của bạn

/*
* Thêm nút mua ngay vào trang chi tiết sản phẩm Woocommerce
*/
add_action('woocommerce_after_add_to_cart_button','dev_quickbuy_after_addtocart_button');
function dev_quickbuy_after_addtocart_button(){
    global $product;
    ?>
    <style>
        .dev-quickbuy button.single_add_to_cart_button.loading:after {
            display: none;
        }
        .dev-quickbuy button.single_add_to_cart_button.button.alt.loading {
            color: #fff;
            pointer-events: none !important;
        }
        .dev-quickbuy button.buy_now_button {
            position: relative;
            color: rgba(255,255,255,0.05);
        }
        .dev-quickbuy button.buy_now_button:after {
            animation: spin 500ms infinite linear;
            border: 2px solid #fff;
            border-radius: 32px;
            border-right-color: transparent !important;
            border-top-color: transparent !important;
            content: "";
            display: block;
            height: 16px;
            top: 50%;
            margin-top: -8px;
            left: 50%;
            margin-left: -8px;
            position: absolute;
            width: 16px;
        }
    </style>
    <button type="button" class="button buy_now_button">
        <?php _e('Mua ngay', 'dev'); ?>
    </button>
    <input type="hidden" name="is_buy_now" class="is_buy_now" value="0" autocomplete="off"/>
    <script>
        jQuery(document).ready(function(){
            jQuery('body').on('click', '.buy_now_button', function(e){
                e.preventDefault();
                var thisParent = jQuery(this).parents('form.cart');
                if(jQuery('.single_add_to_cart_button', thisParent).hasClass('disabled')) {
                    jQuery('.single_add_to_cart_button', thisParent).trigger('click');
                    return false;
                }
                thisParent.addClass('dev-quickbuy');
                jQuery('.is_buy_now', thisParent).val('1');
                jQuery('.single_add_to_cart_button', thisParent).trigger('click');
            });
        });
    </script>
    <?php
}
add_filter('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout($redirect_url) {
    if (isset($_REQUEST['is_buy_now']) && $_REQUEST['is_buy_now']) {
        $redirect_url = wc_get_checkout_url(); //đổi thành wc_get_cart_url()
    }
    return $redirect_url;
}

Nếu bạn muốn chuyển đến trang giỏ hàng , không muốn chuyển đến cart woocommerce nữa thì sửa đoạn dưới cùng của code:

Đổi wc_get_checkout_url đổi thành wc_get_cart_url  là được, đừng sửa dấu chấm phẩy gì hết nhé!