Wordpress 如果购物车项目属于此类别,请将其删除

Wordpress 如果购物车项目属于此类别,请将其删除,wordpress,woocommerce,Wordpress,Woocommerce,我有一个页面,用户在其中填写表单,然后创建一个自定义的商业产品 用户只能拥有这一类型的产品,因此我将其设置为“quotes”类别 然而,为了确保他们还没有把它放在购物车里,我有一个脚本,在添加产品之前清空购物车 //check if product already in cart WC()->cart->empty_cart(true); WC()->session->set('cart', array()); if ( WC()->cart->get_ca

我有一个页面,用户在其中填写表单,然后创建一个自定义的商业产品

用户只能拥有这一类型的产品,因此我将其设置为“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;  
但由于还有一个“插件”类别,我不想从购物车中删除任何这些产品,只想删除分配给“报价”类别的产品

我已经看到了很多从购物车中删除特定产品的方法,但是我没有看到任何可以删除属于某个类别的产品的方法

我使用了下面的方法

但这也不起作用

有人能帮忙吗

编辑:

该产品是通过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\u item\u key
而不是
$prod\u id

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

     WC()->cart->remove_cart_item( $cart_item_key );

}
功能从购物车中删除购物车($cart){
if(定义了('DOING'uajax'))
返回;
如果(did_action('woocommerce_before_calculate_totals')>=2)
返回;
/**********背景**********/
$remove_categories=array('quotes');//删除这些类别
/**********循环浏览购物车项目**********/
foreach($cart->get\u cart()作为$cart\u item\u key=>$cart\u item){
//获取产品id
$product_id=$cart_项目['product_id'];
//检查产品是否属于某一类别
if(具有术语($product\U cat'删除类别,$product\U id)){
$cart->remove\u cart\u item($cart\u item\u key);
}
}
}
添加行动('WOOMerce'在计算总额之前,'从购物车中删除',10,1);

谢谢,只有当他们收到新报价时,我怎么才能处理这件事?当用户获得新报价时,我需要购物车清空其他“报价”项。这有意义吗?说清楚点。因此,如果将“quotes”类别的产品添加到购物车中,则希望从购物车中删除相同类别的所有其他产品。是否打算让访问者/用户看到此通知?(其他产品已被移除)不完全是。因此,他们进入“获取报价”页面,填写表格,在woocommerce中创建报价产品。然后它们被重定向到购物车。然而。如果他们这样做时,购物车中已经有一个产品属于类别报价,则应将其删除并替换为他们刚刚制作的产品。因此,他们在购物车中只能有一个报价产品。好吧,为了给出一个适当的答案,了解该产品是如何通过该表单(代码)创建的是很有用的。这样我们就可以测试您的问题并提供相应的代码。我们将把它添加到我的问题中
// 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');
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {

     WC()->cart->remove_cart_item( $cart_item_key );

}