__('Mandatory Item', 'studiou-wc-product-cat-manage'), 'value' => __('This item was automatically added', 'studiou-wc-product-cat-manage') ); } return $item_data; } /** * Adjust cart item quantity field for mandatory products * * @param string $product_quantity HTML of quantity field * @param string $cart_item_key Cart item key * @param array $cart_item Cart item * @return string Modified quantity field HTML */ public function adjust_cart_item_quantity($product_quantity, $cart_item_key, $cart_item) { // If this is a mandatory product, make quantity field readonly if (isset($cart_item['mandatory_product']) && $cart_item['mandatory_product']) { // Get current quantity $quantity = $cart_item['quantity']; // Return readonly quantity field return sprintf( '
', esc_attr($quantity) ); } return $product_quantity; } /** * Adjust remove link for mandatory products * * @param string $link The cart item remove link * @param string $cart_item_key The cart item key * @return string Modified remove link */ public function adjust_cart_item_remove_link($link, $cart_item_key) { if (!isset(WC()->cart) || !method_exists(WC()->cart, 'get_cart') || !method_exists(WC()->cart, 'get_cart_item')) { return $link; } // Use get_cart_item method if available (WC 3.7+) if (method_exists(WC()->cart, 'get_cart_item')) { $cart_item = WC()->cart->get_cart_item($cart_item_key); } else { $cart = WC()->cart->get_cart(); $cart_item = isset($cart[$cart_item_key]) ? $cart[$cart_item_key] : null; } // Return original link if cart item not found if (!$cart_item) { return $link; } // If this is a mandatory product, remove the ability to remove it if (isset($cart_item['mandatory_product']) && $cart_item['mandatory_product']) { return ''; } return $link; } /** * Validate cart to ensure mandatory products are present */ public function validate_cart_has_mandatory_products() { // Skip if in admin if (is_admin()) { return; } // Skip if cart is not initialized if (!function_exists('WC') || !WC()->cart || WC()->cart->is_empty()) { return; } $cart_contents = WC()->cart->get_cart(); $plugin = new StudioU_WC_Mandatory_Products(); $required_mandatory_products = array(); // First, get all mandatory products that should be in the cart foreach ($cart_contents as $cart_item) { // Skip if this is already a mandatory product if (isset($cart_item['mandatory_product']) && $cart_item['mandatory_product']) { continue; } $product_id = $cart_item['product_id']; $mandatory_products = $plugin->get_mandatory_products($product_id); foreach ($mandatory_products as $mandatory_product) { $mandatory_product_id = $mandatory_product['product_id']; $mandatory_product_type = $mandatory_product['type']; $mandatory_product_quantity = $mandatory_product['quantity']; // For OnePerCart, add once to requirements if ($mandatory_product_type === 'OnePerCart') { $key = 'cart_' . $mandatory_product_id; if (!isset($required_mandatory_products[$key])) { $required_mandatory_products[$key] = array( 'product_id' => $mandatory_product_id, 'quantity' => $mandatory_product_quantity, 'type' => $mandatory_product_type ); } } // For OnePerProduct, add for each product else if ($mandatory_product_type === 'OnePerProduct') { $key = 'product_' . $product_id . '_' . $mandatory_product_id; if (!isset($required_mandatory_products[$key])) { $required_mandatory_products[$key] = array( 'product_id' => $mandatory_product_id, 'quantity' => $mandatory_product_quantity, 'type' => $mandatory_product_type, 'for_product_id' => $product_id ); } } } } // Now check if all required mandatory products are in the cart $missing_products = array(); foreach ($required_mandatory_products as $key => $required_product) { $found = false; foreach ($cart_contents as $cart_item) { if (isset($cart_item['product_id']) && $cart_item['product_id'] == $required_product['product_id']) { // For OnePerCart, just check it exists if ($required_product['type'] === 'OnePerCart') { $found = true; break; } // For OnePerProduct, check it exists for the specific product else if ($required_product['type'] === 'OnePerProduct' && isset($cart_item['mandatory_for']) && $cart_item['mandatory_for'] == $required_product['for_product_id']) { $found = true; break; } } } if (!$found) { $missing_products[] = $required_product; } } // If we have missing products, add them and show notice if (!empty($missing_products)) { foreach ($missing_products as $missing_product) { $product_id = $missing_product['product_id']; $quantity = $missing_product['quantity']; $type = $missing_product['type']; $extra_data = array( 'mandatory_product' => true ); // For OnePerProduct, add the for_product_id if ($type === 'OnePerProduct' && isset($missing_product['for_product_id'])) { $extra_data['mandatory_for'] = $missing_product['for_product_id']; } // Add the product to cart WC()->cart->add_to_cart( $product_id, $quantity, 0, // No variation array(), // No variation data $extra_data ); // Get product name $product = wc_get_product($product_id); $product_name = $product ? $product->get_name() : __('A product', 'studiou-wc-product-cat-manage'); // Add notice wc_add_notice( sprintf( __('%s has been automatically added to your cart as it is required.', 'studiou-wc-product-cat-manage'), $product_name ), 'notice' ); } } } }