我想给一个链接,一个允许他们使用购物车中已有产品的用户直接进入结帐页面的链接。
我相信我将需要做两件事:
我每个人都有两个摘要,但是我不确定它们是否可以像这样一起工作,而且我不确定如何在特定页面上使用它(可能只是空白页面,其显示时间不足以添加到购物车和将用户发送到结帐页面)
<?php
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 21;
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if {( $_product->id == $product_id )
$found = true;}
}
// if product not found, add it
if {( ! $found )
WC()->cart->add_to_cart( $product_id );}
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
}
/**
* Redirect users after add to cart.
*/
function my_custom_add_to_cart_redirect( $url ) {
$url = WC()->cart->get_checkout_url();
// $url = wc_get_checkout_url(); // since WC 2.5.0
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
如果也有更好的方法可以做到这一点,请告诉我。
谢谢,Brian
您实际上可以执行此操作而无需任何其他代码。
WooCommerce支持通过?add-to-cart
URL参数将商品添加到购物车。
如果您将某人发送到任何页面,后跟URL参数?add-to-cart=21
,它将自动将ID 21的产品添加到他们的购物车中。在这种情况下,您可以将其直接发送到Checkout页面。
这是您的自定义URL的样子:
http://example.com/checkout/?add-to-cart=21
这种方式比运行脚本然后重定向到结帐要快得多。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句