php-Woocommerce-产品页面上的“添加到购物车和立即购买”按钮
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-Woocommerce-产品页面上的“添加到购物车和立即购买”按钮,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3139字,纯文字阅读大概需要5分钟。
内容图文
我试图在产品页面的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
希望以上内容对与我有类似问题的任何人有所帮助.
内容总结
以上是互联网集市为您收集整理的php-Woocommerce-产品页面上的“添加到购物车和立即购买”按钮全部内容,希望文章能够帮你解决php-Woocommerce-产品页面上的“添加到购物车和立即购买”按钮所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。