简体   繁体   English

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

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

I am trying the following code by LoicTheAztec - Add to cart and redirect to checkout for variable products in WooCommerce我正在尝试 LoicTheAztec 的以下代码 - 添加到购物车并重定向到 WooCommerce 中可变产品的结帐

Now, the problem is whenever I am clicking on the Buy Now button with the default variations set in the Product editor, the code is not working.现在,问题是每当我在产品编辑器中设置默认变体的情况下单击“立即购买”按钮时,代码都不起作用。

The Buy Now URL in this case becomes …/?addtocart=0&quantity=n在这种情况下,立即购买 URL 变为 …/?addtocart=0&quantity=n

The Theme I am using - Basel我正在使用的主题 - 巴塞尔

Any solutions?任何解决方案?

Tried the code by LoicTheAztec.尝试了 LoicTheAztec 的代码。

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

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

I expect this code to work when the user does not change or update any variations on the product page. That is when the user clicks on the Buy Now button with the default variations.

you don't need all that you should add the button in the variation form it will work as submiting the form as the add to cart button then make the redirect您不需要在变体表单中添加按钮的所有内容,它将作为提交表单作为添加到购物车按钮然后进行重定向

this is tested and working with me这是经过测试并与我一起工作

 add_action( 'woocommerce_single_variation', 'second_button_single_variation', 30 );
   function second_button_single_variation() {
    global $product;

echo '<form method="post">
    <button type="submit" name="checkout_now" id="checkout_now" class="single_add_to_cart_button button alt buy_now_button" value="'.$product->get_id().'">Buy Now</button></form>';
  }

  function woo_redirect_to_checkout() {
global $woocommerce;
  if( isset($_POST['checkout_now']) ){
 wc_clear_notices();
    $checkout_url = $woocommerce->cart->get_checkout_url();
 wp_redirect($checkout_url);
  }
 }
    add_filter ('add_to_cart_redirect', 'woo_redirect_to_checkout');

 // clearing the cart if you want to go to checkout with the desired product only
  add_filter( 'woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 20, 3 );
  function remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
if( isset($_POST['checkout_now']) ){
 if( ! WC()->cart->is_empty() )
    WC()->cart->empty_cart();
}
return $passed;
  }

暂无
暂无

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

相关问题 添加到购物车并重定向到 WooCommerce 中可变产品的结帐 - Add to cart and redirect to checkout for variable products in WooCommerce WooCommerce 添加到购物车后重定向到结帐 - WooCommerce after add to cart redirect to checkout Woocommerce添加到购物车按钮重定向到结帐 - Woocommerce add to cart button 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 重定向到Checkout滚动到WooCommerce中添加到购物车的账单详细信息 - Redirect to Checkout scrolling to billing details on add to cart in WooCommerce Woocommerce添加到购物车重定向到Checkout不适用于具有变体的产品 - Woocommerce Add To Cart redirect to Checkout not working for product with variants 将添加到购物车按钮重定向到 WooCommerce 中 1 个特定登录页面的结帐页面 - Redirect add to cart button to checkout page for 1 specific landing page in WooCommerce 在结帐时添加一个按钮以清空购物车并重定向到Woocommerce中的商店页面 - Add a button in checkout to empty cart and redirect to shop page in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM