[英]Additional Woocommerce product Add To Cart button that redirect to checkout
我需要客户能够在添加到购物车,继续购物和添加到购物车以及重新定向到结帐之间进行选择。 换句话说,我要在产品页面上添加一个额外的按钮。
作为WooCommerce的新手,我正在努力使数量输入起作用。 如果只购买一台,效果很好,但添加多于一台(数量)则不能。
另外,我无法理解如何添加对可变产品的支持,但这可能是一个单独的问题? (很抱歉)。
这是我正在使用的代码:
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtocart' );
function add_content_after_addtocart() {
$current_product_id = get_the_ID();
$product = wc_get_product( $current_product_id );
$checkout_url = WC()->cart->get_checkout_url();
if( $product->is_type( 'simple' )) { ?>
<script>
jQuery(function($) {
$(".custom-checkout-btn").on("click", function() {
$(this).attr("href", function() {
return this.href + '&quantity=' + $('input.qty').val();
});
});
});
</script>
<?php
echo '<a href="'.$checkout_url.'?add-to-cart='.$current_product_id.'" class="single_add_to_cart_button button alt">Buy & Checkout</a>';
}
}
关于我要去哪里的任何输入吗? 我能得到的所有帮助都受到赞赏。
您的代码中存在一些错误……请尝试以下操作:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_addtocart_and_checkout' );
function add_custom_addtocart_and_checkout() {
global $product;
$addtocart_url = wc_get_checkout_url().'?add-to-cart='.$product->get_id();
$button_class = 'single_add_to_cart_button button alt custom-checkout-btn';
$button_text = __("Buy & Checkout", "woocommerce");
if( $product->is_type( 'simple' )) :
?>
<script>
jQuery(function($) {
var url = '<?php echo $addtocart_url; ?>',
qty = 'input.qty',
button = 'a.custom-checkout-btn';
// On input/change quantity event
$(qty).on('input change', function() {
$(button).attr('href', url + '&quantity=' + $(this).val() );
});
});
</script>
<?php
echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>';
endif;
}
代码进入您的活动子主题(或活动主题)的function.php文件中。 它应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.