Code thêm nút MUA NGAY vào kế nút Add to cart

Với đoạn code chia sẻ dưới đây, bạn sẽ dễ dàng thêm được nút MUA NGAY vào kế bên nút THÊM VÀO GIỎ như hình bên dưới

mua ngay
Thêm nút Mua Ngay

Việc của bạn là thêm đoạn code vào file functions.php của theme đang dùng là được

window.addEventListener(‘DOMContentLoaded’, function() {

/** Add quick buy button go to checkout after click* Author: levantoan.com*/add_action('woocommerce_after_add_to_cart_button','devvn_quickbuy_after_addtocart_button');functiondevvn_quickbuy_after_addtocart_button(){global$product;    ?>    <style>.devvn-quickbuy button.single_add_to_cart_button.loading:after {            display: none;        }.devvn-quickbuy button.single_add_to_cart_button.button.alt.loading {            color: #fff;            pointer-events: none !important;        }.devvn-quickbuy button.buy_now_button {            position: relative;            color: rgba(255,255,255,0.05);        }.devvn-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', 'devvn'); ?>    </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');returnfalse;                }                thisParent.addClass('devvn-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');functionredirect_to_checkout($redirect_url) {if (isset($_REQUEST['is_buy_now']) && $_REQUEST['is_buy_now']) {$redirect_url = wc_get_checkout_url(); //or wc_get_cart_url()    }return$redirect_url;}

Đoạn trên sau khi click vào button Mua Ngay sẽ chuyển hướng tới trang thanh toán. Nếu bạn nào muốn chuyển hướng tới trang giỏ hàng thì sửa dòng 62 như sau

từ

$redirect_url = wc_get_checkout_url();

sửa thành

$redirect_url = wc_get_cart_url();

Code này đã được test với biến thể mặc định của woo. Còn bạn nào dùng plugin thứ 3 để custom hiển thị biến thể mà bị lỗi thì comment bên dưới nha

Chúc các bạn thành công!

Nguồn: levantoan.com

4.9/5 - (50 bình chọn)
Đăng ký
Thông báo về
guest
0 Bình Luận
Oldest
Newest Most Voted
Inline Feedbacks
Xem tất cả
0
Rất thích suy nghĩ của bạn, hãy bình luận.x
()
x