Remove upsell product with main product From Cart in woocommerce

 add_action( 'woocommerce_cart_item_removed', function( $removed_cart_item_key, $cart){

//wp_mail( 'navototest@gmail.com', 'test', $removed_cart_item_key );

$cart = WC()->instance()->cart;

$line_item = $cart->removed_cart_contents[ $removed_cart_item_key ];

    $product_id = $line_item[ 'product_id' ];

wp_mail( 'navototest@gmail.com', 'test'.$product_id, $product_id );

//$counter = WC()->cart->get_cart_contents_count();

// for($i = 1; $i <= $counter; $i++){

// $cart_item_breakfast_id = $cart->find_product_in_cart($product_id);

// wp_mail( 'navototest@gmail.com', 'upsell'.$cart_item_breakfast_id, $cart_item_breakfast_id );

//             if($cart_item_breakfast_id){

//                 $cart->set_quantity($cart_item_breakfast_id, 0);                

                

//             }

// }

 $product = wc_get_product( $product_id ); 

//$upsell_sell_products = $product->get_upsell_ids(); 

//$product = new WC_Product($product_id);   

    $upsells = $product->get_upsells();

$upsell_key = array();

if(!empty($upsells)){

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

   $product_cart_key = $cart_item['key'];

   $productchild = $cart_item['product_id'];

if(in_array($productchild,$upsells)){

$upsell_key[] = $product_cart_key;

}   

}

}

foreach ($upsell_key as  $upsell_cart_key ) {

wp_mail( 'navototest@gmail.com', 'up_sell_product_cart_item_key'.$upsell_cart_key, $upsell_cart_key );

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

}

// $cartId = WC()->cart->generate_cart_id( 'PUT PRODUCT ID IN HERE' );

// $cartItemKey = WC()->cart->find_product_in_cart( $cartId );

// WC()->cart->remove_cart_item( $cartItemKey );

   wp_mail( 'navototest@gmail.com', 'opselitem', implode(' ',$upsells) );

//     if(!empty($upsells)){

//         foreach($upsells as $up_sell_product_id){

            //Get cart item Key

//             $up_sell_product_cart_item_key = WC()->cart->generate_cart_id( $up_sell_product_id );

//wp_mail( 'navototest@gmail.com', 'up_sell_product_cart_item_key'.$up_sell_product_cart_item_key, $up_sell_product_cart_item_key );

//             if(  WC()->cart->find_product_in_cart( $up_sell_product_cart_item_key ) ){

//                  WC()->cart->remove_cart_item( $up_sell_product_cart_item_key ); 

//             }

// $cart_item_upse_id = WC()->cart->find_product_in_cart($up_sell_product_id);

// wp_mail( 'navototest@gmail.com', 'up_sell_product_cart_item_key'.$cart_item_upse_id, $cart_item_upse_id );

//             if($cart_item_upse_id){

//                 $cart->set_quantity($cart_item_upse_id, 0);                

//                 //$output = "Item Removed";

//             }

//         }

//     }

}, 99, 2 );

Comments

Popular posts from this blog

Add Css And Js Files on Specific Page In laravel

Xpendy Pending

Laravel Join with two coloums