Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce 商品快速結帳按鈕
<?php
function mxp_woocommerce_after_add_to_cart_button() {
$checkout_url = wc_get_checkout_url();
echo '<div id="mxp_quick_checkout"><a href="' . $checkout_url . '?add-to-cart=' . get_queried_object_id() . '" class="button alt">直接結帳</a></div>';
?>
<script>
jQuery(document).ready(function() {
var v_product = jQuery('form.variations_form.cart').data();
if (v_product === undefined) {
return;
}
var p_vdata = v_product.product_variations;
var p_id = v_product.product_id;
var keys = [];
for (var i = 0; i < p_vdata.length; i++) {
var e = p_vdata[i];
var attrs = e.attributes;
var v_id = e.variation_id;
for (var key in attrs) {
var index = key;
var data = attrs[key];
keys[index] = 1;
}
}
jQuery('#mxp_quick_checkout>a').attr('href', 'javascript:');
jQuery('#mxp_quick_checkout').click(function() {
//掃描現在的選擇
var pass = true;
for (var check in keys) {
if (jQuery('select[name="' + check + '"]').val() == "") {
pass = false;
}
}
if (pass){
var form = jQuery('form.variations_form.cart');
form.attr('action', '/checkout/');
form.submit();
} else {
alert('請先選擇項目!');
}});
});
</script>
<?php
}
add_action('woocommerce_after_add_to_cart_button', 'mxp_woocommerce_after_add_to_cart_button');
@nczz

This comment has been minimized.

Copy link
Owner Author

commented Mar 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.