Thought I could share the code I've written to create a custom menu using the API, maybe it can be to some help. You can see the menu live on http://groweco.se
What the code does:
1. The code will first retrieve all the root categories.
2. Then it will fetch the sub categories if they exists.
3. If sub categories exists, the code will get one random product from the parent category, which will be shown together with the sub categories in the drop down sub menu. The product will be presented with product image, link, product title, price and the Add to cart button. Every time you refresh/revisit the site a new random product will be shown for the categories that have a sub menu. For load time reason I have cached my wordpress site, so the random product is refreshed once every day, I suggest you do the same. If you are using Wordpress for your site, install a cache plugin like WP Super Cache.
The categories are retrieved in the order that you have set in your Ecwid Control Panel.
The code:
PHP Code:
<?php
include_once "ecwid_product_api.php";
$api = new EcwidProductApi(YOURSTOREID); // initialize the API class
?>
<div id="meny">
<div id="ecwid-menu">
<?php
//get parent categories using the ID
$categories = $api->get_subcategories_by_id($parent_category_id = 0);
echo '<ul>';
foreach($categories as $category) {
echo '<li id="parentitem">';
echo <<<EOT
<a href='{$category["url"]}' style='float: left; width: auto;'>{$category["name"]}</a>
EOT;
//get sub categories if they exist using category ID from parent categories above
$subcategories = $api->get_subcategories_by_id($category["id"]);
if ($subcategories) {
echo '<ul id="submenu">';
echo '<div id="categorytitle">' . $category["name"] . '</div>';
//get one random product from each category that has a subcategory
$categorytips = $api->get_products_by_category_id($category["id"]);
shuffle($categorytips);
$i=0;
foreach($categorytips as $categorytip) if ($i < 1) {
$produktid = $categorytip['id'];
echo <<<EOT
<div id="tips">
<strong>Populärast just nu</strong><br>
<a href="{$categorytip["url"]}" class="productlink">{$categorytip["name"]}<br>
<img src="{$categorytip["thumbnailUrl"]}"></a><br>
{$categorytip["price"]} SEK<br>
<script type="text/javascript">xAddToBag('productid=$produktid');</script>
</div>
EOT;
$i +=1;
}
foreach($subcategories as $subcategory) {
echo <<<EOT
<li id="subitem"><a href='{$subcategory["url"]}'>{$subcategory["name"]}</a></li>
EOT;
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
?>
</div>
</div>
The final result:
