Swo*_*wof 16 wordpress product shopping cart woocommerce
我认为这段代码应该有效,但不能确定将它放在何处.我到过的每个地方到目前为止都失败了......
add_action('init', 'woocommerce_clear_cart');
function woocommerce_clear_cart() {
global $woocommerce, $post, $wpdb;
$url = explode('/', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$slug=$url[4];
$postid = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status='publish' AND post_name = '$slug'");
if ($postid){
if ($postid == PRODUCTID1 || $postid == PRODUCTID2){
$woocommerce->cart->empty_cart();
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 39
不幸的是,在WooCommerce将商品添加到购物车之前,没有"动作"挂钩.但是在添加到购物车之前他们有一个"过滤器"钩子.这就是我使用它的方式:
add_filter( 'woocommerce_add_cart_item_data', 'woo_custom_add_to_cart' );
function woo_custom_add_to_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
// Do nothing with the data and return
return $cart_item_data;
}
Run Code Online (Sandbox Code Playgroud)
小智 14
根据接受的答案和最新的Woo版本2.5.1,我使用类-wc-checkout.php中的代码更新了函数,使其更加清晰,以清除购物车
add_filter( 'woocommerce_add_cart_item_data', '_empty_cart' );
function _empty_cart( $cart_item_data ) {
WC()->cart->empty_cart();
return $cart_item_data;
}
Run Code Online (Sandbox Code Playgroud)
hel*_*ing 10
从我对另一个问题的回答:
存在是一个过滤器/钩运行之前的项目被添加到购物车为每个产品经过验证在加入之前.
因此,在验证产品时,我们可以检查商品是否已经存在购物车中的商品并清除它们(如果能够添加当前商品)并添加错误消息.
/**
* When an item is added to the cart, remove other products
*/
function so_27030769_maybe_empty_cart( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Whoa hold up. You can only have 1 item in your cart', 'error' );
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'so_27030769_maybe_empty_cart', 10, 3 );
Run Code Online (Sandbox Code Playgroud)
这对我来说就像一个魅力,删除了以前的产品,并添加了新的产品配置.干杯
更新:对于WooCommerce 3.0.X
function check_if_cart_has_product( $valid, $product_id, $quantity ) {
if(!empty(WC()->cart->get_cart()) && $valid){
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if( $product_id == $_product->get_id() ) {
unset(WC()->cart->cart_contents[$cart_item_key]);
}
}
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );
Run Code Online (Sandbox Code Playgroud)
对于低于3.0.X的WooCommerce版本
function check_if_cart_has_product( $valid, $product_id, $quantity ) {
if(!empty(WC()->cart->get_cart()) && $valid){
foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
$_product = $values['data'];
if( $product_id == $_product->id ) {
unset(WC()->cart->cart_contents[$cart_item_key]);
}
}
}
return $valid;
}
add_filter( 'woocommerce_add_to_cart_validation', 'check_if_cart_has_product', 10, 3 );
Run Code Online (Sandbox Code Playgroud)
尝试这个,
要从购物车中删除所有产品并添加最后添加的产品,
//For removing all the items from the cart
global $woocommerce;
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart($product_id,$qty);
Run Code Online (Sandbox Code Playgroud)
类文件是wp-content/plugins/woocommerce/classes/class-wc-cart.php.
您可以将上述代码添加到functions.php中的add to cart函数中。
希望它的作品..
| 归档时间: |
|
| 查看次数: |
31690 次 |
| 最近记录: |