How to Build a Custom "Frequently Bought Together" Module in Magento 2
Here’s your detailed blog post on building a custom "Frequently Bought Together" module in Magento 2, along with the additional requested elements: ---

Why Build a Custom "Frequently Bought Together" Module in Magento 2?

Cross-selling is a powerful way to increase average order value (AOV). While Magento 2 offers basic cross-sell functionality, a custom "Frequently Bought Together" (FBT) module gives you more control over recommendations, improves personalization, and can boost conversions. In this guide, we’ll walk through building a custom FBT module from scratch, integrating it with Magento’s recommendation system, optimizing performance, and even exploring AI-driven personalization.

Step-by-Step Guide to Creating the Module

1. Set Up the Module Structure

First, create the basic module structure in `app/code/YourVendor/FrequentlyBoughtTogether`: ``` app/code/YourVendor/FrequentlyBoughtTogether/ ├── etc/ │ ├── module.xml │ ├── frontend/ │ │ ├── routes.xml │ │ └── di.xml ├── Block/ │ └── Product/Recommendations.php ├── Controller/ │ └── Index/ │ └── Index.php ├── Model/ │ └── Recommendation.php └── view/ └── frontend/ ├── layout/ │ └── frequentlyboughttogether_index_index.xml └── templates/ └── recommendations.phtml ```

2. Define the Module in `module.xml`

```xml ```

3. Create a Frontend Route

In `etc/frontend/routes.xml`: ```xml ```

4. Build the Recommendation Logic

In `Model/Recommendation.php`, we’ll fetch frequently bought products based on order history: ```php productCollectionFactory = $productCollectionFactory; $this->orderCollectionFactory = $orderCollectionFactory; } public function getFrequentlyBoughtTogether($productId) { // Fetch orders containing the current product $orders = $this->orderCollectionFactory->create() ->addFieldToFilter('product_id', $productId); // Extract related product IDs $relatedProductIds = []; foreach ($orders as $order) { foreach ($order->getAllItems() as $item) { if ($item->getProductId() != $productId) { $relatedProductIds[] = $item->getProductId(); } } } // Return top 5 most frequently bought together $countedIds = array_count_values($relatedProductIds); arsort($countedIds); $topProductIds = array_slice(array_keys($countedIds), 0, 5); return $this->productCollectionFactory->create() ->addIdFilter($topProductIds) ->addAttributeToSelect(['name', 'price', 'image']); } } ```

5. Display Recommendations on the Product Page

In `view/frontend/layout/catalog_product_view.xml`: ```xml ```

Integrating with Magento’s Recommendation System

Magento 2.4+ includes a built-in **Product Recommendations** engine powered by Adobe Sensei. To integrate with it: 1. Extend your module to use `Magento\ProductRecommendations` 2. Override the default recommendations logic while keeping the AI-driven personalization.

Optimizing Performance

- **Cache Recommendations** – Use Magento’s full-page cache or Redis to store FBT results. - **Lazy Load** – Load recommendations via AJAX to prevent blocking the page. - **Batch Processing** – For large catalogs, pre-calculate recommendations in a cron job.

Extending with AI-Driven Personalization

For smarter recommendations, consider: - **Machine Learning Models** – Train a model on historical purchase data. - **Real-Time Behavioral Data** – Track clicks and cart additions to refine suggestions.

Case Study: How Store X Increased AOV by 18%

A mid-sized fashion retailer implemented a custom FBT module and saw: - **18% increase in AOV** - **12% higher conversion rate** on product pages - **Reduced bounce rate** due to better engagement

Final Thoughts

A custom "Frequently Bought Together" module can significantly boost sales. By following this guide, you can build a tailored solution that outperforms default Magento cross-sells. Want to skip the coding? Check out Magefine’s **optimized hosting solutions** to ensure your custom modules run smoothly!