| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- /**
- * StudioU WC Mandatory Products Frontend
- *
- * @package StudioU_WC_Mandatory_Products
- */
- // Exit if accessed directly
- if (!defined('ABSPATH')) {
- exit;
- }
- class StudioU_WCMP_Frontend {
- /**
- * Constructor
- */
- public function __construct() {
- // Nothing to do at constructor
- }
- /**
- * Initialize hooks
- */
- public function init() {
- // Add CSS for frontend
- add_action('wp_enqueue_scripts', array($this, 'enqueue_styles'));
-
- // Add custom item data to cart item
- add_filter('woocommerce_get_item_data', array($this, 'add_cart_item_data'), 10, 2);
-
- // Prevent quantity changes for mandatory products
- add_filter('woocommerce_cart_item_quantity', array($this, 'adjust_cart_item_quantity'), 10, 3);
-
- // Make mandatory products non-removable if needed
- add_filter('woocommerce_cart_item_remove_link', array($this, 'adjust_cart_item_remove_link'), 10, 2);
-
- // Validate cart to ensure mandatory products are present
- add_action('woocommerce_check_cart_items', array($this, 'validate_cart_has_mandatory_products'));
- }
- /**
- * Enqueue frontend styles
- */
- public function enqueue_styles() {
- if (is_product() || is_cart()) {
- wp_enqueue_style(
- 'studiou-wcmp-styles',
- STUDIOU_WCMP_PLUGIN_URL . 'assets/css/frontend.css',
- array(),
- STUDIOU_WCMP_VERSION
- );
- }
- }
- /**
- * Add custom item data to cart item
- *
- * @param array $item_data Cart item data
- * @param array $cart_item Cart item
- * @return array Modified item data
- */
- public function add_cart_item_data($item_data, $cart_item) {
- if (isset($cart_item['mandatory_product']) && $cart_item['mandatory_product']) {
- $item_data[] = array(
- 'key' => __('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(
- '<div class="quantity"><input type="text" value="%s" class="input-text qty text" readonly="readonly" /></div>',
- 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 '<span class="mandatory-product-info" title="' .
- esc_attr__('This is a mandatory product and cannot be removed directly', 'studiou-wc-product-cat-manage') .
- '"><i class="dashicons dashicons-lock"></i></span>';
- }
-
- 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'
- );
- }
- }
- }
- }
|