【问题标题】:Additional Woocommerce product Add To Cart button that redirect to checkout重定向到结帐的其他 Woocommerce 产品添加到购物车按钮
【发布时间】:2019-02-25 00:09:50
【问题描述】:

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

作为 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>';
    } 
}

任何关于我哪里出错的意见?感谢我能得到的所有帮助。

【问题讨论】:

    标签: php jquery wordpress woocommerce product


    【解决方案1】:

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

    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 文件中。它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 2014-08-30
      • 2022-11-18
      • 2019-07-18
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 2020-09-01
      • 2020-04-04
      相关资源
      最近更新 更多