簡體   English   中英

其他Woocommerce產品“添加到購物車”按鈕,可重定向到結帳

[英]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 &amp; 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 &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;
}

代碼進入您的活動子主題(或活動主題)的function.php文件中。 它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM