我试图在产品页面的Woocommerce中添加“立即购买”按钮,所以有两个按钮:
>添加到购物车
>立即购买(将产品添加到购物车并重定向到结帐)
我仍然想添加到购物车以照常运行.
我该如何实现?非常感谢.
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' );
然后我修改了theme / 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:
07001
希望以上内容对与我有类似问题的任何人有所帮助.