Example #1
0
    /**
     * The getCategoriesWithProducts function returns all defined community store categories,
     * with the first six products of the category.
     * The function selects first all categories and calls for each category the internal helper function
     * "getCategoryProducts" which selects all products for a specify store category.
     *
     * @param      $offset
     * @param      $limit
     * @param null $orderBy
     * @param null $filters
     * @param      $shopwareVersion
     * @return array|Shopware_StoreApi_Core_Response_SearchResult
     */
    public function getCategoriesWithProducts($offset = null, $limit = null, $orderBy = null, $filters = null, $shopwareVersion)
    {
        $categoryQuery = new Shopware_StoreApi_Models_Query_Category();
        $categoryQuery->setOrderBy(Shopware_StoreApi_Models_Query_Category::ORDER_BY_DESCRIPTION);
        $categoryQuery->setOrderDirection(Shopware_StoreApi_Models_Query_Category::ORDER_DIRECTION_ASC);
        $productQuery = $this->getProductQueryForListing($offset, $limit, $orderBy, $filters);
        $productQuery->addCriterion(
            new Shopware_StoreApi_Models_Query_Criterion_Version($shopwareVersion)
        );

        $resultSet = $this->getProductService()->getProductsGroupByCategories($productQuery, $categoryQuery);

        if ($resultSet instanceof Shopware_StoreApi_Exception_Response) {
            return $resultSet;
        }
        $iterator = $resultSet->getIterator();
        $categories = array();

        /**@var $model Shopware_StoreApi_Models_Category */
        foreach($iterator as $model) {
            $category = $model->getRawData();
            $productResult = $model->getProducts();
            if ($productResult instanceof Shopware_StoreApi_Exception_Response) {
                $products = array('message' => $productResult->getMessage(), 'code' => $productResult->getCode());
            } else {
                $products = array();
                /**@var $productModel Shopware_StoreApi_Models_Product*/
                foreach($productResult as $productModel) {
                    $product = $productModel->getRawData();
                    $product['details'] = $productModel->getDetails();
                    $products[] = $product;
                }
            }
            unset($category['_products']);
            $category['products'] = $products;
            $categories[] = $category;
        }

        return $categories;
    }