I probably would need to see the rest of your code, because
item is added using ajax, I then adjust the quantity and click the add to cart button for a second time but nothing happens
isn’t the way that WooCommerce behaves by default. From a shop archive you can’t edit the quantity and add that by ajax.
But modifying some of my old code, here’s something that would make sure a “special item” is removed from the cart before being added.
/*
Plugin Name: WooCommerce Special Handling
Description: Removes certain items from cart before re-adding them
Version: 1.0
Author: Kathy Darling
Author URI: https://kathyisawesome.com
Requires at least: 4.1.0
Tested up to: 4.1.0
Copyright: ? 2015 Kathy Darling.
License: GNU General Public License v3.0
License URI: https://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* The Main WC_Special_Handling class
**/
if ( ! class_exists( 'WC_Special_Handling' ) ) :
class WC_Special_Handling {
/**
* WC_Special_Handling init
*
* @access public
* @since 1.0
*/
public static function init() {
// product meta
add_action( 'woocommerce_product_options_general_product_data', array( __CLASS__, 'add_to_wc_metabox' ) );
add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_wc_meta_box' ), 10, 2 );
// validation - ensure product is never in the cart with other products
add_filter( 'woocommerce_add_to_cart_validation', array( __CLASS__, 'maybe_remove_item' ), 10, 6 );
}
/*-----------------------------------------------------------------------------------*/
/* Product Write Panels */
/*-----------------------------------------------------------------------------------*/
/*
* Add text inputs to product metabox
* @since 1.0
*/
public static function add_to_wc_metabox(){
global $post;
echo '<div class="options_group">';
echo woocommerce_wp_checkbox( array(
'id' => '_special_handling',
'label' => __( 'Special Handling', 'your-plugin' ) ,
'description' => __( 'For special items that need to be removed/re-added to cart.' )
)
);
echo '</div>';
}
/*
* Save extra meta info
* @since 1.0
*/
public static function process_wc_meta_box( $post_id, $post ) {
if ( isset( $_POST['_special_handling'] ) ) {
update_post_meta( $post_id, '_special_handling', 'yes' );
} else {
update_post_meta( $post_id, '_special_handling', 'no' );
}
}
/*-----------------------------------------------------------------------------------*/
/* Check Cart for presence of certain items */
/*-----------------------------------------------------------------------------------*/
/**
* When an item is added to the cart, remove other products
* based on WooCommerce Subscriptions code
*/
public static function maybe_remove_item( $valid, $product_id, $quantity, $variation_id = null, $variations = array(), $cart_item_data = array() ){
if ( self::is_item_special( $product_id ) && WC()->cart->get_cart_contents_count() > 0 ){
$cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id );
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variations, $cart_item_data );
// Find the cart item key in the existing cart
$cart_item_key = WC()->cart->find_product_in_cart( $cart_id );
if( $cart_item_key ){
WC()->cart->set_quantity( $cart_item_key, 0 );
wc_add_notice( __( 'Here is some message.', 'your-plugin' ) );
}
}
return $valid;
}
/*-----------------------------------------------------------------------------------*/
/* Helper methods */
/*-----------------------------------------------------------------------------------*/
/*
* check if an item has custom field
*/
public static function is_item_special( $product_id ){
if ( 'yes' == get_post_meta( $product_id, '_special_handling', true ) ){
return TRUE;
} else {
return false;
}
}
} //end class: do not remove or there will be no more guacamole for you
endif; // end class_exists check
// Launch the whole plugin
WC_Special_Handling::init();