/*添加buy Now*/
add_action('woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
add_action('woocommerce_after_add_to_cart_form', 'buy_Now_submit_form');
add_filter('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
function add_content_after_addtocart() {
// get the current post/product ID
$current_product_id = get_the_ID();
// get the product based on the ID
$product = wc_get_product( $current_product_id );
// get the "Checkout Page" URL
//$checkout_url = wc_get_checkout_url();
if (in_array($product->get_type(), ['simple', 'variable'])) {
$buy_Now_button = '<button type="submit" style="clear:both;margin-top:10px;width:100%;height:45px;" name="add-to-cart" class="single_add_to_cart_button button alt" value="' . $current_product_id .'" id="buy_Now_button">ซื้อทันที </button>
<input type="hidden" name="is_buy_Now" id="is_buy_Now" value="0" />
';
echo $buy_Now_button;
}
}
/**
* 控制跳转
*/
function buy_Now_submit_form() {
?>
<script>
jQuery(document).ready(function(){
// listen if someone clicks 'Buy Now' button
jQuery('#buy_Now_button').click(function(){
// set value to 1
jQuery('#is_buy_Now').val('1');
//submit the form
jQuery('form.cart').submit();
});
});
</script>
<?PHP
}
/**
* 商品加入购物车后,返回要跳转的地址
* @param $redirect_url
* @return string
*/
function redirect_to_checkout($redirect_url) {
if (isset($_REQUEST['is_buy_Now']) && $_REQUEST['is_buy_Now']) {
$redirect_url = wc_get_checkout_url();
}
return $redirect_url;
}
在主题functions.PHP中加入以上代码