gpt4 book ai didi

php - Woocommerce - 产品页面上的添加到购物车和立即购买按钮

转载 作者:可可西里 更新时间:2023-11-01 01:00:56 56 4
gpt4 key购买 nike

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

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

我仍然希望添加到购物车以正常使用。

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

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

最佳答案

我通过找到这篇博文 http://samplacette.com/skip-shopping-cart-in-woocommerce/ 设法解决了这个问题.

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

我将以下文本复制到我的主题 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' );

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

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

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

来自博客:

Usage instructions:

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”).

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-qty_167=1

希望上面的内容能帮到和我有类似问题的人。

关于php - Woocommerce - 产品页面上的添加到购物车和立即购买按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26984384/

56 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com