简体   繁体   English

添加到购物车并重定向到 WooCommerce 中可变产品的结帐

[英]Add to cart and redirect to checkout for variable products in WooCommerce

I got help in this answer thread that allow to add an additional add-to-cart button that redirects to checkout.我在这个答案线程中得到了帮助,允许添加一个额外的添加到购物车按钮,重定向到结账。 It works fine for simple products.它适用于简单的产品。

But how to make it work for variable products as well?但是如何使它也适用于可变产品呢?

I've been trying myself, but no matter what I do, I break the site.我一直在尝试自己,但无论我做什么,我都会破坏网站。 I simply do not understand how to make this work with/ for variable products.我只是不明白如何使用/用于可变产品。

Here's the lightly changed code that works for simple products and which takes the quantity field into consideration:这是适用于简单产品并考虑了数量字段的轻微更改的代码:

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;
}

Does anyone know how to get this working for variable products too?有谁知道如何让它也适用于可变产品?

Update 3更新 3

The following code will handle simple and variable products adding an additional Add to cart button that redirects to cart (with synchronized quantity) .以下代码将处理简单和可变的产品,添加一个额外的添加到购物车按钮,重定向到购物车(具有同步数量)

The code works for simple and variable products as well.该代码也适用于简单和可变的产品。

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

    elseif( $product->is_type( 'variable' ) ) : 

    $addtocart_url = wc_get_checkout_url().'?add-to-cart=';
    ?>
    <script>
    jQuery(function($) {
        var url    = '<?php echo $addtocart_url; ?>',
            vid    = 'input[name="variation_id"]',
            pid    = 'input[name="product_id"]',
            qty    = 'input.qty',
            button = 'a.custom-checkout-btn';

        // Once DOM is loaded
        setTimeout( function(){
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
            }
        }, 300 );

        // On input/change quantity event
        $(qty).on('input change', function() {
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(this).val() );
            }
        });

        // On select attribute field change event
        $('.variations_form').on('change blur', 'table.variations select', function() {
            if( $(vid).val() != '' ){
                $(button).attr('href', url + $(vid).val() + '&quantity=' + $(qty).val() );
            }
        });
    });
    </script>
    <?php
    endif;
    echo '<a href="'.$addtocart_url.'" class="'.$button_class.'">'.$button_text.'</a>';
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 Tested and works.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加到购物车并重定向到 WooCommerce 中默认可变产品的结帐 - Add to cart and redirect to checkout for default variable products in WooCommerce Woocommerce添加到购物车按钮重定向到结帐 - Woocommerce add to cart button redirect to checkout WooCommerce 添加到购物车后重定向到结帐 - WooCommerce after add to cart redirect to checkout 隐藏WooCommerce变量产品上的“添加到购物车”按钮 - Hiding 'Add to Cart' button on WooCommerce variable products WooCommerce:更改多个产品的添加到购物车按钮文本,并重定向到结帐 - WooCommerce: Change add to cart button text for multiple products with a redirection to checkout Ajax 加入购物车 WooCommerce 单品上的简单和可变产品 - Ajax Add to cart for simple and variable products on WooCommerce single products 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce 其他Woocommerce产品“添加到购物车”按钮,可重定向到结帐 - Additional Woocommerce product Add To Cart button that redirect to checkout 将添加到购物车按钮重定向到 WooCommerce 中 1 个特定登录页面的结帐页面 - Redirect add to cart button to checkout page for 1 specific landing page in WooCommerce 重定向到Checkout滚动到WooCommerce中添加到购物车的账单详细信息 - Redirect to Checkout scrolling to billing details on add to cart in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM