Thêm nút mua ngay vào woocommerce
Trong bài này mình sẽ chia sẻ code thêm nút Mua Ngay vào Woocommerce một cách đơn giản. Nút Mua Ngay này có chức năng rất đơn giản, chỉ là click vào và chuyển hướng tới trang giỏ hàng, đồng thời thêm sản phẩm đã chọn vào giỏ hàng.

Bạn chỉ cần thêm đoạn code này vào file functions.php trong theme của bạn đang sử dụng. Vậy là xong
/*
* Add quick buy button go to checkout after click
* Author: webgoi.net
*/
add_action('woocommerce_after_add_to_cart_button','devvn_quickbuy_after_addtocart_button');
function devvn_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');
return false;
}
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');
function redirect_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 như sau
$redirect_url = wc_get_checkout_url();
Sửa thành
$redirect_url = wc_get_cart_url();
Chúc các bạn thành công!
Bài viết liên quan
- 07/02/2023
21.Code comment bằng facebook cho wordpress <div class=”cmt”> <div class=”fb-comments” data-width=”100%” data-href=”<?php the_permalink(); ?>” data-numposts=”3″></div> </div> <div id=”fb-root”></div> <script> (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = “//connect.facebook.net/vi_VN/sdk.js#xfbml=1&version=v2.7&appId=750688268378229”; fjs.parentNode.insertBefore(js, fjs); }(document, ‘script’, ‘facebook-jssdk’)); </script>
Đọc thêm
- 06/02/2023
20.Code lấy 10 comment mới nhất <?php $cmt = get_comments(array( ‘status’ => ‘approve’, ‘number’=> 10, )); ?> <div class=”content-w content-news”> <ul> <?php foreach ($cmt as $value) { ?> <li> <a href=”<?php the_permalink($value->comment_post_ID);?>#comment-<?php echo $value->comment_ID; ?>”><?php echo get_avatar($value->comment_author_email, 150 ); ?></a> <a href=”<?php the_permalink($value->comment_post_ID); ?>#comment-<?php echo $value->comment_ID; ?>”><?php echo $value->comment_author; ?></a> – <span style=”color: #cd8a35;font-size: 12px;”><?php […]
Đọc thêm
- 05/02/2023
19.Code lấy bài viết ngẫu nhiên <?php $postquery = new WP_Query(array(‘posts_per_page’ => 35, ‘orderby’ => ‘rand’)); if ($postquery->have_posts()) { while ($postquery->have_posts()) : $postquery->the_post(); $do_not_duplicate = $post->ID; ?> <li> <a href=”<?php the_permalink();?>”><?php the_title();?></a> </li> <?php endwhile; } wp_reset_postdata(); ?> Đoạn code trên lấy 35 bài viết ngẫu nhiên.
Đọc thêm
- 04/02/2023
18.Code query bài viết theo custom filed Query 1 custom filed <?php // args $args = array( ‘numberposts’ => -1, ‘post_type’ => ‘event’, ‘meta_key’ => ‘location’, ‘meta_value’ => ‘Melbourne’ ); // query $the_query = new WP_Query( $args ); ?> <?php if( $the_query->have_posts() ): ?> <ul> <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?> <li> <a href=”<?php […]
Đọc thêm