Эх сурвалжийг харах

studiou-wc-mandatory-products: rewrite to be Hi-Res Woo Commerce compatibility. works

Dalibor Votruba 1 жил өмнө
parent
commit
3e41071470

+ 9 - 0
studiou-wc-mandatory-products/directory-structure.txt

@@ -0,0 +1,9 @@
+studiou-wc-mandatory-products/
+├── assets/
+│   └── css/
+│       └── frontend.css
+├── includes/
+│   ├── class-studiou-wcmp-admin.php
+│   └── class-studiou-wcmp-frontend.php
+├── readme.md
+└── studiou-wc-mandatory-products.php

+ 22 - 1
studiou-wc-mandatory-products/includes/class-studiou-wcmp-frontend.php

@@ -103,7 +103,22 @@ class StudioU_WCMP_Frontend {
      * @return string Modified remove link
      */
     public function adjust_cart_item_remove_link($link, $cart_item_key) {
-        $cart_item = WC()->cart->get_cart()[$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']) {
@@ -119,10 +134,16 @@ class StudioU_WCMP_Frontend {
      * 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();

+ 2 - 0
studiou-wc-mandatory-products/readme.md

@@ -32,6 +32,8 @@ StudioU WC Mandatory Products allows store administrators to define mandatory pr
 - WooCommerce 3.0 or higher
 - PHP 8.2 or higher
 
+This plugin is compatible with WooCommerce's High-Performance Order Storage (HPOS) feature.
+
 ## Usage
 
 ### Setting Up Mandatory Products

+ 15 - 3
studiou-wc-mandatory-products/studiou-wc-mandatory-products.php

@@ -10,10 +10,11 @@
  * Author URI: https://www.quadarax.com
  * License: GPL v2 or later
  * License URI: https://www.gnu.org/licenses/gpl-2.0.html
- * Text Domain: studiou-wc-product-cat-manage
+ * Text Domain: studiou-wc-mandatory-products
  * Domain Path: /languages
  * WC requires at least: 3.0
  * WC tested up to: 8.0
+ * Woo: 12345:a123b456c789d0
  */
 
 // Exit if accessed directly
@@ -26,6 +27,17 @@ if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get
     return;
 }
 
+// Declare HPOS compatibility
+add_action('before_woocommerce_init', function() {
+    if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
+        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
+            'custom_order_tables',
+            __FILE__,
+            true
+        );
+    }
+});
+
 if (!class_exists('StudioU_WC_Mandatory_Products')) {
 
     class StudioU_WC_Mandatory_Products {
@@ -79,8 +91,8 @@ if (!class_exists('StudioU_WC_Mandatory_Products')) {
             // Frontend hooks
             add_action('init', array($this, 'init_frontend'), 10);
             
-            // Add mandatory products to cart
-            add_action('woocommerce_add_to_cart', array($this, 'add_mandatory_products_to_cart'), 10, 6);
+            // Add mandatory products to cart (with priority to ensure it runs after WooCommerce's own handlers)
+            add_action('woocommerce_add_to_cart', array($this, 'add_mandatory_products_to_cart'), 20, 6);
             
             // Display mandatory products info on product page
             add_action('woocommerce_before_add_to_cart_button', array($this, 'display_mandatory_products_info'), 10);