用户只能拥有这一类型的产品,因此我将其设置为“quotes”类别
但是为了确保他们还没有得到它在购物车我有一个脚本清空购物车之前添加的产品
//check if product already in cart
WC()->cart->empty_cart(true);
WC()->session->set('cart', array());
if ( WC()->cart->get_cart_contents_count() == 0 ) {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
wp_redirect( home_url('/checkout/') ); exit;
但由于还有一个“插件”类别,我不想从购物车中删除任何这些产品,只想删除分配给“报价”类别的产品
我已经看到了很多从购物车中删除特定产品的方法,但是我没有看到任何可以删除属于某个类别的产品的方法。
我想出了以下使用排除某些类别时自动添加产品到购物车WooCommerce
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id
$prod_id = $cart_item['data']->get_id();
$category = quotes;
if( has_term( $category, 'product_cat', $prod_id ) ) {
WC()->cart->remove_cart_item($prod_id);
}
}
wp_redirect( home_url('/checkout/') ); exit;
和
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$prod_id = wc_get_product( $values['data']->get_id());
$category = quotes;
if( has_term( $category, 'product_cat', $prod_id ) ) {
WC()->cart->remove_cart_item($prod_id);
}
}
但这也不起作用。
有人能协助吗拜托
编辑:
该产品是通过HTML/JQUERY表单创建的
这是表单底部的PHP
// Add the shortcode to WordPress.
add_shortcode('vehicle-quote', 'vehicle_quote');
function vehicle_quote_add_post(){
if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && $_POST['action'] == 'post' ){
if ( !is_user_logged_in() )
return;
global $current_user;
$user_id = $current_user->ID;
$post_title = $_POST['post-title'];
$post_content = $_POST['posttext'];
$tags = $_POST['tags'];
$length = filter_var($_POST['outlengthquestion'], FILTER_SANITIZE_STRING);
$timefor = filter_var($_POST['outtimefor'], FILTER_SANITIZE_STRING);
$essential = filter_var($_POST['outessential'], FILTER_SANITIZE_STRING);
$contents = filter_var($_POST['outcontents'], FILTER_SANITIZE_STRING);
$sterio = filter_var($_POST['outsterio'], FILTER_SANITIZE_STRING);
global $error_array;
$error_array = array();
if (empty($post_title)) $error_array[]='Please add a title.';
if (count($error_array) == 0){
$post_id = wp_insert_post( array(
'post_author' => $user_id,
'post_title' => $post_title,
'post_type' => 'product',
'meta_input' => array(
'timefor_period' => $timefor,
'length_level' => $length,
'essential' => $essential,
'contents' => $contents,
'sterioprotect' => $sterio,
),
'post_status' => 'publish'
)
);
// select ID
$numberAsString = number_format($price, 2);
$product_id = $post_id;
wp_set_object_terms( $post_id, 'Quotes', 'product_cat' );
update_post_meta($post_id, '_regular_price', $numberAsString );
add_post_meta($post_id, '_price', $numberAsString );
add_post_meta($post_id, '_stock_status', 'instock' );
add_post_meta($post_id, '_manage_stock', 'yes');
add_post_meta ($post_id, '_stock', 1);
add_action( 'woocommerce_before_calculate_totals', 'remove_from_cart', 10, 1 );
//check if product already in cart
WC()->cart->add_to_cart( $product_id );
/*} */
wp_redirect( home_url('/checkout/') ); exit;
} else {
}
}
}
add_action('init','vehicle_quote_add_post');
要删除购物车项目,您应该使用$cart_item_key
而不是$prod_id
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
function remove_from_cart( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
/********** SETTINGS **********/
$remove_categories = array( 'quotes' ); // Remove these categories
/********** LOOP THROUGH CART ITEMS **********/
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Get product id
$product_id = $cart_item['product_id'];
// Check if product belongs to a certain category
if( has_term( $remove_categories, 'product_cat', $product_id ) ) {
$cart->remove_cart_item( $cart_item_key );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'remove_from_cart', 10, 1 );