| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCart\App\Http\Controllers; |
| 4 | 4 | |
| 5 | +use FluentCart\Api\ModuleSettings; | |
| 5 | 6 | use FluentCart\Api\Resource\ProductDetailResource; |
| 6 | 7 | use FluentCart\Api\Resource\ProductResource; |
| 7 | 8 | use FluentCart\Api\Resource\ProductVariationResource; |
| 8 | 9 | use FluentCart\App\Events\StockChanged; |
| @@ -49,11 +50,29 @@ | ||
| 49 | 50 | // Attach the resolved variation_display_title to each variation (batched, |
| 50 | 51 | // no N+1) so the admin order product picker matches the order item display. |
| 51 | 52 | AttributeHelper::attachVariationDisplayTitles($products->getCollection()); |
| 52 | 53 | |
| 54 | + $collection = $products->getCollection(); | |
| 55 | + | |
| 53 | 56 | $products->setCollection( |
| 54 | - $products->getCollection()->transform(function ($product) { | |
| 55 | - return $product->setAppends(['view_url', 'edit_url']); | |
| 57 | + $collection->transform(function ($product) { | |
| 58 | + $product->setAppends(['view_url', 'edit_url']); | |
| 59 | + | |
| 60 | + // Source rating from canonical detail.other_info (maintained by recalculateProductRatings). | |
| 61 | + // Use array_key_exists to avoid overwriting valid data with fallback-to-zero. | |
| 62 | + if ($product->detail) { | |
| 63 | + $otherInfo = $product->detail->other_info ?? []; | |
| 64 | + | |
| 65 | + if (array_key_exists('average_rating', $otherInfo)) { | |
| 66 | + $product->avg_rating = (float) $otherInfo['average_rating']; | |
| 67 | + } | |
| 68 | + | |
| 69 | + if (array_key_exists('review_count', $otherInfo)) { | |
| 70 | + $product->reviews_count = (int) $otherInfo['review_count']; | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + return $product; | |
| 56 | 75 | }) |
| 57 | 76 | ); |
| 58 | 77 | |
| 59 | 78 | $products = apply_filters('fluent_cart/products_list', $products); |
| @@ -1539,8 +1558,13 @@ | ||
| 1539 | 1558 | } |
| 1540 | 1559 | |
| 1541 | 1560 | public function updateInventory(Request $request, $postId, $variantId) |
| 1542 | 1561 | { |
| 1562 | + if (!ModuleSettings::isActive('stock_management')) { | |
| 1563 | + return $this->response->sendError([ | |
| 1564 | + 'message' => __('Stock Management module is disabled. Enable it from Settings to manage inventory.', 'fluent-cart') | |
| 1565 | + ], 422); | |
| 1566 | + } | |
| 1543 | 1567 | |
| 1544 | 1568 | $variant = ProductVariation::query()->find($variantId); |
| 1545 | 1569 | |
| 1546 | 1570 | if (!$variant) { |
| @@ -1605,8 +1629,17 @@ | ||
| 1605 | 1629 | |
| 1606 | 1630 | public function updateManageStock(Request $request, $postId) |
| 1607 | 1631 | { |
| 1608 | 1632 | $manageStock = sanitize_text_field($request->get('manage_stock')); |
| 1633 | + | |
| 1634 | + // Turning inventory ON requires the Stock Management module to be active. | |
| 1635 | + // Turning it OFF stays allowed so a store that disables the module can | |
| 1636 | + // still clean up products that were left with manage_stock = 1. | |
| 1637 | + if ($manageStock == 1 && !ModuleSettings::isActive('stock_management')) { | |
| 1638 | + return $this->response->sendError([ | |
| 1639 | + 'message' => __('Stock Management module is disabled. Enable it from Settings to manage inventory.', 'fluent-cart') | |
| 1640 | + ], 422); | |
| 1641 | + } | |
| 1609 | 1642 | |
| 1610 | 1643 | $detail = ProductDetail::query()->where('post_id', $postId)->first(); |
| 1611 | 1644 | |
| 1612 | 1645 | $updateData = [ |