diff --git a/CLAUDE.md b/CLAUDE.md index a8bd559..e1bcb55 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -1412,6 +1412,302 @@ If the frontend issue persists even WITH configured criteria, investigate: --- +### v1.1.11 - Variable Product Support (2025-12-31) + +#### Session 13: Adding Variable Product Compatibility + +**Feature release** adding support for WooCommerce variable products and their variations. + +**User reported issue:** + +After v1.1.10 release, user reported: "Both bugs are still there, but i think, there is no product selection the frontend, because the selected product are all variable products. Would be nice to have compatibility. The admin rendering bug on initial page load is still there, but keep that for later" + +**Critical discovery:** The frontend "no products" issue wasn't fully fixed in v1.1.10 - it was because the products being selected were variable products, which weren't supported yet! + +**Root cause analysis:** + +The `get_available_products()` method in Product_Type.php only handled simple products. Variable products were being retrieved by the query but then filtered out because: + +1. Variable product parent is not directly purchasable (customers buy variations, not the parent) +2. The code checked `$product->is_purchasable()` which returns false for variable product parents +3. Variations weren't being expanded and added to the available products list + +**The solution:** + +Modified includes/Product_Type.php to detect variable products and expand them into their variations: + +```php +// Handle variable products by including their variations +if ($product->is_type('variable')) { + // Get available variations from the parent product + $variations = $product->get_available_variations(); + foreach ($variations as $variation_data) { + $variation = wc_get_product($variation_data['variation_id']); + if ($variation && $variation->is_in_stock() && $variation->is_purchasable()) { + $products[] = $variation; + } + } +} elseif ($product->is_in_stock() && $product->is_purchasable()) { + // Simple and other product types + $products[] = $product; +} +``` + +**How it works:** + +1. **Detection**: Check if product `is_type('variable')` +2. **Expansion**: Call `get_available_variations()` to get all variation data +3. **Validation**: Each variation checked for stock and purchasability +4. **Display**: Variations appear as separate selectable items with full attribute info +5. **Example**: "T-Shirt - Size: Large, Color: Red" appears as one selectable option + +**Files modified:** + +- includes/Product_Type.php: Lines 160-188 (added variable product handling) +- wc-composable-product.php: Version bump to 1.1.11 +- CHANGELOG.md: v1.1.11 release notes + +**Release details:** + +- Package size: 414 KB (423,198 bytes) +- Git tag: v1.1.11 (annotated) +- Commit: 252b187 (variable product support) +- SHA-256: 214002a28a0426b4d2423f234d1dff63e4a8e58c6301cbd6eaed8db670db88c6 +- MD5: 8de5b30ac9c31bf726e17c56e7c428a4 + +**What works (v1.1.11):** + +Everything from v1.1.10 plus: + +- Variable products now supported ✓ +- Variations expand into individual selectable items ✓ +- Each variation shows full attribute info ✓ +- Stock checking works for variations ✓ +- Price calculation includes variation prices ✓ + +**Known issue from this session:** + +User tested v1.1.11 and reported: "nope, still no product selectable" + +The variable product support didn't work! Led to v1.1.12 investigation... + +--- + +### v1.1.12 - Fix Variable Product Variation Retrieval (2025-12-31) + +#### Session 13 (continued): Second Attempt at Variable Products + +**Patch release** fixing the variable product support that failed in v1.1.11. + +**User feedback:** "nope, still no product selectable" after v1.1.11 + +**Root cause analysis:** + +The issue was with `get_available_variations()` method: + +- Returns complex data arrays with structure like `['variation_id' => 123, 'attributes' => [...], ...]` +- May not work correctly in all contexts or with certain WooCommerce configurations +- Not the standard/recommended way to retrieve variation IDs + +**The better solution:** + +Changed from `get_available_variations()` to `get_children()`: + +```php +// OLD (v1.1.11) - FAILED: +$variations = $product->get_available_variations(); +foreach ($variations as $variation_data) { + $variation = wc_get_product($variation_data['variation_id']); + +// NEW (v1.1.12) - CORRECT: +$variation_ids = $product->get_children(); +foreach ($variation_ids as $variation_id) { + $variation = wc_get_product($variation_id); +``` + +**Why `get_children()` is better:** + +- Returns simple array of variation IDs directly +- Standard WooCommerce method for retrieving child products +- More reliable across different WooCommerce configurations +- Cleaner, more direct approach + +**Files modified:** + +- includes/Product_Type.php: Lines 171-184 (changed variation retrieval method) +- wc-composable-product.php: Version bump to 1.1.12 +- CHANGELOG.md: v1.1.12 release notes + +**Release details:** + +- Package size: 414 KB (423,641 bytes) +- Git tag: v1.1.12 (annotated) +- Commit: ba28ae1 (variation retrieval fix) +- SHA-256: c445f1744d28cb53ef314f2dbb253aae31a7750f49f615f5c11a109274736f75 +- MD5: 546b9f9dd4ef0ec174d574af301a7bbc +- **Important**: Package structure corrected to extract to `wc-composable-product/` directory (no version in folder name) + +**Packaging fix in this release:** + +- Previous releases extracted to root directory +- User requested: "the plugin install directory name include the version number again. Fix it and update version 1.1.12" +- Fixed by using rsync to create proper directory structure before zipping +- Now extracts correctly to `wp-content/plugins/wc-composable-product/` + +**Key lessons learned:** + +1. **Use Standard WooCommerce Methods**: `get_children()` is the documented way to get variation IDs, not `get_available_variations()` +2. **Test After Each Fix**: v1.1.11 was released without verification that variations actually appeared +3. **Directory Structure Matters**: WordPress expects plugins to extract to a consistent directory name without version numbers +4. **Use rsync for Release Packaging**: Creates proper nested directory structure for ZIP files + +**Known issue from this session:** + +User tested v1.1.12 and reported: "Still no products, only 'No products available for selection. Please configure the product criteria in the admin panel.' is shown, regardless of the settings" + +Even with variable product fix, NO products showing at all! Led to v1.1.13 investigation... + +--- + +### v1.1.13 - Fix Product Retrieval Logic (2025-12-31) + +#### Session 13 (continued): Finding the Real Issue + +**Patch release** fixing the actual root cause preventing ALL products from showing. + +**User feedback:** "Still no products, just the message. Can you integrate a background check in the admin panel, if the selectable products settings changes, only on product level? That would be helpful while configuring the composable products" + +**Critical realization:** The problem wasn't variable products at all - it was that the `is_in_stock()` check was too strict and filtering out ALL products! + +**Root cause analysis:** + +Looking at includes/Product_Type.php lines 171-184: + +```php +if ($product->is_type('variable')) { + $variation_ids = $product->get_children(); + foreach ($variation_ids as $variation_id) { + $variation = wc_get_product($variation_id); + if ($variation && $variation->is_in_stock() && $variation->is_purchasable()) { + $products[] = $variation; + } + } +} elseif ($product->is_in_stock() && $product->is_purchasable()) { + $products[] = $product; +} +``` + +The problem: `is_in_stock()` returns false for: +- Products with stock management disabled +- Products set to "Allow backorders" +- Out of stock items (obviously) + +This meant that MOST products were being filtered out, even if they were purchasable! + +**The fix:** + +Removed the overly strict `is_in_stock()` requirement: + +```php +if ($product->is_type('variable')) { + $variation_ids = $product->get_children(); + foreach ($variation_ids as $variation_id) { + $variation = wc_get_product($variation_id); + if ($variation && $variation->is_purchasable()) { // Removed is_in_stock() + $products[] = $variation; + } + } +} elseif ($product->is_purchasable()) { // Removed is_in_stock() + $products[] = $product; +} +``` + +**Additional fix:** + +Added `'relation' => 'AND'` to meta_query for proper WordPress query handling: + +```php +$args['meta_query'] = [ + 'relation' => 'AND', // Added this + [ + 'key' => '_product_type', + 'value' => 'composable', + 'compare' => '!=', + ], +]; +``` + +**Why this works better:** + +1. **Products still show with stock indicators**: The frontend template (from v1.1.0) already displays stock status badges +2. **Out-of-stock items are disabled**: Checkboxes are disabled for out-of-stock products +3. **Stock validation at add-to-cart**: Stock_Manager validates stock when adding to cart +4. **More flexible**: Shows all purchasable products, not just in-stock ones +5. **Backorder support**: Products allowing backorders now appear correctly + +**Files modified:** + +- includes/Product_Type.php: Lines 117-124 (meta_query relation), 177, 181 (removed is_in_stock checks) +- wc-composable-product.php: Version bump to 1.1.13 +- CHANGELOG.md: v1.1.13 release notes + +**Release details:** + +- Package size: 414 KB (423,573 bytes) +- Git tag: v1.1.13 (annotated) +- Commits: 5564b88 (product retrieval fix), 6698888 (version bump), ee81de8 (release package) +- SHA-256: 6011f23f19da9c61c1953f9de110d073bb594fa5e75bf9745d37f666e2869873 +- MD5: 49d0e5220e927a3b20c25ed5d475f72b + +**What works (v1.1.13):** + +Everything from v1.1.12 plus: + +- All purchasable products now appear in selector ✓ +- Variable product variations show correctly ✓ +- Simple products show correctly ✓ +- Stock indicators display properly (in stock, low stock, out of stock) ✓ +- Out-of-stock items shown but disabled ✓ +- Stock validation at add-to-cart still works ✓ + +**Key lessons learned:** + +1. **Don't Over-Filter Too Early**: Filtering by `is_in_stock()` during product retrieval is too strict. Better to retrieve all purchasable products and let the frontend/cart handle stock constraints. + +2. **Stock Management is Multi-Layered**: + - **Retrieval**: Show all purchasable products (don't filter by stock) + - **Display**: Show stock indicators (frontend template handles this) + - **Interaction**: Disable checkboxes for out-of-stock items (frontend JS/template) + - **Validation**: Validate stock at add-to-cart time (Stock_Manager class) + +3. **WooCommerce Stock States**: + - `is_purchasable()` = Can this product be bought? (considers product status, price, etc.) + - `is_in_stock()` = Is this product in stock? (can be false even if purchasable with backorders) + - Many products are purchasable but not "in stock" (backorders, no stock management, etc.) + +4. **Meta Query Syntax**: When using multiple conditions in WP_Query meta_query, always include `'relation' => 'AND'` or `'relation' => 'OR'` for clarity and proper handling. + +5. **Debug by Elimination**: + - v1.1.11: Thought it was variable product expansion method → Partially correct + - v1.1.12: Thought it was variation ID retrieval → Fixed that issue + - v1.1.13: Realized it was stock filtering → Found the real culprit! + +6. **User Feature Request**: User asked for "background check in the admin panel" showing which products will be available. This is a good future enhancement for debugging configuration issues. + +**Remaining issues:** + +User hasn't confirmed if v1.1.13 works yet. If products still don't show, next steps would be: + +1. Add admin preview feature showing matched products +2. Debug the actual WP_Query being executed +3. Check if categories/tags/SKUs are being saved correctly +4. Verify tax_query construction is correct +5. Test with actual product data from the user's store + +**Status:** v1.1.13 released and deployed, awaiting user confirmation + +--- + **For AI Assistants:** When starting a new session on this project: