Mi sitio web WooCommerce vende 2 productos de edición limitada para recaudar fondos para la investigación del cáncer. Uno es un zapato de hombre, el otro es un zapato de mujer.
Debido a que estos productos se ofrecerán como preventa (no estarán listos para enviarse hasta septiembre), quiero asegurarme de que cualquier pedido que contenga 1 de estos zapatos no pueda tener otros productos en el carrito (porque esto causará la orden se cumpla parcialmente, lo que causará confusión para todos).
Aquí está el comportamiento que quiero:
Solo como referencia, los ID de producto de WooCommerce para los 2 productos son: 348455 y 348443. Ambos están en una categoría de producto llamada 'EMI' y el ID de categoría de producto es 348.
Aquí está el código que he escrito hasta ahora, basado en una pregunta similar de Stackoverflow de 2017 que encontré. El problema con mi código a continuación es que, si primero agrego un producto normal a mi carrito, luego uno de los zapatos de edición limitada, eliminará el zapato de edición limitada de mi carrito en lugar del producto normal.
Funciona correctamente si agrego primero el zapato de edición limitada y luego un producto normal. ¿Puede alguien ayudarme a averiguar qué está mal?
add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 ); function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) { // Set HERE your targeted product IDs $target_product_id_one = 348455; $target_product_id_two = 348443; // Initialising some variables $has_item = false; $is_product_id = false; foreach( WC()->cart->get_cart() as $key => $item ){ // Check if there are other products in the cart with the 2 limited edition shoes if( $item['product_id'] !== $target_product_id_one && $item['product_id'] !== $target_product_id_two ){ $has_item = true; $key_to_remove = $key; } // Check if we add to cart the targeted product ID if( $product_id == $target_product_id_one || $product_id == $target_product_id_two ){ $is_product_id = true; } } if( $has_item && $is_product_id ){ WC()->cart->remove_cart_item($key_to_remove); // Optionaly displaying a notice for the removed item: wc_add_notice( __( 'Due to the presale, EMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' ); } }Encontré un código que debería funcionar para ti. Comprueba si el producto está en el carrito cuando se agrega el segundo (o posterior) artículo a su carrito y elimina todo lo demás si el zapato especial es lo primero que se agrega al carrito, y elimina el zapato especial si un producto normal es el 1ra cosa agregada al carrito.
// Remove conditionally cart items based on a specific product (item) add_action( 'woocommerce_before_calculate_totals', 'remove_cart_items_conditionally_a', 10, 1 ); function remove_cart_items_conditionally_a( $cart ) { // HERE define your specific product ID $specific_product_id = 348455; if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; $cart_items = $cart->get_cart(); // Cart items array $items_count = count($cart_items); // Different cart items count // Continue if cart has at least 2 different cart items if ( $items_count < 2 ) return; $last_item = end($cart_items); // Last cart item data array $is_last_item = false; // Initializing // Check if the specific product is the last added item if ( in_array($specific_product_id, array( $last_item['product_id'], $last_item['variation_id'] ) ) ) { $is_last_item = true; } // Loop through cart items foreach ( $cart_items as $cart_item_key => $cart_item ) { // Remove all others cart items when specific product ID is --the last-- added to cart if ( ! in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && $is_last_item ) { $cart->remove_cart_item( $cart_item_key ); // Optionaly displaying a notice for the removed item: wc_add_notice( __( 'Due to the presale, AMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' ); } // Remove the specific item when its is not the last added to cart elseif ( in_array($specific_product_id, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) && ! $is_last_item ) { $cart->remove_cart_item( $cart_item_key ); // Optionaly displaying a notice for the removed item: wc_add_notice( __( 'Due to the presale, AMI products cannot be purchased with other products in the same order.', 'theme_domain' ), 'notice' ); } } }