我正在尝试在产品页面上的Woocommerce中添加一个立即购买按钮,因此有两个按钮:
- 加入购物车
- 立即购买(会将产品添加到购物车并重定向到结帐)
我仍然希望添加到购物车以照常运行。
我怎样才能做到这一点?非常感谢。
http://wordpress.org/extend/plugins/woocommerce/
我设法通过找到这篇博文来解决这个问题 http://samplacette.com/skip-shopping-cart-in-woocommerce/。
如果其他人发现他们正在努力实现这一点,这就是我的做法(可能不是最好的解决方案,但它对我有用):
我将以下文本复制到我的主题函数中.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/单一产品/添加到购物车/简单.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>
我在现有的添加到购物车按钮旁边添加了另一个按钮,但将表单分开。博客文章提到您可以添加超链接,但就我需要自定义页面的方式而言,上述内容对我有用(稍微长一点)
来自博客:
使用说明:
使用查询字符串参数创建超链接,如下所示: ?设置购物车-qty_= 其中 是产品的数字 ID(类似于"167"),是您希望在用户购物车中设置的>数量(很可能只是"1")。
将用户发送到购物车中 ID 为"167"的产品商品结账的示例 URL:
http://my.website.com/checkout?set-cart-qty_167=1
我希望以上内容可以帮助任何与我有类似问题的人。
经过多次搜索,我很惊讶这不是标准的东西。这是我的解决方案:
要么使用像"woocommerce_single_product_summary"这样的钩子
或者将 wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php复制到您的子主题中,例如:WP-content/themes/child-theme/woocommerce/single-product/add-to-cart/simple.php
编辑该文件,并在要显示按钮的位置添加以下代码:
<div class="express-checkout-wrapper">
<a id="dc_express_checkout" href="/checkout/?add-to-cart=<?php echo get_the_ID(); ?>">
Purchase
</a>
</div>
现在唯一的问题是该按钮将带您结帐并添加正确的产品,但如果您更改了它,则没有正确的数量,所以我在页脚排队的自定义文件中使用了 js.js:
// Add and change quantity to express checkout button when the quantity is updated
if($('.cart .qty').length){
var originalUrl = $('#dc_express_checkout').attr('href');
$('.cart .qty').change(function(){
var url = originalUrl + '&quantity=' + $(this).val();
$('#dc_express_checkout').attr('href', url);
});
}
您可以将网址从以下位置更改:
href="/checkout/?add-to-cart=<?php echo get_the_ID(); ?>"
自:
href="/cart/?add-to-cart=<?php echo get_the_ID(); ?>"
如果您希望按钮定向到购物车而不是结帐页面。