我在这个答案线程中获得了帮助,该线程允许添加一个额外的添加到购物车按钮,该按钮重定向到结帐。它适用于简单的产品。
但是如何让它也适用于可变产品呢?
我一直在尝试自己,但无论我做什么,我都会破坏网站。我根本不明白如何使这项工作与可变产品一起使用。
这是适用于简单产品的稍作改动的代码,其中考虑了数量字段:
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;
}
有谁知道如何使它也适用于可变产品?