* @var MenuItem
 */
$recommendedProductsPanel = null;
// Build a sidebar panel with related product items on the cart checkout page.
add_hook('PreCalculateCartTotals', 1, function (array $parameters) use($recommendedProducts, &$recommendedProductsPanel) {
    // Don't build a panel if there's nothing in the cart (because there's
    // nothing to recommend!)
    if (count($parameters['products']) == 0) {
        return;
    }
    $recommendedProductsPanel = new MenuItem('Related Products', new MenuFactory());
    // Look through the products in the cart and instantiate a new Product
    // object based off the product's id.
    foreach ($parameters['products'] as $product) {
        $product = Product::find($product['pid']);
        // Add the product type's related products to the new panel.
        foreach ($recommendedProducts[$product->type] as $recommendedProduct) {
            $recommendedProductsPanel->addChild($recommendedProduct);
        }
    }
});
// Assign the new panel to the secondary sidebar.
add_hook('ClientAreaSecondarySidebar', 1, function (MenuItem $secondarySidebar) use(&$recommendedProductsPanel) {
    // $recommendedProductsPanel is only defined on the cart checkout page,
    // so if it's null then the user is on another page and we shouldn't
    // edit the sidebar.
    if (is_null($recommendedProductsPanel)) {
        return;
    }
    $secondarySidebar->addChild($recommendedProductsPanel);
});