Woocommerce - 产品页面上的添加到购物车和立即购买按钮 [英] Woocommerce - Add To Cart and Buy Now buttons on Product Pages

查看:39
本文介绍了Woocommerce - 产品页面上的添加到购物车和立即购买按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Woocommerce 的产品页面上添加一个立即购买按钮,因此有两个按钮:

I am attempting to add a buy now button in Woocommerce on the product page so there are two buttons:

  1. 加入购物车
  2. 立即购买(将产品添加到购物车并重定向到结帐)

我仍然想添加到购物车以照常运行.

I still want add to cart to function as usual.

我怎样才能做到这一点?非常感谢.

How can I achieve this? Many thanks.

http://wordpress.org/extend/plugins/woocommerce/

推荐答案

我通过找到这篇博文 http://samplacette.com/skip-shopping-cart-in-woocommerce/.

I managed to resolve this by finding this blog post http://samplacette.com/skip-shopping-cart-in-woocommerce/.

如果其他人发现他们正在努力实现这一点,我就是这样做的(可能不是最好的解决方案,但对我有用):

If anyone else finds that they are struggling to implement this, this is how I did it (might not be the best solution but it works for me):

我将以下文本复制到我的主题functions.php中

I copied the following text into my theme functions.php

/** * Set cart item quantity action *
* Only works for simple products (with integer IDs, no colors etc) *
* @access public
* @return void */
function woocommerce_set_cart_qty_action() { 
    global $woocommerce;
    foreach ($_REQUEST as $key => $quantity) {
    // only allow integer quantities
    if (! is_numeric($quantity)) continue;

        // attempt to extract product ID from query string key
        $update_directive_bits = preg_split('/^set-cart-qty_/', $key);
        if (count($update_directive_bits) >= 2 and is_numeric($update_directive_bits[1])) {
            $product_id = (int) $update_directive_bits[1]; 
            $cart_id = $woocommerce->cart->generate_cart_id($product_id);
            // See if this product and its options is already in the cart
            $cart_item_key = $woocommerce->cart->find_product_in_cart( $cart_id ); 
            // If cart_item_key is set, the item is already in the cart
            if ( $cart_item_key ) {
                $woocommerce->cart->set_quantity($cart_item_key, $quantity);
            } else {
                // Add the product to the cart 
                $woocommerce->cart->add_to_cart($product_id, $quantity);
            }
        }
    }
}

add_action( 'init', 'woocommerce_set_cart_qty_action' );

然后我修改了theme/woocommerce/single-product/add-to-cart/simple.php(确保你没有修改插件文件,所以复制并粘贴到你的主题文件放入 woocommerce 文件夹)到以下内容(请注意,我已从代码中删除了我的数量输入,因此如果您需要它,请确保重新编写代码以使其正常工作):

And then I modified theme/woocommerce/single-product/add-to-cart/simple.php (make sure you don't midify the plugin files so make a copy and paste into your theme files into a woocommerce folder) to the following (notice that I had removed my quantity input from my code so if you need it,ensure you rework the code to get it working):

<form class="cart single-product" method="post" enctype='multipart/form-data'>
    <?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
    <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
    <button type="submit" class="single_add_to_cart_button button alt cart-buttton add-to-cart"><?php echo $product->single_add_to_cart_text(); ?></button>
    <?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
</form>

<form class="cart single-product" method="post" enctype='multipart/form-data' action="/checkout?set-cart-qty_<?php echo $product->id;?>=1">
    <button type="submit"  class="single_add_to_cart_button button alt cart-buttton buy-now">Buy Now</button>
    <input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
</form>

我在现有的添加到购物车"按钮旁边添加了另一个按钮,但将表单分开.博客文章提到你可以添加一个超链接,但上面的内容对我有用,我需要自定义页面的方式(稍微冗长一些)

I added another button next to the existing Add to Cart button but separating the form. The blog post mentions that you can add a hyperlink instead but the above worked for me in terms of the way I needed to customise the page (slightly more long winded)

来自博客:

使用说明:

使用查询字符串参数创建超链接,如下所示:?set-cart-qty_=您的产品的数字 ID 在哪里(类似于167"),以及 > 您希望在用户购物车中设置的数量(很可能只是1").

Create a hyperlink with a query string argument like so: ?set-cart-qty_= Where is the numerical ID of your product (something like "167") and is the >quantity you want set in the user’s shopping cart (most likely this will just be "1").

用于将用户发送到购物车中仅包含一件 ID 为167"的产品进行结账的示例网址:

Example URL to send user to checkout with exactly one item of product with ID "167" in cart:

http://my.website.com/checkout?set-cart-数量_167=1

我希望以上内容可以帮助任何和我有类似问题的人.

I hope the above helps anyone who has a similar problem as I had.

这篇关于Woocommerce - 产品页面上的添加到购物车和立即购买按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆