Php 附加的Woocommerce产品添加到购物车按钮,重定向到结帐

Php 附加的Woocommerce产品添加到购物车按钮,重定向到结帐,php,jquery,wordpress,woocommerce,product,Php,Jquery,Wordpress,Woocommerce,Product,我需要客户能够在添加到购物车和继续购物之间进行选择,并添加到购物车和重新定向到收银台。换句话说,我在产品页面添加了一个额外的按钮 作为WooCommerce的新手,我很难让数量输入发挥作用。如果只买一个就可以了,但如果添加多个(数量)就不行了 另外,我不明白如何添加对可变产品的支持,但这可能是一个单独的问题?(很抱歉) 以下是我使用的代码: add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after_addtoca

我需要客户能够在添加到购物车和继续购物之间进行选择,并添加到购物车和重新定向到收银台。换句话说,我在产品页面添加了一个额外的按钮

作为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 &amp; Checkout</a>';
    } 
}
add_action('woocommerce_后加_至购物车按钮','add_内容后加_至购物车按钮');
函数add\u content\u在\u addtocart()之后{
$current_product_id=获取_id();
$product=wc\U get\U product($current\U product\U id);
$checkout\u url=WC()->cart->get\u checkout\u url();
如果($product->是_类型('simple')){?>
jQuery(函数($){
$(“.custom checkout btn”)。打开(“单击”,函数(){
$(this.attr(“href”,function()){
返回this.href+'&quantity='+$('input.qty').val();
});
});
});

您的代码中有一些错误…请尝试以下操作:

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 &amp; 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;
}
add_操作('woocommerce_后加_至_购物车按钮','add_自定义_添加至购物车和_结帐');
函数add_custom_addtocart_和checkout(){
全球$产品;
$addtocart\u url=wc\u get\u checkout\u url()。?添加到购物车='。$product->get\u id();
$button_class='single_add_to_cart_button alt custom checkout btn';
$button_text=uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
如果($product->是_类型('simple')):
?>
jQuery(函数($){
var url=“”,
数量='输入数量',
按钮='a.custom-checkout-btn';
//关于输入/更改数量事件
$(数量).on('input change',function()){
$(按钮).attr('href',url+'和quantity='+$(this.val());
});
});